Technical article
Laravel session security on shared hosting
Protect Laravel sessions with restricted storage, appropriate cookie attributes and verified login and logout transitions.
- Security & privacy
- Laravel
- session
- security
- shared
- hosting

In this article
Protect a Laravel session in three places: its browser cookie, storage on the host and the transition from anonymous to authenticated use. Shared hosting determines the available storage and proxy configuration.
Choose storage the host supports
File sessions can suit one server when storage/framework/sessions remains outside the public webroot and only the application account can access it. Database sessions require the correct table and permissions. Existing sessions do not automatically migrate when changing drivers; plan the effect on signed-in users.
Cookie protection differs from encryption
SESSION_DRIVER=file
SESSION_ENCRYPT=true
SESSION_SECURE_COOKIE=true
SESSION_HTTP_ONLY=true
SESSION_SAME_SITE=laxIllustrative production configuration for a single HTTPS host. SESSION_ENCRYPT encrypts stored session data; Secure restricts cookie transmission to HTTPS and HttpOnly restricts JavaScript access. Lax is a starting point to check against external login or payment flows. Use a separate cookie name per application and a narrow cookie domain.
Check the authentication transition
Laravel starter kits and Fortify regenerate the session ID during authentication. A custom login flow needs that transition explicitly. Logout should combine signing out, invalidating the session and regenerating the CSRF token. Inspect the existing authentication code before adding duplicate behaviour.
Illustrative check behind a proxy
- Inspect the actual HTTPS response and Set-Cookie with test account A without recording its value in a report.
- Sign in and sign out. The old session must no longer open a protected page.
- Repeat with account B in a separate browser profile. Data and caches must never leak between accounts.
- Check the actual trusted proxy addresses and forwarded HTTPS information. Never trust an arbitrary client header.
A configuration file does not prove browser behaviour. Repeat these checks after changing hosts, proxies or cookie domains.
Code examples are illustrative. They have not been executed in a complete Laravel project; adapt imports, relationships, routes and configuration to your project and test actual behaviour.
Last updated:


