2013-08-10 3 views
0

사용자가 옵션을 확인하고 로그인 할 때 Remember me 쿠키의 만료 시간을 변경하려고하지만이 코드를 실행할 때 만료 시간이 변경되지 않습니다. Laravel 4에서이 문제를 해결하려면 무엇을해야합니까?Laravel 4를 사용하여 쿠키 만료 변경

더 구체적으로 말하자면 만료 시간을 12 시간으로 설정하고 싶습니다. App : after 메서드를 사용해 보았지만 코드가 실행될 때마다 기존 쿠키를 원래의 12 시간으로 다시 새로 고칩니다. 나는 그것을 어떻게하지 않아서 그렇게하지 않을 것인가?

LoginController.php 당신은에서 rememberme 쿠키 만료 시간을 변경 App::after 방법을 사용할 수 있습니다

Route::get('/', '[email protected]'); 
Route::get('logout', '[email protected]'); 

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

당신이 컨트롤러에서 변경할 수 없습니다, 당신은 만료 시간을 설정 할 원하는 after' 방법 –

+0

@Trying Tobemyself :: 앱'에서 변경해야 ~ 12 시간. 나는 App : after 메서드를 사용했지만 코드가 실행될 때마다 기존의 쿠키에 12 시간을 추가/추가합니다. 나는 그것을 어떻게하지 않아서 그렇게하지 않을 것인가? – user1307016

+0

그게 나를 위해 일하고, 당신은 코드를 게시 할 수 –

답변

1

class LoginController extends BaseController { 

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

public function getIndex() 
{ 
    if (Auth::check()) { 
     return Redirect::action('[email protected]') 
      ->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. 이므로 filters.php 파일 find App::after() 방법으로 쿠키 만료 시간을 변경하십시오. 예 - 토론에서

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 시간 만료 시간을 설정하고 싶은 무엇 @Trying. App:after 메서드를 시도했지만 코드가 실행될 때마다 원래 12 시간을 덮어 쓰거나 새로 고칩니다. 어떻게 해결할 수 있을까요?

나 : 그것은 업데이 트 쿠키를 존재하고 세션 값을 삭제하면 u는 이렇게하면 당신은 UR 로그인 방법에 세션 값을 설정하려고 할 수 THN이 App::after 방법, 세션 값에 그것을 확인 , 이 만 업데이트됩니다 일단 쿠키, 나는 로그인 할 때 전자.

예 : -

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 
    } 
} 
});