2

最近我决定在我的 Laravel 4 应用程序中添加“记住我”功能。找到了合适的语法方法:

Auth::attempt(array $credentials = array(), $remember = false)

这是为满足我的需求而采用的,如下所示:

Auth::attempt($userdata, Input::has('remember'))

应用程序保持身份验证会话,即使在浏览器关闭后用户也被验证。

虽然,我发现现在 Laravel 始终保持用户身份验证,无论“记住”复选标记是什么状态。

我试图这样做:

Auth::attempt($userdata, false)

Auth::attempt($userdata,)

用户仍然通过浏览器会话进行身份验证!!!现在,由于Auth::attempt($userdata)没有保留 auth 会话,我觉得每当Auth::attempt方法中有第二个参数的迹象时,Laravel 自动将其假定为“真”。谁能澄清一下?

编辑:为了让每个人都非常清楚,我将列出重新创建此行为的步骤:

  1. 退出应用程序Auth::logout();
  2. 再次登录Auth::attempt($userdata, false)
  3. 关闭并打开浏览器
  4. 转到应用程序网址。
  5. 应用程序已加载验证

这是我在这里的第一个问题,所以请耐心等待我:)

4

1 回答 1

4

编辑:OP明确表示他Auth::logout()正确调用,因此编辑答案以包含“真实”答案。

lifetime值设置app/config/session/php0以在浏览器关闭时清除 cookie。

上一个答案

这是(其外观为)类中的login方法,最终由 调用。Illuminate\Auth\GuardAuthAuth::attempt()

来源: http: //laravel.com/api/source-class-Illuminate.Auth.Guard.html#263-291

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

     $this->session->put($this->getName(), $id);

     // 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->queuedCookies[] = $this->createRecaller($id);
     }

     // If we have an event dispatcher instance set we will fire an event so that
     // any listeners will hook into the authentication events and run actions
     // based on the login and logout events fired from the guard instances.
     if (isset($this->events))
     {
         $this->events->fire('auth.login', array($user, $remember));
     }

     $this->setUser($user);
 }

很明显,即使在设置为 时$remember设置了 cookie,但在设置为 false 或其他非真实值true时,cookie 本身不会被清除。$remember

Auth::logout()调用函数时会清除 cookie 。

于 2013-11-15T03:03:43.420 回答