Codeigniter Setting Remember Me in Sessions

2019-04-16 06:44发布

问题:

I'm trying to implement remember me function to my login form. When user checks the checkbox or not var_dumping the session expiration data is returning 63072000. I couldn't be able to set that and i don't understand why?

How i var_dump:

var_dump($this->session->sess_expiration); // returning 63072000

form view:

<input name="rememberme" value="rememberme" type="checkbox" />

login model:

$rememberme = $this->input->post('rememberme');
if((!isset($rememberme)) || ($rememberme != "rememberme")){
$this->session->sess_expiration = 10800; // 3 hours
$this->session->sess_expire_on_close = TRUE;
}
$this->session->set_userdata($data);

session config:

$config['sess_cookie_name']     = 'cisession';
$config['sess_expiration'] = 0;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = TRUE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;

回答1:

According to the forums at EllisLabs, the session data is set from the config/config.php file when the Session Class is initially loaded (which I imagine you are either auto-loading or manually loading before this code in the model), and can't be changed afterwards.

Further down the thread however, it is suggested that you can change the session data. What you might try doing is forcing an update to the session data after setting the expiration configs, while still within the if statement:

$rememberme = $this->input->post('rememberme');

if( (!isset($rememberme)) || ($rememberme != "remember me") ) {
    $this->session->sess_expiration = 10800; // 3 hours
    $this->session->sess_expire_on_close = TRUE;

    $this->session->sess_update(); //Force an update to the session data
}