2014-09-27 163 views
17

是否有一种固有的方法来设置会话在一段时间后过期。我目前的设置似乎在30分钟后过期,我想禁用该设置或至少增加该设置,但是我无法在Laravel中找到可以设置此位置的任何位置?如何在Laravel 4.2中设置会话超时?

回答

20

检查的php.ini,它有session.gc_maxlifetime的值(和也session.cookie_lifetime),它限制了PHP允许会话持续多长时间。当Laravel设置选项时,它通过cookie_lifetime作为app/config/session.php中设置的值。

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

为了解决这个问题

一个解决办法是检查你的php.ini文件。你可以定义这个变量:session.gc_maxlifetime。默认情况下,它被设置为1440. 只是评论或删除它

从这个时候开始,session可以正常使用你的session.php配置值。

5

App\Config\Session.php

检查终身...

您还可以设置...

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

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决定运行垃圾回收器的机会是多少。默认情况下,在1440秒(24分钟)后,此会话数据将被破坏的可能性只有1%。

+2

的这第一部分(终身和expire_on_close)是正确的;其余的不是。 – 2015-01-31 17:52:48

8

本地PHP会话支持滴在Laravel 4.1

开始配置会话有效期编辑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.