简体   繁体   English

无法在Laravel 4.1.28中设置记住我的cookie到期日期

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

Background: 背景:

I was Using laravel 4.0.x , and resetting the remember_me cookie expiry date to 1 month (since the default is 5 years ) using this code : 我用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
        }
    }

That code is from app\\filters.php of course, and it was working like charm. 该代码当然来自app \\ filters.php ,它的工作方式就像魅力。

The problem : 问题 :

I recently updated laravel from 4.0.x to v4.1.28, and now the remember_me cookies are set to 5 years, I tried for the last hours digging in the code trying to debug but with no luck :( . 我最近将laravel从4.0.x更新到了v4.1.28,现在Remember_me cookie设置为5年,我尝试了最后几个小时来挖掘试图调试的代码,但是没有运气:(。
Notice that changing $ckname to another value like "test" in the last line of the above code works just fine, it creates a "test" cookie with an expiry of 1 month like I intended 请注意,在上述代码的最后一行中,将$ ckname更改另一个值,例如“ test” ,可以很好地工作,它会创建一个“ test” cookie,有效期为1个月,就像我打算的那样

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

I don't really understand why the remember_me cookie persist to the 5 years expiry date ! 我真的不明白为什么Remember_me Cookie会持续到5年有效期!

Any help would be appreciated :) 任何帮助,将不胜感激 :)
Abdou. 阿卜杜。

Update: 更新:

The question is not why I want to change the cookie expiry date, but it's why the cookie won't get updated?! 问题不是为什么我要更改cookie的到期日期,而是为什么cookie不会更新? . Thanks. 谢谢。

This is intentional behavior with Laravel. 这是Laravel的故意行为。

The remember_me settings is to "permanently" remember the User, until such time as they 'logout' of the application. Remember_me设置用于“永久”记住用户,直到用户“注销”应用程序为止。

If you dig into the Auth classes - it specifically says it is a "permanent cookie". 如果您深入研究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);
        }

There is no way to set the cookie to anything other than 'permanent' (aka 5 years). 除了“永久”(也就是5年)以外,没有办法将Cookie设置为其他任何内容。

Also - the Laravel docs state the remember me is forever: 另外-Laravel文档指出永远记住我:

If you would like to provide "remember me" functionality in your application, you may pass true as the second argument to the attempt method, which will keep the user authenticated indefinitely (or until they manually logout) 如果您想在应用程序中提供“记住我”功能,则可以将true作为第二个参数传递给try方法,这将使用户无限期地进行身份验证(或直到他们手动注销为止)

Edit: as you've updated your question - I looked into it more. 编辑:当您更新了问题时-我进行了更多调查。 This works for me: 这对我有用:

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');
    }
}

It only sets the 'remember_me' cookie to 1 month. 它将“ remember_me” cookie设置为1个月。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM