Skip to content

Fix this error

Laravel: 419 Page Expired

Dasun Kalupahana 104 views

The error

Error

419 | Page Expired

Why it happens

Laravel rejects a POST, PUT, PATCH or DELETE request when its CSRF token is missing or does not match the one in the session. Common reasons:

  • The form has no @csrf field.
  • The page was open so long that the session expired (default 120 minutes).
  • An AJAX request does not send the X-CSRF-TOKEN header.
  • The session cookie is not saved: wrong SESSION_DOMAIN, SESSION_SECURE_COOKIE=true on plain http, or storage/framework/sessions is not writable.

How to fix it

  1. Add @csrf inside every form that posts to Laravel.
  2. For JavaScript requests, put the token in a meta tag and send it as the X-CSRF-TOKEN header (axios in a default Laravel app does this already).
  3. Check .env: SESSION_DOMAIN must match your domain (or be empty), and SESSION_SECURE_COOKIE=true only works over https.
  4. Make sure storage/ is writable, then run php artisan config:clear and try again in a fresh tab.
<form method="POST" action="/profile">
    @csrf
    ...
</form>

{{-- For fetch / axios --}}
<meta name="csrf-token" content="{{ csrf_token() }}">
<script>
    fetch('/profile', {
        method: 'POST',
        headers: { 'X-CSRF-TOKEN': document.querySelector('meta[name=csrf-token]').content },
    });
</script>
Still stuck? Send us the full error and what you tried. We add the answers here.

Learn more on the blog

Get new articles in your inbox

Practical tutorials and deep dives, about once a week. No spam, unsubscribe anytime.

Privacy policy

Add DEV Talk to your Home Screen

  1. 1 Tap the Share button in Safari's toolbar.
  2. 2 Scroll down and tap Add to Home Screen.
  3. 3 Tap Add. Open it from the Home Screen to read like an app and to turn on notifications with the bell.

We use cookies to improve your experience and analyze traffic. By continuing, you agree to our Privacy Policy.