簡體   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