我在使用Symfony2的session组件时遇到了问题。我通过session容器将一些数据设置到会话中,就像这样:
$sess = $this->get( 'session' );
$sess->set( 'some_key', 'some_value' );
但是过了一段时间(大约15-20分钟),会话就丢失了。
我可以设置会话生存时间参数吗?最好的情况是我可以设置特定时间的会话存活期...请问有人能帮忙吗?
framework
部分的配置文件中设置会话过期时间。我的配置文件如下所示:config.yml
framework:
secret: %secret%
charset: UTF-8
error_handler: null
csrf_protection:
enabled: true
router: { resource: "%kernel.root_dir%/config/routing.yml" }
validation: { enabled: true, annotations: true }
templating: { engines: ['twig'] } #assets_version: SomeVersionScheme
session:
default_locale: %locale%
cookie_lifetime: 3600 // was "lifetime" but deprecated
auto_start: true
您可以更改framework.session.lifetime
的值,以秒为单位(默认为3600或1小时)。
参考此处。
在Symfony 2.3中,我认为正确的答案可以在app / config / config.yml中找到:
framework:
session:
cookie_lifetime: 7200
gc_maxlifetime: 3600
每次服务器被访问时,都会重置GC(垃圾回收),这意味着如果用户活跃,他将有3600秒来继续工作。 cookie_lifetime
将在其限制内强制用户注销。在此示例中,用户将有一小时的不活动时间,并将在2小时后被强制退出。
cookie_lifetime
设置为0,这意味着当浏览器关闭时,cookie会过期。 framework:
session:
cookie_lifetime: 0
NativeSessionStorage
中,我发现默认值来自于 PHP 的session_get_cookie_params()
,因此默认的 PHP 会话 cookie 生命周期为 0(当浏览器关闭时过期)。此外,需要注意的是,即使更改了 PHP 会话 cookie 生命周期,我认为您还必须更改 PHP 设置中的session.gc_maxlifetime
(因为默认值为 1440 秒 / 24 分钟)。请参见 http://tinyurl.com/6txls7n。 - Adam Monsensession_get_cookie_params()
将覆盖传递的值。 - Problematic0
。哦,糟糕,打错字了。它也是86,400!我会发送一个补丁,看起来有人搞砸了。 - Adam Monsenlifetime
已被弃用为cookie_lifetime
... 看起来文档在这里已经过时了。 - Problematic