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

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

问题描述

我对Laravel相当新,并且有一个关于记住我功能的问题。



我已经成功启用了记住我功能, Auth :: attempt(array('email'=> $ email))。Auth :: attempt(array('email'=> $ email ,'password'=> $ password),true))
{
//正在记住用户...
}

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



我基本上想在记住我功能设置到期日期。



看看控制台,我看到启用记住我 HASH} Cookie。



要覆盖此Cookie中指定的过期日期的最佳方式是什么?



请记住,我必须在sessions.php中设置'lifetime'=> 0,我可以根据用户的偏好触发记住我的功能,所以改变为一个星期不会在我的情况下工作。



谢谢!

解决方案

我不知道它是否是一个好的方式做,但如果你绝望设置到期时间记住我,你可以试试这个



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



我们将在因此在 filters.php 文件中查找 App :: after()方法并更改cookie的过期时间

  App :: after(function($ request,$ response)
{
if(Auth :: check()){
$ ckname = Auth :: getRecallerName(); //获取cookie的名称,记住我的过期时间存储
$ ckval = Cookie :: get($ ckname); //获取cookie的值
return $ response-> withCookie(Cookie :: make($ ckname,$ ckval,360)); // change the expiration time
}
});

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


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

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.

Looking at the console, I can see that enabling "remember me" generates a remember_{HASH} 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.

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.

Thanks!

解决方案

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.

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

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

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

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

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