简体   繁体   English

如何在代码点火器中延长会话超时?

[英]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.php ,配置文件中与会话相关的配置如下:

$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. 这里的问题是任务可能会延迟30分钟以上,因此当用户播放计时器时会话会过期。 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. 我无法在config中更改sess到期时间,因为对于其他用户(未使用计时器的用户),它应在30分钟后超时。 So as suggested in other thread , when user starts timer, I make an ajax call to change the sess expiration time dynamically. 因此,正如其他线程中所建议的那样,当用户启动计时器时,我进行了ajax调用来动态更改sess到期时间。 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. 但是,此操作无效,用户将在30分钟后退出。 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. 当单击计时器时,如何实现Ajax调用以延长现有的超时,而当计时器停止时,会话过期时间重置为30分钟,我该如何实现这一点。

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. 您只需在间隔时间过后再运行ajax,然后再登录控制器功能即可。
Means we are hitting the after login url through ajax request. 意味着我们正在通过ajax请求访问登录后URL。 codeigniter auto update current login user session. codeigniter自动更新当前登录用户会话。 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. 唯一有效的方法是,我每2分钟拨打一次ajax电话,然后检查会话是否即将过期。 And i reset the session 5 minutes before it gets expire. 我在会话过期前5分钟重置了会话。 Code

Javascript ajax call. Javascript Ajax调用。

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;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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