繁体   English   中英

如何在 Laravel 中设置会话超时?

[英]How to set session timeout in Laravel?

是否有设置会话在特定时间后过期的固有方法。 我当前的设置似乎在 30 分钟后到期,我想禁用它或至少增加它,但我在 Laravel 中找不到任何可以设置它的地方?

app/config/session.php你有:

lifetime

允许您以分钟为单位(而不是以秒为单位)设置会话过期时间的选项

'lifetime' => 60,

意味着会话将在一个小时后到期。

这里还有一个设置:

'expire_on_close' => true,

这决定了当浏览器关闭时会话是否会过期。

您可能感兴趣的其他设置也是php.ini值:

session.cookie_lifetime = 0

session.gc_maxlifetime = 1440

这些是默认值。

第一个表示会话 cookie 将存储多长时间 - 默认值为 0(直到浏览关闭)。 第二个选项表示 PHP 可能会在多少后销毁此会话数据。

我说可能是因为php.ini文件中还有另一个选项session.gc_probability决定了运行垃圾收集器的机会。 默认情况下,只有 1% 的机会在 1440 秒(24 分钟)后此会话数据将被销毁。

检查您的php.ini ,它具有session.gc_maxlifetime (以及session.cookie_lifetime )的值,该值设置了 PHP 允许会话持续多长时间的限制。 当 Laravel 设置选项时,它会将cookie_lifetime作为在app/config/session.php设置的值传递。

但是,会话不会在达到最大生命周期后立即过期。 发生的事情是在这段时间过去之后,垃圾收集器可以删除会话。

解决问题

一种解决方法是检查您的php.ini文件。 您可能定义了这个变量: session.gc_maxlifetime 默认设置为 1440。只需评论或删除它

从此时起,您的会话可能会使用您的 session.php 配置值正常工作。

从 Laravel 4.1 开始,原生 PHP 会话支持被取消

要配置会话生命周期编辑app/config/session.php并设置以下内容:

/* if this is set to 'native' it will use file.
   if this is set to 'array' sessions will not persist across requests
   effectively expiring them immediately.
*/ 
'driver' => 'file'

/* number of minutes after which the session is available for Laravel's
   built in garbage collection.
   Prior to 4.1 you could set this to zero to expire sessions when
   the browser closes. See the next option below.
*/
'lifetime' => 60

/* If true sessions will expire when the user closes the browser.
   This effectively ignores your lifetime setting above.
   Set to false if you want Laravel to respect the lifetime value.
   If your config file was written before 4.1 you need to add this.
*/
'expire_on_close' => false,

参考:

在命令行运行artisan changes 4.1.*以查看有关native驱动程序等效于file的注释

$ artisan changes 4.1.* | grep -i native
-> Native session driver has been replaced by 'file'. Specifying 'native' driver will just use the new file driver.

App\\Config\\Session.php

检查终身...

你也可以设置...

Cookie::make('name', 'value', 60); // 1 hr

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM