[eluser]AnimeCYC[/eluser]
Hello,
Having trouble with CI cessions, for some reason it creates a new session on every page load and doesn't update the previous one; thus making it impossible to retrieve set data with either the set_userdata or set_flashdata methods and retrieve it. Does anyone have a clue as to why this is happening?
UPDATE ::
Ok here is the fix, instead of setting the expire on the cookie it is much easier and better to set the max age, this should work across all browsers (yes even IE). Let me know if there are any issues.
UPDATE 2 ::
Updated to fix an issue when the sessions gets re-written.
Code:
<?php
class MY_Session extends CI_Session {
function sess_write() {
if ($this->sess_use_database === FALSE) {
$this->_set_cookie();
return;
}
$custom_userdata = $this->userdata;
$cookie_userdata = array();
foreach(array('session_id','ip_address','user_agent','last_activity') AS $val) {
unset($custom_userdata[$val]);
$cookie_userdata[$val] = $this->userdata[$val];
}
if(count($custom_userdata) === 0) {
$custom_userdata = '';
} else {
$custom_userdata = $this->_serialize($custom_userdata);
}
$this->CI->db->where('session_id', $this->userdata['session_id']);
$this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata));
$this->_set_cookie($cookie_userdata);
}
function _set_cookie($cookie_data = NULL) {
if(is_null($cookie_data)) {
$cookie_data = $this->userdata;
}
$cookie_data = $this->_serialize($cookie_data);
if ($this->sess_encrypt_cookie == TRUE) {
$cookie_data = $this->CI->encrypt->encode($cookie_data);
} else {
$cookie_data = $cookie_data . md5($cookie_data.$this->encryption_key);
}
$this->_set_cookie_age($this->sess_cookie_name, $cookie_data, $this->sess_expiration, $this->cookie_path, $this->cookie_domain);
}
function _set_cookie_age($cookieName, $data = '', $age = 0, $path = '', $domain = '', $secure = FALSE, $http = FALSE) {
header('Set-Cookie: ' . rawurlencode($cookieName) . '=' . rawurlencode($data)
. ($age < 1 ? '' : '; Max-Age=' . $age)
. (empty($path) ? '' : '; path=' . $path)
. (empty($domain) ? '' : '; domain=' . $domain)
. (!$secure ? '' : '; secure')
. (!$http ? '' : '; HttpOnly'), FALSE);
}
}