Cookies 使用Laravel 4更改cookie过期时间
当用户选中该选项并登录时,我试图更改“记住我”cookie的过期时间,但当我运行此代码时,过期时间不会更改。我需要做什么才能在Laravel 4中修复此问题 更具体地说,我想做的是将过期时间设置为12小时。我尝试了App:after方法,但每次代码运行时,它都会将现有cookie刷新回原来的12小时。我如何让它运行,使它不会这样做 LoginController.phpCookies 使用Laravel 4更改cookie过期时间,cookies,laravel,laravel-4,Cookies,Laravel,Laravel 4,当用户选中该选项并登录时,我试图更改“记住我”cookie的过期时间,但当我运行此代码时,过期时间不会更改。我需要做什么才能在Laravel 4中修复此问题 更具体地说,我想做的是将过期时间设置为12小时。我尝试了App:after方法,但每次代码运行时,它都会将现有cookie刷新回原来的12小时。我如何让它运行,使它不会这样做 LoginController.php class LoginController extends BaseController { public function
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
方法更改rememberme cookie过期时间。
因此,在您的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
}
});
通过讨论:
你:@试一下我想做的就是把过期时间设为12小时。我尝试了App:after
方法,但每次代码运行时,它都会覆盖/刷新到原来的12小时。我该如何解决这个问题
Me:您可以尝试在您的登录方法中设置会话值,然后在App::method之后,在方法中检查会话值,
如果存在,则更新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
}
}
});
您不能在控制器中更改它,您必须在App::after
method@Trying我想做的是将过期时间设置为12小时。我尝试了App:after方法,但每次代码运行时,它都会在现有cookie上增加12个小时。我如何使它运行,使它不会这样做?它为我工作,你可以张贴code@Trying为了证明自己,我已经用我使用的现有代码编辑了我的帖子,其中包括App:after方法。你确定它会增加12小时而不是覆盖时间吗?因为完全相同的代码对我有效。你能发布cookie的详细信息吗?