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

martyn's avatar

Is Session::save() required each time I write to session?

Hi, I've been asking on SO as I have freshly installed Laravel 5.3, and followed the documentation on storing data in session. Here is the post for background - http://stackoverflow.com/questions/41633766/laravel-session-out-the-box-not-working/41633962

In conclusion, it seems I need to do this:

Session::put('key', md5(rand()));  // this alone won't write to session
Session::save(); // this call is required in addition to put()

So far it would seem that I need to call Session::save() after each cycle to ensure that data is written to session (file, db, etc). However, I see no mention of this in the Laravel docs on sessions. Isn't this save be done automatically at the end? Otherwise, if it is required, where should I put it in the app? Should I create middleware that calls Session::save() once the dispatch cycle is complete?

0 likes
9 replies
martyn's avatar

I've tried $request->session()->get(), Request::session()->get(), Session::get() and session() but all the same result. In my case it doesn't make any difference, and will only store the session values when I call save(). The only reason I'm using Session:: here is because I've put the code within Route::get('/', function () {...});. I'm more concerned why I have to call save() for the session values to persist when there is no mention of this in the session documentation.

3 likes
luceos's avatar

Under normal circumstances session persistence is arranged by Laravel itself. There could be reasons however why it might no longer work.

  • Amongst others unexpectedly quitting the application (I think dd for instance does that) can be a reason for this behaviour.
  • Another reason can be a misconfigured session driver.

Could you give us more information about your current environment?

2 likes
martyn's avatar

What information would you like to know?

If it helps, below is the config/session.php content:

'driver' => env('SESSION_DRIVER', 'file'),
...
'files' => storage_path('framework/sessions'),
...

My .env has the following:

...
SESSION_DRIVER=file
...

And permissions on the folders are:

$ ll /var/www/o-eco/website/storage/framework
total 24
drwxrwxr-x 1 ubuntu www-data 4096 Oct  3 02:33 ./
drwxrwxr-x 1 ubuntu www-data 4096 Oct  3 02:33 ../
drwxrwxr-x 1 ubuntu www-data 4096 Oct  3 02:33 cache/
-rw-rw-r-- 1 ubuntu www-data  103 Oct  3 02:33 .gitignore
drwxrwxr-x 1 ubuntu www-data 4096 Jan 13 10:59 sessions/
drwxrwxr-x 1 ubuntu www-data 4096 Jan 13 10:59 views/

$ ll /var/www/o-eco/website/storage/framework/sessions
total 16
drwxrwxr-x 1 ubuntu www-data 4096 Jan 13 10:59 ./
drwxrwxr-x 1 ubuntu www-data 4096 Oct  3 02:33 ../
-rw-rw-r-- 1 ubuntu www-data  258 Jan 13 10:59 5xqzOeb6f5lLKyvZIwOeonutkmluREfcaQ5owTNE
-rw-rw-r-- 1 ubuntu www-data   14 Oct  3 02:33 .gitignore

Let me know if I need to give more. I have the application running inside a vagrant box but was getting the same results when I run on the host machine.

clay's avatar

a write to the session via Session::put('key', md5(rand()));, for example, will be persisted at the end of the request cycle. I believe Laravel uses terminable middleware to save any writes to the session. So as long as sessions are configured properly and the request isn't ending prematurely, there is no reason to call save().

Also, if calling 'save' does indeed save to the session, then your sessions are most likely configured properly. So it must be a logic issue.

4 likes
Ic3MaN's avatar

I'm actually experiencing the same issue. Did you end up figuring this one out?

Ic3MaN's avatar

Okay, never mind...clay's I had to re-read clay's answer.

I was dd'ing in my controller which was prematurely terminating the request lifecycle so it never got to the save part. Makes sense now. Thanks!

dd(\Session::all());
1 like
arkid's avatar

I had the problem of writing to sessions 1st time per session working, but updates not working.

The fix was the old classic...

use Session;

Please or to participate in this conversation.