【问题标题】:Unable to set the remember me cookie expiry date in laravel 4.1.28无法在 laravel 4.1.28 中设置记住我的 cookie 到期日期
【发布时间】:2014-07-05 23:52:45
【问题描述】:

背景:

我正在使用 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" 像我预期的那样过期 1 个月的 cookie

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

我真的不明白为什么 remember_me cookie 会持续到 5 年到期日期!

任何帮助将不胜感激:)
阿卜杜。

更新:

问题不是我为什么要更改 cookie 到期日期,而是为什么 cookie 不会更新?! .谢谢。

【问题讨论】:

  • 您是否遵循此处的升级指南:laravel.com/docs/upgrade?同样在 app/config/session.php 中,您是否将“lifetime”属性设置为非零值(如 120)?
  • 是的,我遵循了整个升级指南,并且生命周期设置为 120,坦率地说我不相信这与配置有关,因为 cookie 设置正常,所以问题只是5年内:/
  • 您是否在使用任何其他软件包,例如 Cartalysts Sentry?
  • 不,只有 jeffrey way generator,我相信这与这个问题无关。
  • 您为什么不希望它是永久性的?为什么要让它过期?

标签: php cookies laravel laravel-4 remember-me


【解决方案1】:

这是 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);
        }

没有办法将 cookie 设置为“永久”(也就是 5 年)以外的任何值。

另外 - the Laravel docs 声明永远记住我:

如果您想在您的应用程序中提供“记住我”功能,您可以将 true 作为第二个参数传递给尝试方法,这将使用户无限期地保持身份验证(或直到他们手动注销)

编辑:当您更新了您的问题时 - 我对其进行了更多调查。这对我有用:

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 个月。

【讨论】:

  • 我知道记住我的 cookie 是永远的,我想将其更改为 1 个月,因为我发现 5 年太多了,无论如何我的问题范围是:为什么我的代码在 v4.1.28 中不再工作!尤其是设置另一个 cookie 会起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-23
  • 1970-01-01
相关资源
最近更新 更多