如何在CakePHP中配置会话生存期仅为24小时



我的应用程序使用CakePHP v4。

我要做的是设置会话在24小时后到期,而不处于非活动状态。

一个会话应该持续24小时,无论是否有几个小时的不活动。

这是我在bootstrap.php文件中的配置。

Configure::write('Session', [
'defaults' => 'php',
// 'timeout' => 1440,
'cookieTimeout' => 1440, // The session cookie will live for at most 24 hours, this does not effect session timeouts
'checkAgent' => false,
'autoRegenerate' => false, // causes the session expiration time to reset on each page load
]);

我应该将autoRegenerate设置为false,对吗?

timeout可以省略,对吧?

会话应在24小时后过期,即使处于非活动状态。

所以请让我知道什么是正确的配置。

目前,如果浏览器关闭,会话将过期。

如果用户关闭浏览器,如何防止会话过期?

提前谢谢。

更新:我正在使用会话来保留我的令牌。

$accessToken = $this->getRequest()->getSession()->read('accessToken');

即使我关闭了浏览器,我也希望accessToken保持24小时。

默认情况下,PHP将会话cookie设置为在浏览器关闭后立即过期,而不考虑配置的session.timeout值。cookie超时由session.cokie_lifetime.ini值控制,可以使用以下命令进行配置:

Configure::write('Session', [
'defaults' => 'php',
'ini' => [
// Invalidate the cookie after 30 minutes without visiting
// any page on the site.
'session.cookie_lifetime' => 1800
]
]);

请参阅:https://book.cakephp.org/4/en/development/sessions.html

最新更新