Zend Framework 2 会话生命周期 [英] Zend Framework 2 session life time
问题描述
我正在尝试使用 \Zend\Session\Container
设置会话的最大生命周期.为了测试它,我将其设置为 1 秒.
I am trying to set the max life time of a session with the \Zend\Session\Container
. To test it I put it to 1 sec.
现在我查看了文档
我也是这样
$config = new StandardConfig();
$config->setOptions(array(
'remember_me_seconds' => 1,
));
$manager = new SessionManager($config);
$session = new Container('user', $manager);
但没有成功.然后我开始谷歌搜索并找到了这个答案
But no success. Then I started googling and found this answer
所以我做了配置
return array(
'session' => array(
'remember_me_seconds' => 2419200,
'use_cookies' => true,
'cookie_httponly' => true,
),
);
(配置工作并加载到管理器中)但再次没有成功
(the config worked and was loaded into the manager) but again no success
所以我继续搜索并找到了这个答案
So I continued searching and found this answer
但还是没有成功.
所以在所有搜索之后我都无法让它工作,所以现在我希望其他人能够让它工作并且可以帮助我.
So after all the searching I couldn't get it working, so now I hope some one else got it working and can help me.
推荐答案
我终于找到了问题所在.
Well I finaly found out what the issue was.
问题是我用过
$sessionConfig = new SessionConfig();
$sessionConfig->setOptions(array(
'use_cookies' => true,
'cookie_httponly' => true,
'gc_maxlifetime' => $config['authTimeout'],
));
$manager = new SessionManager($sessionConfig);
这个有效"的唯一问题是设置了一个生命周期为 session
的 cookie.这说明了浏览器中的不同内容.即在 chrome 中,如果您关闭选项卡,它会被破坏,因此无论 gc_maxlifetime
有多高,它都不起作用.
This "worked" the only issue was that there was set a cookie with the lifetime session
. This ment different things in browsers. Ie in chrome it is destroyed if you close the tab, so no matter how high the gc_maxlifetime
it would not work.
所以一个简单的解决方法是以下
So an easy fix would be the following
$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['authTimeout']
is a positive integer value
这篇关于Zend Framework 2 会话生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!