简体   繁体   中英

How to extend session time out in code igniter?

In my config.php , the session related config in my config file are as follows;

$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 1800; //30 minutes
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = TRUE;
$config['sess_table_name']      = 'rd_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;

For normal usage of the site the session expires after half an hour of login. However I have a feature in my website where user can play timer based tasks. For example when user clicks a button, the timer is started, he does certain task after some times, when he is done he clicks stop button and the timer is stopped and time is saved in the database.

The problem here is the task might lats more than 30 minutes, so session gets expire when user is playing timer. I can't change the sess expiration time in config because for other users (who is not playing timer) it should timeout after 30 minutes. So as suggested in other thread , when user starts timer, I make an ajax call to change the sess expiration time dynamically. Here is the method, that is called -

 public function sessiontimeout($time){
    $session_lifetime = $time > 0  ? $time: 864000;
    $this->config->set_item('sess_expiration', $session_lifetime);
    echo "Session lifetime set to ".$this->config->item("sess_expiration")." seconds";
    exit;
}

However this didn't work, user gets logged out in 30 minutes. how can I achieve this when timer is clicked the ajax call is made to extend the existing timeout and when timer is stopped, the session expiration time is reset to 30 minutes.

Thanks

Hi you don't need to change config file . You just run a ajax after a timeinterval to your any after logged controller function.
Means we are hitting the after login url through ajax request. codeigniter auto update current login user session. Thats simple

The only thing that worked is, i made an ajax call every 2 minutes and I checked if the session is going to get expired. And i reset the session 5 minutes before it gets expire. Code

Javascript ajax call.

var sessionTimer = setInterval(function(){
  $.ajax({
            url: '/ajax/sessiontimeout',
            beforeSend: function(){},
            success: function(data){
                console.info(data);
            }
        });
},120000);

And Controller method

public function sessiontimeout(){

        $lastActivity = $this->session->userdata('last_activity');
        $configtimeout = $this->config->item("sess_expiration");
        $sessonExpireson = $lastActivity+$configtimeout;


        $threshold = $sessonExpireson - 300; //five minutes before session time out

        $current_time = time();

        if($current_time>=$threshold){
            $this->session->set_userdata('last_activity', time()); //THIS LINE DOES THE TRICK
            echo "Session Re-registered";
        }else{
            echo "Not yet time to re-register";
        }

        exit;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM