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

SlamDalius's avatar

Losing session when using database driver

Hey, has anyone had an issue, where session is lost after first redirect after successful login? This only happens when using database for storing session, if I use file - no issues. Also, I can see a record in a database after login with correct user id, but after redirect, next record is null. Any help is appreciated!

0 likes
4 replies
nagavinod424's avatar

@slamdalius can you be more clear there, it think the issue may be that you are unsetting the session after you have stored in db it is reflecting while you are trying to redirect.....

shez1983's avatar

@NAGAVINOD424 - he clearly says it works when he uses file.. so he cant be unsetting the session..

OP: i have not yet used DB as a session store so cant comment but why do you think DB is a good (or better?) choice than a File?

SlamDalius's avatar

@shez1983 idea is to have multiple instances in a cluster controlled by a load balancer. It means that every instance will have separate session files and a user will lose his session every time load balancer passes him to another instance. If session was stored in a database, all instances could act as one - read and write same database.

shez1983's avatar

@slamdalius ok that makes sense BUT what you could do is have a redis instance on one server (separate server) and then connect to it via all your instances... Redis is probably faster, more efficient and build for session.. just an idea

is your encryption key in you .env same for ALL instances? thats all i can think of..

1 like

Please or to participate in this conversation.