如何增加 Symfony2 中的会话寿命? [英] How to increase the session lifetime in Symfony2?

查看:47
本文介绍了如何增加 Symfony2 中的会话寿命?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有记住复选框的登录名.如果选中了记住复选框,我正在尝试将会话的生命周期设置为 1 年,但无论我尝试什么,它都不起作用......Symfony 分析器总是说会话生命周期为 0.

I have a login with a remember checkbox. I'm tring to set lifetime of session to 1 year if the remember checkbox is checked, but no matter what I tried, it doesn't work... Symfony profiler always say that session lifetime is 0.

这就是我在控制器功能中尝试的:

That's what I tryed in my controller function:

$this->getRequest()->getSession()->set("gesaudit",array("login"=>true,"user"=>$user));

if($remember == "1"){
    $lifetime = new NativeSessionStorage();
    $lifetime->setOptions(array('cookie_lifetime' => 31557600));
}else{
    $lifetime = new NativeSessionStorage();
    $lifetime->setOptions(array('cookie_lifetime' => 0));
}

推荐答案

可以根据 文档,通过 config.yml:

Normal session lifetime can be managed, according to the documentation, via config.yml:

framework:
    session:
        cookie_lifetime: 3600

要将生命周期设置为一年:3600*24*365 = 86400*365 = 31536000.简单:).

To set the lifetime to a year: 3600*24*365 = 86400*365 = 31536000. Easy :).

这会将会话 cookie 生存期设置为 1 小时.您可以配置大量其他内容,但这就是我包含文档链接的原因.如果您坚持在特定情况/控制器中手动执行此操作,则将选项数组传递给控制器​​.也许这可以代替使用 setOptions 方法:如果您的 php.ini 包含:

This sets the session cookie lifetime to 1 hour. There are tons of other things you can configure, but that's why I included a link to the documentation.
If you insist on doing this manually, in a specific case/controller, then pass the options array to the controller. Perhaps that'll work instead of using the setOptions method: If your php.ini contains:

session.auto_start = 1

然后创建 NativeSessionStorage 实例将自动并立即创建会话,具有默认生命周期.之后设置它会产生很少或没有区别.检查您的 ini 设置,或执行以下操作:

Then creating the NativeSessionStorage instance will automatically, and immediately create the session, with the default lifetime. Setting it afterwards will make little or no difference. Check your ini settings, or do:

$test = new NativeSessionStorage();
var_dump($test->isStarted())

如果它转储 true,请尝试:

If it dumps true, try:

$lifetime = new NativeSessionStorage(
    array(
        'cookie_lifetime' => 31536000
    )
);

Symfony2 会话中的所有内容都可以在这里找到.

Everything on sessions in Symfony2 can be found here.

如果您使用的是 Symfony2.4,文档中有一个特殊部分涉及 记住-正如 Jakub Polák 指出的那样,我的功能.它的本质是复选框必须被称为 _remember_me,config.yml 必须定义一个 %secret% 值,并且你添加(一个定制的)将此版本添加到您的 security.yml 文件中:

If you are using Symfony2.4, there is a special section in the docs that deals with remember-me functionality, as Jakub Polák pointed out. The essence of it is that the checkbox has to be called _remember_me, that the config.yml has to define a %secret% value, and that you add (a tailored) version of this to your security.yml file:

firewalls:
    main:
        remember_me:
            key:      "%secret%"
            lifetime: 31536000
            path:     /
            domain:   ~ # Defaults to the current domain from $_SERVER

但是文档解释了这一切,但是您必须浏览"手册.例如,如果您想为特定部分指定不同的记住我的行为,请更改上面 yml 片段中的 main,并添加一个 pattern 设置,作为 在此处解释.
您可能最好扫描整个安全部分.

But the documentation explains this all, but you'll have to "cruise" the manual a bit. For example, if you want to specify different remember-me behaviour for specific sections, change the main in the yml snippet above, and add a pattern setting, as explained here.
You'd probably best scan the entire security section.

这篇关于如何增加 Symfony2 中的会话寿命?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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