I'm confused. Child classes inherit their parents public and protected properties, right? When then am I getting NULL when I try to var_dump() $data in the home controller but get array(1) { ["user_info"]=> array(2) { [0]=> bool(false) [1]=> bool(false) } } when I var_dump() it in the MY_Controller controller.
MY_Controller.php
PHP Code:
class MY_Controller extends CI_controller {
public $user_info = array();
public function __construct() { parent::__construct(); $this->user_info[] = $this->session->userdata('username'); $this->user_info[] = $this->session->userdata('id'); $data=array(); $data['user_info'] = $this->user_info; } }
/* end of class */
Home.php
PHP Code:
class Home extends MY_Controller {
public function __construct() { parent::__construct(); //tried var_dump($this->data); here <<--- look }
public function index() { //and tried var_dump($this->data); here <<--- look
$data['title'] = 'Dog Realm - A Virtual Dog Game';
$data is not defined as a class variable, so it is only visible within the parent class's constructor. If it needs to be accessible elsewhere, then you need to define it as a class variable and access it via $this->data.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
I think you're confusing method-local variables and class/object variables. Without the "$this->" in front of it, you are referring to a separate variable that is local only to that method, and has no connection of any sort with the object property of the same name.
PHP Code:
class A { public $foo; public function test() { $foo = 1; $this->foo = 99; echo $foo . " - " . $this->foo; } }
$obj = new A(); $obj->test(); // outputs "1 - 99"
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Only if you have a way to identify the IP address as being a proxy (maybe a DB of known proxy hosts?). However, the downside is that you then risk excluding people who are using proxy hosts for non-malicious reasons.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
**I didn't use PHP bracket tags around the code because it was omitting some of the $this variables in my code They need to fix that 0_0 Hope you can read this
Anyway, I come with two problems. First off, I'm wondering if I'm putting my methods in the best controllers. I made an Auth controller and put a bunch of methods in it.
It has:
signup() - public
login() - public
logout() - public
activate() - public
send_code() - private, used methods that send a code through email
resend_code() - public, re-sends the activation code (utilizes send_code())
change_email() - public, uses send_code() because it deactivates account and sends another activation code
create_code() - private, creates a 10 character string of random letters and numbers, used for activation codes and other kinds of codes
forgot_password() - public, accepts a login name, sends a password change code to email on file with account
reset_password() - public, accepts a login name and password reset code, allows user to change password for account
change_password() - public, lets somebody change their password the normal way, by entering their old password and the new password they want
change_login_name() - public, haven't made yet but will let somebody change their login name
So, Question #1 do all these belong in Auth?
Now, I'm also wondering if I'm putting too much functionality in each of my methods and if I would be better off splitting the functionality into several methods. Auth::change_password() is a good example of the approach I've been taking. Here it is:
public function change_password()
{
$this->data['title'] = 'Change Password';
$this->data['captcha'] = $this->load_captcha();
if (!$this->user_info['logged_in']) // can't change an account's password unless you're logged into it
{
$this->data['alert_type'] = 'problem'; //my view is set up so if alert_type and alert_message are set, a message box will display
$this->data['alert_message'] = 'You have to be logged in to an account, if you want to change its password.';
}
else
{
if ($this->input->post('process'))
{
$this->load->model('auth_model');
$login_name = $this->auth_model->get_user_info($this->user_info['user_id'], 'login_name'); //querys the database with SELECT `login_name` FROM `user_accounts` WHERE `user_id` = 'whatever id was entered'
$this->load->library('form_validation');
$this->form_validation->set_rules('current_password', 'Current Password', 'trim|required|max_length[25]');
$this->form_validation->set_rules('new_password_1', 'New Password', 'trim|required|matches[new_password_2]|does_not_match['.$login_name.','.$this->user_info['display_name'].']|min_length[6]|max_length[25]'); //password can't be the same as login_name or display_name
$this->form_validation->set_rules('new_password_2', 'Enter Again', 'trim|required');
$this->form_validation->set_rules('recaptcha_response_field', 'Image Verification', 'required|captcha'); //captcha looks at what was entered and sees if it matches the text in the image verification
$this->form_validation->set_message('does_not_match', 'Your new password cannot be the same as your display name or login name.');
if (!$this->form_validation->run())
{
$this->data['alert_type'] = 'problem';
$this->data['alert_message'] = validation_errors();
$this->data['extra_brs'] = 'no';
}
elseif (!$this->auth_model->password_check($this->user_info['user_id'], $this->input->post('current_password')))
{
$this->data['alert_type'] = 'problem';
$this->data['alert_message'] = 'What you put for your current password was wrong. Did you forget it? If so, <a href="http://'.$_SERVER['HTTP_HOST'].'/auth/forgot_password">click here</a>.';
}
else
{
$changes = array(
'password' => md5($this->input->post('new_password_1'))
);
if ($this->auth_model->alter_user($this->user_info['user_id'], $changes)) //querys the database with UPDATE `user_info` SET `password` = md5(their new password) WHERE `user_id` = 'whatever their user id is'
{
$this->data['alert_type'] = 'success';
$this->data['alert_message'] = 'Password successfully changed. Write it down and put it in a safe place.';
}
else
{
$this->data['alert_type'] = 'problem';
$this->data['alert_message'] = 'Everything you entered was fine but there was a problem updating your account. This is unusual. Please send an email to **staff email address omitted** and explain what happened. so it can be looked into.';
}
To my mind, you probably need a User model class that would include methods for all your associated "crud" activities: create, read, update, and delete -- keeping all your direct interaction with the database there. Whether or not you then just have one User controller or separate controllers, (e.g. maybe User_Registration, User_Profile, and User_Auth) is probably not as cut-and-dried. Whichever way you go, if you find individual methods getting too long, look for both complex logic and/or duplicate logic that can be moved into separate methods (most likely private methods available only to that controller (or children)).
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Well, I have an auth_model class. That's where the get_user_info() and alter_user() methods were coming from. Here's what's in them:
auth_model::get_user_Info()
public function get_user_info($user_id, $fields)
{
$this->db->where('user_id', $user_id);
$this->db->select($fields);
$query = $this->db->get('user_accounts');
if ($query->num_rows() == 1)
{
if (is_array($fields)) implode(',', $fields);
if (str_replace(',', '', $fields) == $fields) // return a string if just one item was pulled
{
$user_info = $query->row_array();
return $user_info[$fields];
}
else //return an array if they pulled more than one item
{
return $query->row_array();
}
}
else
{
return FALSE;
}
}
auth_model::alter_user()
public function alter_user($user_id, $changes)
{
$this->db->where('user_id', $user_id);
$this->db->update('user_accounts', $changes);
I want to use PHP to layer transparent PNG images on top of each other, and preserve their anti-aliasing. I need help writing the code that does this. The reason I want to do this with PHP and not CSS is complicated and I'm not going to bother to explain unless you ask. Anyway, I threw together a crude example:
I found some code on-line that sort of does this, albeit with some problems:
PHP Code:
/**
* Compose a PNG file over a src file.
* If new width/ height are defined, then resize the PNG (and keep all the transparency info)
* Author: Alex Le - http://www.alexle.net
*/
function imageComposeAlpha( &$src, &$ovr, $ovr_x, $ovr_y, $ovr_w = false, $ovr_h = false)
{
if( $ovr_w && $ovr_h )
$ovr = imageResizeAlpha( $ovr, $ovr_w, $ovr_h );
/**
* Resize a PNG file with transparency to given dimensions
* and still retain the alpha channel information
* Author: Alex Le - http://www.alexle.net
*/
function imageResizeAlpha(&$src, $w, $h)
{
/* create a new image with the new width and height */
$temp = imagecreatetruecolor($w, $h);
/* making the new image transparent */
$background = imagecolorallocate($temp, 0, 0, 0);
ImageColorTransparent($temp, $background); // make the new temp image all transparent
imagealphablending($temp, false); // turn off the alpha blending to keep the alpha channel
/* Resize the PNG file */
/* use imagecopyresized to gain some performance but loose some quality */
imagecopyresized($temp, $src, 0, 0, 0, 0, $w, $h, imagesx($src), imagesy($src));
/* use imagecopyresampled if you concern more about the quality */
//imagecopyresampled($temp, $src, 0, 0, 0, 0, $w, $h, imagesx($src), imagesy($src));
return $temp;
}
header('Content-type: image/png');
/* Open the photo and the overlay image */
$photoImage = ImageCreateFromJPEG('images/MiuMiu.jpg');
$overlay = ImageCreateFromPNG('images/hair-trans.png');
/* Compose the overlay photo over the target image */
imageComposeAlpha( $photoImage, $overlay, 86, 15, $newW, $newH );
/* Open another PNG file, then resize and compose it */
$watermark = imagecreatefrompng('images/watermark.png');
imageComposeAlpha( $photoImage, $watermark, 10, 20, imagesx($watermark)/2, imagesy($watermark)/2 );
/**
* Open the same PNG file then compose without resizing
* As the original $watermark is passed by reference, it was resized already.
* So we have to reopen it.
*/
$watermark = imagecreatefrompng('images/watermark.png');
imageComposeAlpha( $photoImage, $watermark, 80, 350);
Imagepng($photoImage); // output to browser
The problems with this code are:
-The anti-aliasing is not completely preserved. The edges aren't as soft.
-It requires the base image to be a .jpg. If I change it to a ImageCreateFromPNG() function and put a .png in there, the anti-aliasing goes away entirely and I'm left with jagged edges.
-It only layers two images. I'm going to need to be able to layer as many as 6 or so.
The better I get at programming, the more I appreciate arrays. Handy dandy things they are.
My first thought is to take a look at the ImageMagick extension to see if it performs any better for you. (Downside is that it's not always installed by default on many web servers.)
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
Looks okay to me with the exception that you would need to pass $word to the parent method when you call it (which, by the way, such a call is not required -- it's correct if you want to duplicate the entire functionality of the parent method at that point, but if not, then you would leave it out and just write entirely new functionality to override it).
Last edited by NogDog; 10-20-2012 at 11:46 AM.
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
#1 I want to make a page on my website that shows a list of all the users that have been active in the last 5 minutes. My dates are stored in time()'s format and they are in an int column that is called "last_active". PHP sends a query that updates this column every time the user visits a page.
The main thing stumping me is I'm not sure what to put in the "WHERE" part of my MySQL query, nor do I know how to get PHP to generate anything that it might need to generate.
#2 I also want to make a user's profile page say "Online" if they have been active in the last 5 minutes. Is there anything different I need to do?
The better I get at programming, the more I appreciate arrays. Handy dandy things they are.
Bookmarks