简体   繁体   English

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

[英]How to set session timeout in Laravel?

Is there an inherent way to setup sessions to expire after a certain time.是否有设置会话在特定时间后过期的固有方法。 My current setup seems to be expiring after 30 minutes and I would like to disable that or at least increase it, but I can't find any places in Laravel where this could be set?我当前的设置似乎在 30 分钟后到期,我想禁用它或至少增加它,但我在 Laravel 中找不到任何可以设置它的地方?

In app/config/session.php you have:app/config/session.php你有:

lifetime

option that allow you to set session expire time in minutes (not in seconds)允许您以分钟为单位(而不是以秒为单位)设置会话过期时间的选项

'lifetime' => 60,

means that session will expire after an hour.意味着会话将在一个小时后到期。

There is also one more setting here:这里还有一个设置:

'expire_on_close' => true,

that decides if session will be expired when browser will be closed.这决定了当浏览器关闭时会话是否会过期。

Other settings you could get interested is also php.ini values of:您可能感兴趣的其他设置也是php.ini值:

session.cookie_lifetime = 0

and

session.gc_maxlifetime = 1440

Those are default values.这些是默认值。

The first one means how long session cookie will be stored - default value is 0 (until browse is closed).第一个表示会话 cookie 将存储多长时间 - 默认值为 0(直到浏览关闭)。 The second option means after how many of seconds PHP may destroy this session data.第二个选项表示 PHP 可能会在多少后销毁此会话数据。

I said may because there is one other option session.gc_probability in php.ini file that decides what's the chance of running garbage collector.我说可能是因为php.ini文件中还有另一个选项session.gc_probability决定了运行垃圾收集器的机会。 Be default there is only 1% chance that after 1440 seconds (24 minutes) this session data will be destroyed.默认情况下,只有 1% 的机会在 1440 秒(24 分钟)后此会话数据将被销毁。

Check your php.ini , it has a value for session.gc_maxlifetime (and also session.cookie_lifetime ) that sets a limit on how long PHP will allow sessions to last.检查您的php.ini ,它具有session.gc_maxlifetime (以及session.cookie_lifetime )的值,该值设置了 PHP 允许会话持续多长时间的限制。 When Laravel sets the options, it passes cookie_lifetime as the value set in app/config/session.php .当 Laravel 设置选项时,它会将cookie_lifetime作为在app/config/session.php设置的值传递。

However, sessions are not expired immediately after the max lifetime is reached.但是,会话不会在达到最大生命周期后立即过期。 What happens is after that amount of time has passed the session is then available to be removed by the garbage collector.发生的事情是在这段时间过去之后,垃圾收集器可以删除会话。

To solve the issue解决问题

One workaround is to check your php.ini file.一种解决方法是检查您的php.ini文件。 You may have this variable defined: session.gc_maxlifetime .您可能定义了这个变量: session.gc_maxlifetime By default it is set to 1440. Just comment or delete it .默认设置为 1440。只需评论或删除它

From this time on you session may work properly using your session.php config values.从此时起,您的会话可能会使用您的 session.php 配置值正常工作。

Native PHP session support was dropped starting in Laravel 4.1从 Laravel 4.1 开始,原生 PHP 会话支持被取消

To configure session lifetime edit app/config/session.php and set the following:要配置会话生命周期编辑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,

References:参考:

Run artisan changes 4.1.* at the command line to see the note about the native session driver being equivalent to file在命令行运行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

check for lifetime...检查终身...

you can also set...你也可以设置...

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

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

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