Laravel keeps logging users out: session driver, domain and cookie fixes

Laravel keeps logging users out: session driver, domain and cookie fixes

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Users report being logged out at random - sometimes mid-click, sometimes after minutes. The session system is simple, but it crosses your framework, web server, cookies and infrastructure, so the failure can live in five different places. Here is the checklist in the order that finds the cause fastest.

1. Check your session driver against your infrastructure

The SESSION_DRIVER in .env must match how you deploy. The file driver stores sessions on local disk - fine on one server, broken on two. Behind a load balancer without sticky sessions, each request lands on a different machine, and only one of them knows your session. The user appears logged out on every other request.

Fix: switch to a shared store for anything beyond one server.

SESSION_DRIVER=redis    # or database, or memcached

Rule of thumb: more than one web server means redis or database. No exceptions for "the balancer is usually sticky".

2. Stop rotating APP_KEY

Laravel encrypts session cookies with APP_KEY. Run php artisan key:generate on a live app - or deploy with a different .env - and every existing cookie becomes unreadable. Every user is logged out instantly, all at once.

If your whole user base was logged out in one moment, check the git history or deploy log for a changed APP_KEY. Generate the key once, treat it as a secret, and never regenerate it on an app with live sessions.

3. Fix SESSION_DOMAIN

SESSION_DOMAIN controls which hosts receive the cookie. Set it to app.example.com and the cookie will not be sent to www.example.com; set it to null and it defaults to the exact host only. A user moving between subdomains - or between the apex and www - looks logged out on every hop.

SESSION_DOMAIN=.example.com   # leading dot: cookie sent to all subdomains

Match it to where your app actually lives, and redirect the apex to www (or back) so users stick to one host.

4. HTTPS and the secure cookie flag

With SESSION_SECURE_COOKIE=true, the browser only sends the session cookie over HTTPS. If any page or redirect is still plain HTTP - a mixed setup behind a proxy that terminates TLS is the classic case - the cookie vanishes on those requests and the user looks logged out.

Check config/session.php and the secure flag, make sure the whole site is HTTPS, and confirm Laravel knows it is behind a TLS-terminating proxy via TrustProxies. Otherwise secure cookies may never be set at all.

5. Lifetime, lotteries and cache clears

Two quieter causes:

Session lifetime. SESSION_LIFETIME is in minutes (default 120) and counts idle time, not login age. Combined with 'expire_on_close' => true it also ends at browser close. For "remember me" behavior, use the remember-me cookie - do not just raise the lifetime to absurd values.

Clearing the wrong cache. If sessions use Redis and your deploy runs redis-cli FLUSHALL or points cache and sessions at the same logical database and flushes it, you log everyone out. Give sessions their own Redis database and keep cache:clear away from it.

Summary

Random logouts on one server: check APP_KEY, SESSION_DOMAIN and the secure flag. Random logouts behind a balancer: the driver is file and must become redis or database. Everyone logged out at once: someone rotated the key or flushed the session store. Match the symptom to the cause and the fix is usually one line.

Laravel Sessions Authentication Troubleshooting

Our network