0

当用户检查选项并登录时,我正在尝试更改记住我的 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'));
}

}

过滤器.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
    }
});

路由.php

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

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

1 回答 1

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
    }
}
});
于 2013-09-02T13:55:03.740 回答