开发者

Help w/ Codeigniter session expiration time

I would like to dynamically set the session expiration time in Codeigniter. I'm autoload the session class. I have a view that contains checkbox for users to click (remember me). Right now if they click the check box or not the expiration time开发者_高级运维 stays the same :/


    // Config.php
    $config['sess_expiration']      = 7200;

    // Controller
    if ($this->input->post('remember_me') == 'TRUE')
    {
    $this->session->remember_me();
    }

    $newdata = array(
    'failed_login'  => 0,
    'user_name'  => $this->input->post('user_name'),
        'logged_in' => TRUE
    );

    $this->session->set_userdata($newdata);

    // MY_Session.php
    class MY_Session extends CI_Session {

    function remember_me()
    {
        $this->sess_expiration = 172800;
    }

    }


If you need to implement "Remember me" feature - then you've started it in a wrong way.

You need to create one more database table with fields user_id | token.

Then, after user has been logged in (with "remember me" checkbox checked on) - generate random token and insert a new row with current user_id and that token. Also - set remember cookie with the same token value.

Now, if user enters your site, not authenticated and has some token - you can always find that token and authenticate user (each token is unique and strognly related to specific user_id).


Here is my solution I've been playing with.

You can edit the function inside domain.com/system/libraries/Session.php

function _set_cookie($cookie_data = NULL)

Comment out

// $expire = ($this->sess_expire_on_close === TRUE) ? 0 : $this->sess_expiration + time();

Then Add

    if(isset($this->userdata['rememberme'])){
        // If they want to stay connected
        if( $this->userdata['rememberme'] == 1) {
            $expire = 0;
        } else {
            $expire = time() + $this->sess_expiration;
        }
    } else {
        $expire = time() + $this->sess_expiration;
    }

Please let me know if this helps in anyway or modify my code to help me out. Thank you.


$data = array(
    'username' => $this->input->post('username'),
    'ADMIN_is_logged_in' => true
);

$this->session->sess_expiration = '14400';// expires in 4 hours
$this->session->set_userdata($data);// set session
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜