PHP Codeigniter中使用ajax轮询的会话超时

时间:2016-06-13 22:43:29

标签: php ajax codeigniter session

我有一个带有AJAX请求的计数器刷新服务器端。 我每分钟运行一次这个计时器。

我的会话超时必须是2小时,因此这是我的config.php文件:

$config['sess_driver']              = 'files';
$config['sess_save_path']           = '/var/lib/php5/sessions';
$config['sess_cookie_name']         = 'ci_session';
$config['sess_expiration']          = 60*60*2;
$config['sess_match_ip']            = FALSE;
$config['sess_table_name']          = 'ci_sessions';
$config['sess_time_to_update']      = 60*5;
$config['sess_regenerate_destroy']  = FALSE;

计数器更新程序阻止我的会话过期。如何防止控制器操作更新会话超时?

[编辑]

这是尝试扩展Session Class。这个尝试看起来不错,但我等待测试审查。随时阅读和评论;-):

if (
    ($regenerate_time = config_item('sess_time_to_update')) > 0
)
{
    if ( ! isset($_SESSION['__ci_last_regenerate']))
    {
        $_SESSION['__ci_last_regenerate'] = time();
    }
    elseif ($_SESSION['__ci_last_regenerate'] < (time() - $params['expiration']))
    {
        $this->sess_destroy();
    }
    elseif ($_SESSION['__ci_last_regenerate'] < (time() - $regenerate_time))
    {
        if (get_instance()->uri->segment(2) != "refreshTaskCount") {
            $this->sess_regenerate((bool)config_item('sess_regenerate_destroy'));
        }
    }
}

// Another work-around ... PHP doesn't seem to send the session cookie
// unless it is being currently created or regenerated
if (isset($_COOKIE[$this->_config['cookie_name']]) && $_COOKIE[$this->_config['cookie_name']] === session_id())
{
    if (get_instance()->uri->segment(2) != "refreshTaskCount") {
        setcookie(
            $this->_config['cookie_name'],
            session_id(),
            (empty($this->_config['cookie_lifetime']) ? 0 : time() + $this->_config['cookie_lifetime']),
            $this->_config['cookie_path'],
            $this->_config['cookie_domain'],
            $this->_config['cookie_secure'],
            TRUE
        );
    }
}

0 个答案:

没有答案