Zend Framework 2 会话生命周期


Zend Framework 2 session life time

我正在尝试设置与'Zend'Session'Container会话的最长生命周期。为了测试它,我把它放在 1 秒。

现在我看了文档

所以我做到了

$config = new StandardConfig();
$config->setOptions(array(
    'remember_me_seconds' => 1,
));
$manager = new SessionManager($config);
$session = new Container('user', $manager);

但没有成功。然后我开始谷歌搜索并找到了这个答案

所以我做了配置

return array(
    'session' => array(
        'remember_me_seconds' => 2419200,
        'use_cookies' => true,
        'cookie_httponly' => true,
    ),
);

(配置有效并加载到管理器中)但同样没有成功

所以我继续寻找并找到了这个答案

但又没有成功。

所以在所有搜索之后,我无法让它工作,所以现在我希望其他人让它工作并可以帮助我。

好吧,

我终于发现了问题所在。

问题是我用了

$sessionConfig = new SessionConfig();
$sessionConfig->setOptions(array(
    'use_cookies' => true,
    'cookie_httponly' => true,
    'gc_maxlifetime' => $config['authTimeout'],
));
$manager = new SessionManager($sessionConfig);

这"有效"唯一的问题是设置了一个具有生命周期session的cookie。这在浏览器中有所不同。即在铬中,如果您关闭选项卡,它就会被破坏,因此无论gc_maxlifetime有多高,它都不起作用。

因此,一个简单的解决方法如下

$sessionConfig = new SessionConfig();
$sessionConfig->setOptions(array(
    'use_cookies' => true,
    'cookie_httponly' => true,
    'gc_maxlifetime' => $config['authTimeout'],
    'cookie_lifetime' => $config['authTimeout'],
));
$manager = new SessionManager($sessionConfig);

希望它能帮助未来的某个人

$config['authTimeout']为正整数值

$config = new StandardConfig();
$config->setOptions(array(
    'cookie_lifetime' => '2419200',
    'gc_maxlifetime' => '2419200'
));

2419200 的值更改为实际需要的秒数。

在应用程序全局中.php或者您可以在模块配置中执行此操作:

 'session_config' => array(
    'name' => 'your session name',
    'remember_me_seconds' => 60 * 60 * 24 * 30*3,
    'use_cookies' => true,
    'cookie_httponly' => true,
),

选中 Zend''Session''Service''SessionConfig。