背景:
我使用laravel 4.0.x,并使用以下代码将remember_mecookie到期日重置为1个月(因为默认值为5年(:
App::after(function($request, $response)
{
// If the user is tryin to log_in and he wants to stay logged in, reset the remember_me cookie expiration date from 5 years to 1month
$remember = Input::get('remember',false);
if ($remember) {
if ( Auth::check()){ // check if the user is logged in
$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,43200)); //change the expiration time to 1 month = 43200 min
}
}
当然,该代码来自app\filters.php,它的工作方式很有魅力。
问题是:
我最近将laravel从4.0.x更新到了4.1.28版本,现在remember_me cookie设置为5年,我在最后几个小时里试图挖掘代码进行调试,但没有成功:(。
请注意,在上面代码的最后一行中,将$ckname更改为另一个值,如"test">,效果很好,它会创建一个的"test"[/em>cookie,有效期为1个月,就像我想要的一样
return $response->withCookie(Cookie::make("test",$ckval,43200));
我真的不明白为什么记忆饼干能坚持到5年的有效期!
如有任何帮助,我们将不胜感激:(
阿卜杜。
更新:
问题不是我为什么要更改cookie的有效期,而是为什么cookie不会更新。谢谢
这是Laravel的故意行为。
remember_me设置是"永久"记住用户,直到他们"注销"应用程序。
如果你深入研究Auth类,它特别说它是一个"永久cookie"。
public function login(UserInterface $user, $remember = false)
{
$this->updateSession($user->getAuthIdentifier());
// 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->createRememberTokenIfDoesntExist($user);
$this->queueRecallerCookie($user);
}
没有办法将cookie设置为"永久"(即5年(以外的任何内容。
此外,Laravel医生表示,记住我是永远的:
如果您想在应用程序中提供"记住我"功能,您可以将true作为第二个参数传递给尝试方法,这将使用户无限期地通过身份验证(或直到他们手动注销(
编辑:当你更新你的问题时,我对它进行了更多的研究。这对我有效:
public function login()
{
if (Auth::attempt(array('email' => Input::get('email'), 'password' => ), true))
{
$ckname = Auth::getRecallerName();
Cookie::queue($ckname, Cookie::get($ckname), 43200);
return View::make('welcome');
}
}
它只将"remember_me"cookie设置为1个月。