Laravel 419 Error When enable mod_headers for apache

surandra-parmar

May 28th, 2019 03:09 AM

When enable mod_headers for apache , Laravel gives 419 error (Sorry, your session has expired. ) for all POST requests only.When we disable mod_headers everything works properly. I need mod_headers to set headers in .htacccess file. How to fix it ?

bobbyiliev

May 28th, 2023 09:44 AM

Hi there,

Just following up on some of the old unanswered questions on the site.

The Laravel 419 error (Session expired) usually happens due to a mismatch or absence of CSRF token. CSRF is a type of attack that tricks the victim into submitting a malicious request. Laravel makes it easy to protect your application from such attacks by verifying the CSRF token in every POST, PUT, DELETE, etc. request.

Here are a few steps you might follow to debug your issue:

  1. Verify CSRF token: Ensure you have @csrf field in your form. This will generate a hidden field with CSRF token.

  2. Clear cookies and cache: Sometimes the cookies stored in your browser can cause problems. Try to clear them.

  3. Session config: Check your session configuration in config/session.php. Make sure your session domain is set correctly. Also, ensure the SESSION_DRIVER in your .env file is set to a valid driver (like file, cookie, database, etc.)

  4. Middleware: Ensure the VerifyCsrfToken middleware is active and properly configured in your app/Http/Kernel.php file.

  5. Apache mod_headers: It's unusual for mod_headers to interfere with Laravel's session or CSRF token, unless it's misconfigured. If you are setting any cookie or session-related headers using mod_headers, make sure they are correct.

If after checking all the above, you're still facing the issue, it might be helpful to see the headers that are being sent by your server when mod_headers is enabled. You can check this in the browser's dev tools network tab.

Best,

Bobby