Skip to content

Laravel Sessions Not Working in 4.1

Screen Shot 2014-01-27 at 1.41.23 PM My Laravel app wasn’t retaining the session from one page to another. The session files were created in app/storage/session, but clearly weren’t used.

Basically don’t have any ‘echo’ statements in your function after the auth.

From https://github.com/laravel/framework/issues/161

For many people, it’s because they didn’t use the standard setup for users. Make sure your table is named ‘users’ and the key is ‘id’ as an int. Otherwise, you need to update auth.php and User.php (or similar) to return the right key, etc. That wasn’t my problem…

My login function was:

public function login()
{
	// auth
	if (Auth::attempt(Input::only('email', 'password'), true))
	{
	  // has session now
  	  echo "Welcome " . Auth::user()->username;
	  return '<a href="' . URL::route('companies.index') . '">Companies</a><br>';
	}
	// failed -> back to login
	return Redirect::back()->withErrors(['email' => 'Login failed.'])->withInput();
}

The ‘echo “Welcome…’ was preventing the session from being stored properly. The session file was created in app/storage/session, but it wasn’t used.

I can remove the echo and now change my link to be a true redirect (not that the link was working either).

public function login()
{
  // auth
  if (Auth::attempt(Input::only('email', 'password'), true))
  {
    return Redirect::route('companies.index');
  }
  // failed -> back to login
  return Redirect::back()->withErrors(['email' => 'Login failed.'])->withInput();
}

NOTE: The same goes for Auth::logout(). I can vouch for that.

Yay, time to shout it from the roof tops!!!

Comments are closed.