Laravel 4记住我过期时间 [英] Laravel 4 Remember me expire time

查看:24
本文介绍了Laravel 4记住我过期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Laravel 的新手,对记住我的功能有疑问.

I am fairly new to Laravel and had a question regarding the remember me function.

我已经通过像这样将第二个参数附加到 Auth::attempt 方法成功地启用了记住我"功能.

I have successfully enabled the "remember me" function by attaching a second argument to the Auth::attempt method like so.

if (Auth::attempt(array('email' => $email, 'password' => $password), true))
{
    // The user is being remembered...
}

如文档中所述,这可以无限期地记住我,或者直到用户手动注销.

As noted in the documentation, this enables remember me indefinitely or until an user manually logs out.

我实际上是想在记住我"功能上设置一个过期日期.

I essentially want to set an expire date on the "remember me" function.

查看控制台,我可以看到启用记住我"会生成一个 remember_{HASH} cookie.

Looking at the console, I can see that enabling "remember me" generates a remember_{HASH} cookie.

覆盖此 cookie 中指定的过期日期以假设未来一周的最佳方法是什么?cookie 当前将日期设置为过去,从而使其永久有效.

What would be the best way to overwrite the expire date specified in this cookie to let say a week in the future? The cookie currently sets the date in the past, which makes it last forever.

请记住,我必须在 session.php 中设置 'lifetime' => 0 以便我可以根据用户偏好触发记住我的功能,因此在我的情况下将其更改为一周是行不通的.

Keep in mind that I had to set 'lifetime' => 0 in sessions.php so that I can trigger the remember me function based on user preference so changing this into a week would not work in my case.

谢谢!

推荐答案

我不知道这是否是一个好方法,但如果你不顾一切设置过期时间记住我你可以试试这个,但它对我有用.

I don't know whether its a good way to do it or not, but if you are desperate to set the expiration time for remember me you can try this, it works for me though.

让 laravel 的 Auth::attempt($credential,true) 做它的正常业务,即将记住我的过期时间设置为 5 年

Let the laravel's Auth::atempt($credential,true) do its normal business, i.e, setting the remember me expiration time to 5 years

我们将在 App::after() 方法中进行更改,因此在您的 filters.php 文件中找到 App::after() 方法和更改 cookie 过期时间

We will change this in App::after() method, so in your filters.php file find App::after() method and change the cookie expiration time

App::after(function($request, $response)
{
  if ( Auth::check()){
      $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,360)); //change the expiration time
  }
});

注意:Cookie::make('name','value','expiration time');

这篇关于Laravel 4记住我过期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆