[eluser]ShoeLace1291[/eluser]
I'm having a bit of trouble with a function that I use to create sessions. Everything in the function runs fine until it hits the session set userdata function. I have no idea why this isn't working. This is my function:
Code: function login($email){
$CI =& get_instance();
$CI->load->library('session');
$query = $CI->db->query("
SELECT id, email, last_active, display_name
FROM members
WHERE email = '".$email."'
LIMIT 1");
if($query->num_rows() == 0){
$this->errors = $this->errors + 1;
$this->error_messages[] = 'The email address was not found in our database.';
return FALSE;
} else {
$member = $query->row_array();
$session = array( //session data
'id' => $member['id'],
'last_active' => time(),
'display_name' => $member['display_name']
);
if(!$CI->session->set_userdata($session)){
$this->errors = $this->errors + 1;
$this->error_messages[] = 'The session class failed to update your session with the appropriate data.';
return FALSE;
} else if(!$CI->db->update('members', array('last_active' => time()), array('id' => $member['id']))){
$this->errors = $this->errors + 1;
$this->error_messages[] = 'The database failed to update the active record with the given session data.';
return FALSE;
} else {
return TRUE;
}
}
}
The error that returns is the session line 'The session class failed...'
Any ideas?
[eluser]WanWizard[/eluser]
Why are you testing the result of set_userdata()? It doesn't return anything, so there's nothing to test.
[eluser]ShoeLace1291[/eluser]
Oh, really? It doesn't set the userdata at all so I thought that would tell me why...
[eluser]InsiteFX[/eluser]
And what doe's this tell you?
The error that returns is the session line ‘The session class failed…’
I'll bet it's because your $session array is empty!
InsiteFX
[eluser]WanWizard[/eluser]
The statement
Code: if(!$CI->session->set_userdata($session)){
evaluates to
because the set_userdata() doesn't return anything. ! null evaluates to true, and therefore that section is always executed. Your code is wrong.
[eluser]ShoeLace1291[/eluser]
Ok, I changed the if statement to
Code: $CI->session->set_userdata($session);
But it still doesn't set any session data...
[eluser]InsiteFX[/eluser]
The Session library uses $session internally not sure, but this might be conflicting with the internal Session $session variable! When it is passed into the Session library.
Try changing the name $session to something else!
InsiteFX
[eluser]Twisted1919[/eluser]
[quote author="InsiteFX" date="1303157153"]The Session library uses $session internally not sure, but this might be conflicting with the internal Session $session variable! When it is passed into the Session library.
Try changing the name $session to something else!
InsiteFX[/quote]
Oh boy ...
[eluser]WanWizard[/eluser]
I think the next step is to show us your session and cookie configuration.
And the code that checks the session data. Since you're explicitly loading the session library here, are you sure it's loaded when you need it? If your application needs sessions, autoload the session library, as you're going to need it for every page request anyway.
[eluser]ShoeLace1291[/eluser]
Ok, instead of using an array to set the session data, I'm just doing it one at a time, and it still isn't working... here is my revised code of the member library, which includes the login that sets the session data as well as a function that gets the user's data all at once based on the session data that's already set.
Code: function login($email){
$CI =& get_instance();
$CI->load->library('session');
$CI->load->database();
$query = $CI->db->query("
SELECT id, email, display_name, last_active
FROM members
WHERE email = '".$email."'");
if($query->num_rows() == 0){
$this->error_messages[] = 'The member was not found in the database.';
$this->errors = $this->errors + 1;
return FALSE;
} else {
$member = $query->row_array();
$CI->session->set_userdata('id', $member['id']);
$CI->session->set_userdata('display_name', $member['display_name']);
$CI->session->set_userdata('last_active', now());
if(!$CI->db->update('members', array('last_active' => now()), array('id' => $member['id']))){
$this->error_messages[] = 'The system failed to update the database with the necessary information.';
$this->errors = $this->errors + 1;
return FALSE;
} else {
return TRUE;
}
}
}
function user_context(){
$CI =& get_instance();
$array = array(
'id' => 0,
'display_name' => NULL,
'email' => NULL,
'join_date' => NULL,
'perm_level' => NULL,
'last_active_date' => NULL,
'last_active_span' => NULL,
'last_active_stamp' => NULL,
'points' => 0,
'photo' => 0
);
if($CI->session->userdata('id')){
$query = $CI->db->query("
SELECT *
FROM members
WHERE id = ".$CI->session->userdata('id')."
LIMIT 1");
if($query->num_rows() > 0){
$member = $query->row_array();
$query2 = $CI->db->query("
SELECT *
FROM groups
WHERE id = ".$member['group_id']."
LIMIT 1");
$group = $query2->row_array();
$array = array(
'id' => $member['id'],
'display_name' => $member['display_name'],
'email' => $member['email'],
'join_date' => date('M js, Y', $member['join_date']),
'perm_level' => $group['perm_level'],
'last_active_date' => date('M js, Y', $member['last_active']),
'last_active_span' => timepsan($member['last_active'], now()),
'last_active_stamp' => $member['last_active'],
'points' => $member['points'],
'photo' => ($member['photo']) ? $member['photo'] : 'images/nophoto.jpg'
);
}
}
return $array;
}
This is my session config
Code: $config['encryption_key'] = 'lensereflex';
$config['sess_cookie_name'] = 'lensereflex.com';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = FALSE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
|