2015-10-19 139 views
7

我有一個L5.1應用程序,並且遇到會話隨機過期的問題。它發生在我的本地環境和現場環境中,所以它看起來不像是服務器問題。本地我正在使用PHP 5.5.11的XAMPP。在現場上的是運行Centos 6的專用服務器,使用PHP 5.6.13。Laravel 5.1會話隨機過期

如果我在大約20-30個頁面請求會話丟失後最終瀏覽網站,並且用戶已註銷,則會發生此問題。我使用的數據庫驅動程序,這裏是我的會話配置:

<?php 

return [ 

    /* 
    |-------------------------------------------------------------------------- 
    | Default Session Driver 
    |-------------------------------------------------------------------------- 
    | 
    | This option controls the default session "driver" that will be used on 
    | requests. By default, we will use the lightweight native driver but 
    | you may specify any of the other wonderful drivers provided here. 
    | 
    | Supported: "file", "cookie", "database", "apc", 
    |   "memcached", "redis", "array" 
    | 
    */ 

    'driver' => 'database', 

    /* 
    |-------------------------------------------------------------------------- 
    | Session Lifetime 
    |-------------------------------------------------------------------------- 
    | 
    | Here you may specify the number of minutes that you wish the session 
    | to be allowed to remain idle before it expires. If you want them 
    | to immediately expire on the browser closing, set that option. 
    | 
    */ 

    'lifetime' => 120, 

    'expire_on_close' => false, 

    /* 
    |-------------------------------------------------------------------------- 
    | Session Encryption 
    |-------------------------------------------------------------------------- 
    | 
    | This option allows you to easily specify that all of your session data 
    | should be encrypted before it is stored. All encryption will be run 
    | automatically by Laravel and you can use the Session like normal. 
    | 
    */ 

    'encrypt' => false, 

    /* 
    |-------------------------------------------------------------------------- 
    | Session File Location 
    |-------------------------------------------------------------------------- 
    | 
    | When using the native session driver, we need a location where session 
    | files may be stored. A default has been set for you but a different 
    | location may be specified. This is only needed for file sessions. 
    | 
    */ 

    'files' => storage_path('framework/sessions'), 

    /* 
    |-------------------------------------------------------------------------- 
    | Session Database Connection 
    |-------------------------------------------------------------------------- 
    | 
    | When using the "database" or "redis" session drivers, you may specify a 
    | connection that should be used to manage these sessions. This should 
    | correspond to a connection in your database configuration options. 
    | 
    */ 

    'connection' => null, 

    /* 
    |-------------------------------------------------------------------------- 
    | Session Database Table 
    |-------------------------------------------------------------------------- 
    | 
    | When using the "database" session driver, you may specify the table we 
    | should use to manage the sessions. Of course, a sensible default is 
    | provided for you; however, you are free to change this as needed. 
    | 
    */ 

    'table' => 'sessions', 

    /* 
    |-------------------------------------------------------------------------- 
    | Session Sweeping Lottery 
    |-------------------------------------------------------------------------- 
    | 
    | Some session drivers must manually sweep their storage location to get 
    | rid of old sessions from storage. Here are the chances that it will 
    | happen on a given request. By default, the odds are 2 out of 100. 
    | 
    */ 

    'lottery' => [2, 100], 

    /* 
    |-------------------------------------------------------------------------- 
    | Session Cookie Name 
    |-------------------------------------------------------------------------- 
    | 
    | Here you may change the name of the cookie used to identify a session 
    | instance by ID. The name specified here will get used every time a 
    | new session cookie is created by the framework for every driver. 
    | 
    */ 

    'cookie' => 'laravel_session', 

    /* 
    |-------------------------------------------------------------------------- 
    | Session Cookie Path 
    |-------------------------------------------------------------------------- 
    | 
    | The session cookie path determines the path for which the cookie will 
    | be regarded as available. Typically, this will be the root path of 
    | your application but you are free to change this when necessary. 
    | 
    */ 

    'path' => '/', 

    /* 
    |-------------------------------------------------------------------------- 
    | Session Cookie Domain 
    |-------------------------------------------------------------------------- 
    | 
    | Here you may change the domain of the cookie used to identify a session 
    | in your application. This will determine which domains the cookie is 
    | available to in your application. A sensible default has been set. 
    | 
    */ 

    'domain' => null, 

    /* 
    |-------------------------------------------------------------------------- 
    | HTTPS Only Cookies 
    |-------------------------------------------------------------------------- 
    | 
    | By setting this option to true, session cookies will only be sent back 
    | to the server if the browser has a HTTPS connection. This will keep 
    | the cookie from being sent to you if it can not be done securely. 
    | 
    */ 

    'secure' => false, 

]; 

因爲我不是手動清除該會話的任何地方,我相當有信心,沒有什麼我做可能會造成它。是否有任何已知的錯誤可能導致此行爲?

+1

建議您根據您在登錄時設置的「恢復令牌」cookie,添加中間件以自動將用戶重新登錄回去。將令牌有效性設置爲等於或大於會話持續時間。否則,你是在你的會話存儲和laravels越野車彩票功能的擺佈。 – r3wt

回答

7

進一步回落檢查您的會話席捲彩票如果您正在使用database驅動程序,然後有時你可以運行與​​場太短的問題。默認情況下,​​字段是text字段,它允許64KB的數據。

這看起來很多,但是由於有效載荷是作爲序列化數組存儲的,基本上它是一個非常長的字符串,然後進行base64編碼,所以它可以很容易地達到它的最大長度,這將有效地破壞會話用戶。

我建議將數據類型更改爲MEDIUMTEXT(最大16MB),或者重新考慮首先將多少數據保存到會話中。

+1

哦,我的上帝mikemike你是一個天才非常感謝,我必須給你買一杯啤酒!我認爲像您說的那樣長期,我需要重新考慮我在會話中存儲的信息,但是在將列更改爲中文時已經解決了問題。 – geoffs3310

1

請同一個文件config/session.php

/* 
|-------------------------------------------------------------------------- 
| Session Sweeping Lottery 
|-------------------------------------------------------------------------- 
| 
| Some session drivers must manually sweep their storage location to get 
| rid of old sessions from storage. Here are the chances that it will 
| happen on a given request. By default, the odds are 2 out of 100. 
| 
*/ 

'lottery' => [2, 100],