How to solve "CSRF Token Mismatch" in Laravel
A "CSRF token mismatch" error typically indicates that the token submitted by your form does not match the token stored in the session. While this often points to a frontend configuration issue, such as missing meta tags or headers, it can also stem from backend state management problems. Specifically, when an application behaves perfectly locally but fails immediately upon deployment to a production domain, the root cause usually lies in how the session driver interacts with the server's filesystem and permissions.
The scenario described involves a local environment working correctly while the hosted version generates multiple sessions for a single user. Locally, developers often rely on the default database or redis drivers, which are robust and consistent across environments. However, switching to the file driver in the .env file to debug session contents exposes a critical fragility: the filesystem-based session storage is sensitive to write permissions and directory locking mechanisms.
If you observe that multiple sessions are being generated for one user, it suggests that the application cannot update an existing session file, forcing Laravel to create a new one instead. This creates a race condition or a stale state where the token generated in the browser (attached to the new session) does not match the token expected by the controller logic, which might be reading from a cached or partially written file. The fact that SESSION_LIFETIME is set to 120 minutes is likely irrelevant here; the issue is not about expiration but about write failure.
When debugging sessions in storage/framework/sessions, you may notice files that appear empty or corrupted upon inspection. If no token exists in these files, it often means the session file was never successfully written before the request concluded. This happens frequently when the web server user (e.g., www-data on Ubuntu) lacks write permissions to the storage/framework/sessions directory, or when that specific subdirectory has restrictive umask settings inherited from the parent folder.
To resolve this without reverting to a database driver immediately, you must ensure the filesystem is writable. Run the following command to set appropriate permissions recursively:
chmod -R 775 storage/framework/sessions
chown -R www-data:www-data storage/framework/sessions
If your web server user differs from www-data, adjust the ownership accordingly. It is also crucial to ensure that no other processes are locking the session directory, though this is rare with the default file driver unless a previous script failed mid-write.
Another potential pitfall involves the SESSION_SECURE or SESSION_DOMAIN settings if you are on HTTPS. If the domain configuration forces secure cookies but the server environment does not match expectations, the browser might reject the cookie entirely, causing Laravel to generate a new session ID for every request. This effectively invalidates any stored token because each request hits a fresh session with no history. Check your .env file to ensure SESSION_DOMAIN is set correctly (often left empty or set to your subdomain) and that SESSION_SECURE matches your production protocol.
Finally, consider the caching layer. If you are using a caching driver for sessions but misconfigured it, or if you have a middleware clearing sessions prematurely, tokens will be lost. Ensure that SESSION_DRIVER=file is explicitly set in production if you intend to use filesystem storage, and verify that no middleware is interfering with session regeneration logic.
For complex deployments where filesystem management becomes a bottleneck, migrating to a database or Redis driver provides better consistency and isolation. However, fixing the underlying permission issues on the file system often resolves the mismatch without needing architectural changes.