我对 Laravel 还很陌生,并且对记住我功能有疑问。
我通过将第二个参数附加到 Auth::attempt 方法,成功启用了“记住我”功能,如下所示。
if (Auth::attempt(array('email' => $email, 'password' => $password), true))
{
// The user is being remembered...
}
如文档中所述,这可以无限期地记住我,或者直到用户手动注销为止。
我本质上想为“记住我”功能设置一个到期日期。
查看控制台,我可以看到启用“记住我”会生成一个 Remember_{HASH} cookie。
覆盖此 cookie 中指定的过期日期(比如 future 一周)的最佳方法是什么? Cookie 当前将日期设置为过去的日期,这使其永久有效。
请记住,我必须在sessions.php中设置'lifetime' => 0,以便我可以根据用户偏好触发记住我的功能,因此将其更改为一周在我的情况下不起作用。
谢谢!
最佳答案
我不知道这是否是一个好方法,但是如果您迫切希望设置“记住我”的过期时间,您可以尝试这个,但它对我有用。
让 laravel 的 Auth::atempt($credential,true)
执行正常业务,即将记住我的过期时间设置为 5 年
我们将在 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
}
});
注意:Cookie::make('name','value','expiration time');
关于php - Laravel 4 记住我过期时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18070400/