簡體   English   中英

無法在Laravel 4.1.28中設置記住我的cookie到期日期

[英]Unable to set the remember me cookie expiry date in laravel 4.1.28

背景:

我用laravel 4.0.x版,並使用此代碼重置remember_me cookie的到期日至1個月(自默認為5年):

App::after(function($request, $response)
{
    // If the user is tryin to log_in and he wants to stay logged in, reset the remember_me cookie expiration date from 5 years to 1month
    $remember = Input::get('remember',false);
    if ($remember) {
        if ( Auth::check()){ // check if the user is logged in
            $ckname = Auth::getRecallerName(); //Get the name of the cookie, where remember me expiration time is stored
            $ckval  = Cookie::get($ckname); //Get the value of the cookie
            return $response->withCookie(Cookie::make($ckname,$ckval,43200)); //change the expiration time to 1 month = 43200 min
        }
    }

該代碼當然來自app \\ filters.php ,它的工作方式就像魅力。

問題 :

我最近將laravel從4.0.x更新到了v4.1.28,現在Remember_me cookie設置為5年,我嘗試了最后幾個小時來挖掘試圖調試的代碼,但是沒有運氣:(。
請注意,在上述代碼的最后一行中,將$ ckname更改另一個值,例如“ test” ,可以很好地工作,它會創建一個“ test” cookie,有效期為1個月,就像我打算的那樣

return $response->withCookie(Cookie::make("test",$ckval,43200));

我真的不明白為什么Remember_me Cookie會持續到5年有效期!

任何幫助,將不勝感激 :)
阿卜杜。

更新:

問題不是為什么我要更改cookie的到期日期,而是為什么cookie不會更新? 謝謝。

這是Laravel的故意行為。

Remember_me設置用於“永久”記住用戶,直到用戶“注銷”應用程序為止。

如果您深入研究Auth類-它專門表示它是“永久cookie”。

public function login(UserInterface $user, $remember = false)
    {
        $this->updateSession($user->getAuthIdentifier());

        // If the user should be permanently "remembered" by the application we will
        // queue a permanent cookie that contains the encrypted copy of the user
        // identifier. We will then decrypt this later to retrieve the users.
        if ($remember)
        {
            $this->createRememberTokenIfDoesntExist($user);

            $this->queueRecallerCookie($user);
        }

除了“永久”(也就是5年)以外,沒有辦法將Cookie設置為其他任何內容。

另外-Laravel文檔指出永遠記住我:

如果您想在應用程序中提供“記住我”功能,則可以將true作為第二個參數傳遞給try方法,這將使用戶無限期地進行身份驗證(或直到他們手動注銷為止)

編輯:當您更新了問題時-我進行了更多調查。 這對我有用:

public function login()
{
    if (Auth::attempt(array('email' => Input::get('email'), 'password' => ), true))
    {
        $ckname = Auth::getRecallerName();
        Cookie::queue($ckname, Cookie::get($ckname), 43200);
        return View::make('welcome');
    }
}

它將“ remember_me” cookie設置為1個月。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM