Laravel 4 记住我过期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18070400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 Remember me expire time
提问by Kevin Jung
I am fairly new to Laravel and had a question regarding the remember me function.
我对 Laravel 相当陌生,并且对记住我的功能有疑问。
I have successfully enabled the "remember me" function by attaching a second argument to the Auth::attempt method like so.
通过将第二个参数附加到 Auth::attempt 方法,我已经成功启用了“记住我”功能。
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.
查看控制台,我可以看到启用“记住我”会生成一个 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.
覆盖此 cookie 中指定的到期日期以在未来一周内说的最佳方法是什么?cookie 当前将日期设置为过去,从而使其永远持续下去。
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.
请记住,我必须在 session.php 中设置 'lifetime' => 0 以便我可以根据用户偏好触发记住我的功能,因此在我的情况下将其更改为一周是行不通的。
Thanks!
谢谢!
回答by Trying Tobemyself
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
让laravelAuth::atempt($credential,true)
做正常的事情,即将remember me过期时间设置为5年
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()
方法中更改此方法,因此在您的filters.php
文件中查找App::after()
方法并更改 cookie 过期时间
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');
注意:Cookie::make('name','value','expiration time');