2014-09-27 40 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.