Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

sanjay1688's avatar

laravel session expire on redirect

Hey i m having routes in my route file Route::get('login', array('as' => 'show.login', 'uses' => 'HomeController@showLogin')); Route::post('login', array('as' => 'do.login', 'uses' => 'HomeController@doLogin')); Route::get('/', array('as' => 'home.home','uses' => 'HomeController@home'));

Insdie my home controller

public function doLogin(){

$rules = array('email'    => 'required|email','password' => 'required');

$validator = Validator::make(Input::all(), $rules);

if ($validator->fails()) {

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

    $user           =   Customeruser::where('email', Input::get('email'))->first();

    if(!$user) {
        $attempt = false;
    } else {
        $attempt = Auth::attempt(array('email' => Input::get('email') , 'password' => Input::get('password') ));
    }       

    if($attempt) {
        
        //Session::flush();
        Session::put('user_id', Auth::user()->uuid);
        Session::put('user_name', Auth::user()->first_name." ".Auth::user()->last_name);
        Session::put('user_email', Auth::user()->email);
        // return Session::all();
        // return Redirect::to('/')->with(array('message' => 'Successfully logged in!', 'flash_type' => 'success'));
        $occasions      =   Occasion::with('occasioncategory')->active()->latest()->take(8)->get(); 
        return View::make('home.home' , compact('occasions')); 

        return Redirect::to('/')->with(array('message' => 'Successfully logged in!', 'flash_type' => 'success'));
    }

    return Redirect::back()->with(array('message' => 'Invalid credentials, please try again'))->withInput();

}

}

public function home() {

return Session::all();

}

if retrun return Session::all(); from doLogin function before redirect i can able print session on browser as soon as i redirect i can't able to see my session

What is the issuse can anybody helpme out

0 likes
2 replies
richard's avatar
richard
Best Answer
Level 21

Your login function looks messy! Simply use this

public function login()
{
$rules = array('email'    => 'required|email','password' => 'required');

$validator = Validator::make(Input::all(), $rules);

if ($validator->fails()) {
    return Redirect::to('login')->withErrors($validator)->withInput(Input::except('password')); 
} else {
    $attempt = Auth::attempt(array('email' => Input::get('email') , 'password' => Input::get('password') ));
    if($attempt) {
            $occasions = Occasion::with('occasioncategory')->active()->latest()->take(8)->get();
            
        return Redirect::to('/')
        ->with(array('message' => 'Successfully logged in!', 'flash_type' => 'success'))
        ->with(compact('ocassions'))
        }
    return Redirect::back()
        ->with(array('message' => 'Invalid credentials, please try again'))
        ->withInput();
    }
}

You do not have to save User's info manually in a Session. You can access the info anytime anywhere in the entire application using the Auth::user() object. e.g Auth::user()->email returns the email of the logged in user.

sanjay1688's avatar

Hey richard actually the on redirect my session value are getting destory.

I had 2 routes if i uncomment from 1 route i can able to see my session values in 1st route. But if i get redirect to my 2nd route i cant able see my session variables can any one help me out.

Route::get('/check', function(){  
    Session::put('id', 1); 
    Session::put('id1', 11); 
     //return Session::all();
    return Redirect::to('/check1');
});

Route::get('/check1', function(){  
     return Session::all();
});

Please or to participate in this conversation.