Zend\Session\SessionManager 和 cookie_lifetime [英] Zend\Session\SessionManager and cookie_lifetime

查看:66
本文介绍了Zend\Session\SessionManager 和 cookie_lifetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现 ZF2 会话和超时出现了一些奇怪且令人沮丧的行为.

I'm seeing some odd and frustrating behavior with ZF2 sessions and timeouts.

这是我用来设置会话的代码:

Here's the code I use to set up the session:

    $sessionConfig = new \Zend\Session\Config\StandardConfig();
    $sessionConfig->setOptions(array(
        'cache_expire' => 525949,
        'cookie_domain' => 'mydomain.com',
        'cookie_lifetime' => 31536000,
        'cookie_path' => '/',
        'cookie_secure' => TRUE,
        'gc_maxlifetime' => 31536000,
        'name' => 'mydomain',
        'remember_me_seconds' => 31536000,
        'use_cookies' => TRUE,
    ));

    $sessionManager = new \Zend\Session\SessionManager($sessionConfig);
    $sessionManager->rememberMe(31536000);
    $sessionManager->setSaveHandler(new \Zend\Session\SaveHandler\MongoDB($mongo, $options);
    $session = new \Zend\Session\Container('MY_SESSION', $sessionManager);

当我执行这段代码时,cookie 被创建,但过期是会话结束.

When I execute this code, the cookie gets created but the expiration is end of session.

如果我像这样更改代码:

If I change the code like this:

    $sessionManager = new \Zend\Session\SessionManager();
    $sessionManager->rememberMe(31536000);
    $sessionManager->setConfig($sessionConfig);
    $session = new \Zend\Session\Container('MY_SESSION', $sessionManager);

cookie 已创建,有效期为一年后.

the cookie gets created and the expiration is a year from now.

然而,即使 cookie 仍然存在,会话仍然会在 30 分钟左右过期.

However, the session still expires after 30 minutes or so, even though the cookie remains.

我想要的是 cookie 和 session 都持续一年.我如何在 ZF2 中完成此操作?

What I want is for both the cookie and session to persist for a year. How do I accomplish this in ZF2?

推荐答案

问题似乎与 gc_maxlifetime 选项的处理有关.在\Zend\Session\SaveHandler\MongoDB 中,该值取自 PHP 配置,通过 ini_get('session.gc_maxlifetime');

It looks like the issue has to do with the handling of the gc_maxlifetime option. In \Zend\Session\SaveHandler\MongoDB, this value is taken from the PHP configuration, via ini_get('session.gc_maxlifetime');

我在 \Zend\Session\SessionManager 中没有看到调用 ini_set() 的任何地方.

I don't see anywhere in \Zend\Session\SessionManager where ini_set() is being called.

我认为,解决方案是执行以下操作之一:

The solution, I think, is to do one of the following:

  1. 编辑php.ini并全局设置值
  2. 编辑 .htaccess 并添加 php_value session.gc_maxlifetime
  3. 如果提供了 gc_maxlifetime 选项,则扩展 \Zend\Session\SessionManager 并添加一个调用 ini_set() 的新方法.

这篇关于Zend\Session\SessionManager 和 cookie_lifetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆