I have just started working on a project using codeigniter 3 and having issues with session data being stored in a custom library class.
It worked on previous version of CI but not on 3.
This is the login:
function login($username = '', $password = '')
{
$this->CI =& get_instance();
if($username == '' OR $password == '')
return false;
//Check if already logged in
if($this->CI->session->userdata('user_email') == $username)
return true;
//Check against user table
$this->CI->db->where('user_email', $username);
$query = $this->CI->db->get_where($this->user_table);
if ($query->num_rows() > 0)
{
$user_data = $query->row_array();
$hasher = new PasswordHash(PHPASS_HASH_STRENGTH, PHPASS_HASH_PORTABLE);
if(!$hasher->CheckPassword($password, $user_data['user_pass']))
return false;
//Destroy old session
$this->CI->session->sess_destroy();
//Create a fresh, brand new session
$this->CI->session->sess_regenerate(TRUE);
$this->CI->db->simple_query('UPDATE ' . $this->user_table . ' SET user_last_login = "' . date('c') . '" WHERE user_id = ' . $user_data['user_id']);
//Set session data
unset($user_data['user_pass']);
$user_data['user'] = $user_data['user_email']; // for compatibility with Simplelogin
$user_data['logged_in'] = TRUE;
$this->CI->session->set_userdata($user_data);
return true;
}
else
{
return false;
}
}
I have tested adding session userdata in my controller and it works so not sure why this is not working.
The session table is using the latest structure for CI3 and userdata is stored as BLOB item.
Any help would be appreciated.
Thanks