Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在Laravel中设置会话超时?_Php_Laravel_Session - Fatal编程技术网

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

Php 如何在Laravel中设置会话超时?,php,laravel,session,Php,Laravel,Session,是否有一种固有的方式来设置会话,使其在特定时间后过期。我当前的设置似乎在30分钟后过期,我想禁用它或至少增加它,但我在Laravel中找不到任何可以设置它的地方?App\Config\Session.php 检查生命周期 您还可以设置 Cookie::make('name', 'value', 60); // 1 hr 检查你的php.ini,它有一个session.cookie_life的值,该值设置了php允许会话持续的时间限制。当Laravel设置选项时,它会将cookie\u life

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

App\Config\Session.php

检查生命周期

您还可以设置

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

检查你的php.ini,它有一个session.cookie_life的值,该值设置了php允许会话持续的时间限制。当Laravel设置选项时,它会将
cookie\u life
作为
app/config/session.php
中设置的值传递

但是,会话不会在达到最大生存期后立即过期。发生的情况是,在经过该时间之后,会话就可以被垃圾收集器删除

解决问题

一种解决方法是检查
php.ini
文件。您可以定义此变量:
session.gc\u maxlifetime
。默认情况下,它设置为1440。只需评论或删除它


从现在起,您的会话可以使用session.php配置值正常工作

app/config/session.php
中,您有:

生存期

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

表示会话将在一小时后过期

这里还有一个设置:

'expire_on_close' => true,
这决定了浏览器关闭时会话是否过期

您可能感兴趣的其他设置还有
php.ini
以下值:

session.cookie_lifetime = 0

这些是默认值

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


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

从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,
参考资料:

  • 。(因为它会自动将cookie信息添加到头中,如果框架想要完全包装请求/响应,则需要进行大量的工作)
  • 在《Laravel 4.0至4.1升级指南》中讨论
在命令行中运行artisan changes 4.1.*以查看有关本机会话驱动程序等效于文件的说明

$ 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.
第一部分(生存期和到期时间)是正确的;其余的都不是。
/* 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.* | grep -i native
-> Native session driver has been replaced by 'file'. Specifying 'native' driver will just use the new file driver.