Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 session not persisting after user is logged in

I'm having an interesting issue with Laravel 5.

After logging in a user, the logged in status is not persisted across pages. Clearly it has something to do with Session::.

The way I'm logging in a user is pretty straight-forward:

if (Auth::attempt(['email' => $data['email'], 'password' => $data['password']],     isset($data['remember_me']) ? TRUE : FALSE)) {     return redirect()->intended('/'); } 

A simple print_r(Session::all()); gives me the following if the user is NOT logged in:

Array (     [_token] => wV8o75lZnCZ0f6CMMQgdBBM2AxSYjtWisAXx6TgZ     [flash] => Array         (             [old] => Array                 (                 )              [new] => Array                 (                 )          )      [_previous] => Array         (             [url] => http://localhost/public         )  ) 

After the user is logged in an redirected to / the array looks like this:

Array (     [_token] => wV8o75lZnCZ0f6CMMQgdBBM2AxSYjtWisAXx6TgZ     [flash] => Array         (             [old] => Array                 (                 )              [new] => Array                 (                 )          )      [_previous] => Array         (             [url] => http://localhost/public/         )      [login_82e5d2c56bdd0811318f0cf078b78bfc] => 2 ) 

However, after any action that will lead to a page refresh or a redirect, the session status is lost.

My config/session.php file looks like so:

<?php  return [     'driver' => env('SESSION_DRIVER', 'file'),     'lifetime' => 120,     'expire_on_close' => false,     'encrypt' => false,     'files' => storage_path('framework/sessions'),     'connection' => null,     'table' => 'sessions',     'lottery' => [2, 100],     'cookie' => 'laravel_session',     'path' => '/',     'domain' => null,     'secure' => false,  ]; 

The locally stored file for the session can be written and read.

I've tried using database drive instead of file. Same thing happens the [login_xx] => 2 key/value is lost and I'm logged out.

Since the Session:: is not completely reset I'm suspecting that I'm not logging in the user properly or simply doing something that I shouldn't be doing somewhere.

like image 658
Andrei Avatar asked Jun 11 '15 00:06

Andrei


People also ask

How to set session expiration time in Laravel?

In Session.php set lifetime to your desired time for session expiration. Also in upgraded versions of laravel session lifetime is not set hard anymore, instead it takes from .env file. You can try below configurations. In your config/session.php

How to add sessiondatacheckmiddleware in Laravel?

Create a file called SessionDataCheckMiddleware.php in App\Http\Middleware location and copy past below code. Now open \app\Http\Kernel.php file, add our custom “SessionDataCheckMiddleware” middleware

How do I log out a session without changing server configuration?

Without making any changes to server configurations we will use last activity time to log out the user. Open your config/session.php and specify the number of minutes that you wish the session to be allowed to remain idle before it expires.

Why is my Auth session not expiring after browser Close?

I found a potential answer to your issue here: Laravel - Auth Session not expiring after browser close It sounds like it could be a chrome problem. Try your solution on firefox to see if it is a chrome issue. Have a read of that thread i linked. Show activity on this post. Please make sure to check with your configurations.


2 Answers

I faced similar issue, I simply called:

Session::save(); 

after any add/update/delete to Session storage. So it looked like:

$id = Input::get('id'); Session::forget('cart.' .$id); Session::save(); 
like image 191
Kalpesh Panchal Avatar answered Sep 24 '22 06:09

Kalpesh Panchal


I had the same issue. Once I removed the various combinations of dd() and print_r() I was using to dump responses for testing purposes and allowed the method to complete and fully render the view, the issue went away and sessions persisted.

like image 31
supernifty Avatar answered Sep 25 '22 06:09

supernifty