简体   繁体   中英

Laravel - Sessions not persisting with database driver

I'm using Laravel 5.1 and i've been using the default file driver to store sessions in my project until now.

Now for some reasons i decided to switch to the database driver so, according to the official documentation , I changed the following line in my config/session.php

'driver' => 'database'

I created the sessions table in my database and when i start browsing my website now i can see some data inside the new table:

我的会话表的屏幕截图

but i can't get any data from the session now! These are the steps I'm following:

i store my data into session:

session( [ 'mykey' => 'mydata' ] );

when i move to another page, I try to get my data back.

$data = session( 'mykey' ); // this is always null

The problem is the output will be always null. While if I come back using the default file session driver, everything works as expected and I can get my stored value from session.

The other values in my config/session.php are the default ones:

'lifetime' => 120,
'expire_on_close' => false,
'encrypt' => false,
'connection' => null,
'table' => 'sessions',
'lottery' => [2, 100],
'path' => '/',
'domain' => null,
'secure' => false,

Am i missing something in the configuration?

Laravel has a problem for session persisting https://github.com/laravel/framework/issues/6506

You have to do Session::save() after every session add,update or delete like below

$id = Input::get('id');
Session::forget('cart.' .$id);
Session::save();

After hours upon hours of research, as this has popped up for me multiple times over the past few years, I've determined that there is a intermittent issue that can occur when installing laravel with composer. The laravel team has gotten no shortage of reports about this but their insistence that they cannot replicate it has left the problem ignored since version 4 maybe even longer.

While there are dozens of posts with suggestions on how to fix this, the surest way is to simply spin up a new instance the moment you realize sessions aren't behaving. I assume it has something to do with some of the files or encoding getting mangled. A new install resolves this for me every time.

I probably just found the solution to my issue. I'm writing it as an answer so if someone is having the same issue, this might help.

Basically I was saving TOO MANY things on session (an entire collection of models). And this is of course a really bad idea.

This was happening in a search page, so depending on how many results I was getting, sometimes the user was even been kicked out from his logged session, because i was saving all the results in the session.

Refactoring the code by storing only the ids of the found models and retrieving them at runtime was the right thing to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM