在 Symfony2 中以编程方式设置会话生存期 [英] Programmatically Set Session Lifetime in Symfony2

查看:30
本文介绍了在 Symfony2 中以编程方式设置会话生存期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在网上研究,但找不到答案.在 Symfony2 中,我知道可以通过 config.yml 文件静态设置会话生存期.

I have been researching online, but could not find the answer. In Symfony2, I understand that one can statically set the session lifetime through the config.yml file.

但是,我需要能够根据代码中的某些情况设置会话的生命周期.是否可以以编程方式设置会话生存期?

However, I need to be able to set the lifetime of the session based on certain situations in my code. Is it possible to programmatically set the session lifetime?

推荐答案

在 Symfony2 中,您似乎无法动态更改会话存储容器的属性:请参阅 https://github.com/symfony/HttpFoundation/blob/master/SessionStorage/SessionStorageInterface.php 为实际可用的实现接口,请注意其中没有任何内容可以让您更改生命周期值.

It appears that in Symfony2 you can't change the attributes of the session storage container on the fly: see https://github.com/symfony/HttpFoundation/blob/master/SessionStorage/SessionStorageInterface.php for the actual implementation interface that is made available, and notice there is nothing in there that lets you change the lifetime value.

但是,会话类默认使用 session_get_cookie_params 方法来设置会话生命周期:您可以通过调用 session_set_cookie_params (最好在会话初始化之前:尽量在控制器中尽早调用它).看看这是否适合您.

However, the session classes by default use the session_get_cookie_params method to set the session lifetime: you can adjust those values by calling session_set_cookie_params (preferably before the session is initialized: try to call it as early as possible in the controller). See if that works for you.

这篇关于在 Symfony2 中以编程方式设置会话生存期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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