【问题标题】:Changing cookie expiration with Laravel 4使用 Laravel 4 更改 cookie 过期时间
【发布时间】:2013-08-10 22:21:28
【问题描述】:

当用户检查选项并登录时,我正在尝试更改记住我 cookie 的过期时间,但运行此代码时过期时间不会改变。在 Laravel 4 中我需要做什么来解决这个问题?

更具体地说,我想做的是将过期时间设置为 12 小时。我尝试了 App:after 方法,但每次运行该代码时,它都会将现有的 cookie 刷新回原来的 12 小时。我怎样才能让它运行,这样它就不会那样做?

登录控制器.php

class LoginController extends BaseController {

public function __construct()
{
    $this->beforeFilter('csrf', array('on' => 'post'));
}

public function getIndex()
{   
    if (Auth::check()) {
        return Redirect::action('HomeController@usersIndex')
            ->with('message', 'You are already logged in.');
    }

    return View::make('guests.login');
}

public function postIndex()
{
    $validation = User::validateForm(Input::all());

    if ($validation->passes()) {
        $user = array(
            'username' => Input::get('username'),
            'password' => Input::get('password')
        );

        // Try to log the user in.
        if (Auth::attempt($user, Input::has('remember_me'))) {  
            return Redirect::to('/');
        }

        return Redirect::to('login')
            ->withErrors(array('password' => 'The username or password you entered is incorrect.'))
            ->withInput(Input::except('password'));
    }

    return Redirect::to('login')
        ->withErrors($validation)
        ->withInput(Input::except('password'));
}

}

filters.php

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,720)); //change the expiration time
    }
});

routes.php

Route::get('/', 'HomeController@usersIndex');
Route::get('logout', 'HomeController@logout');

Route::controller('login', 'LoginController');
Route::controller('register', 'RegistrationController');
Route::controller('password', 'PasswordController');

【问题讨论】:

  • 你不能在你的控制器中改变它,你必须在App::after方法中改变它
  • @Trying Tobemyself 我想要做的是将过期时间设置为 12 小时。我尝试了 App:after 方法,但每次运行该代码时,它都会在现有 cookie 上再增加 12 个小时。我怎样才能让它运行,这样它就不会那样做?
  • 它为我工作,你能发布代码
  • @Trying Tobemyself 我已经使用包含 App:after 方法的现有代码编辑了我的帖子。
  • 你确定它增加了 12 小时而不是覆盖时间吗?因为完全相同的代码对我有用。你能发布 cookie 的详细信息吗?

标签: cookies laravel laravel-4


【解决方案1】:

您可以使用App::after 方法更改rememberme cookie 的过期时间。 所以在您的filters.php 文件find 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
  }
});

来自讨论:

你:@Trying 我想做的是将过期时间设置为 12 小时。我尝试了App:after 方法,但每次代码运行时,它都会覆盖/刷新到原来的 12 小时。我将如何解决这个问题?

我:您可以尝试在您的登录方法中设置会话值,然后在App::after 方法中检查它,会话值, 如果存在,则更新 cookie 并删除会话值, 如果你这样做,它只会在你登录时更新 cookie。

例子:-

Login code
if (Auth::attempt($user, Input::has('remember_me'))) {  
   Session::put('key',value);   
   return Redirect::to('/');
}

App after method
App::after(function($request, $response) {
    if(Session::has('key')){
    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
        Session::forget('key');
        return $response->withCookie(Cookie::make($ckname,$ckval,720)); //change the expiration time
    }
}
});

【讨论】:

    猜你喜欢
    • 2015-02-09
    • 1970-01-01
    • 2019-05-27
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 2017-09-04
    • 2014-05-03
    相关资源
    最近更新 更多