使用Laravel 4更改cookie过期

时间:2013-08-10 22:21:28

标签: cookies laravel laravel-4

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

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

LoginController.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');

1 个答案:

答案 0 :(得分: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
    }
}
});