Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Security and hardening

Laravel answers a form or AJAX call with 419 Page Expired

Error419 | Page Expired

Short answer

Laravel could not match the CSRF token in your request to the one stored in the visitor's session. Either the request carried no token, or the session it belongs to was not found. Add @csrf or the token header, make sure the session is saved and its cookie comes back, and never cache pages that contain forms. Do not switch CSRF off.

  • Applies to Laravel 12 and 13, PHP 8.4, session drivers database, file and redis
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find out whether the request carried a token and whether the session cookie came back. Open DevTools, Network tab, the failing request: look for the _token form field (or an X-CSRF-TOKEN or X-XSRF-TOKEN header) and for the session cookie in the request headers. Then check what the server hands out.

    # does a fresh visit receive a session cookie and an XSRF-TOKEN cookie?
    curl -s -o /dev/null -D - -c /dev/stdout https://example.com/login | grep -iE "set-cookie|laravel|xsrf"
  2. 2Match what you see, then jump to its fix

    1. 1The failing request has no _token field and no X-CSRF-TOKEN or X-XSRF-TOKEN header.The form or the AJAX call sends no tokenFix
    2. 2Every request gets a new session cookie value, or storage/logs shows a database or permission error, or the error appears only on some servers behind a load balancer.The session cannot be stored, or is stored somewhere the next request does not lookFix
    3. 3The cookie is set (a Set-Cookie header is visible) but is missing from the next request, or DevTools shows it as blocked; typical after moving to HTTPS, a new domain or an iframe.The browser does not send the session cookie backFix
    4. 4The first submit after a long pause, or after login or logout in another tab, fails; the page source shows the same token for different visitors; curl shows Age or X-Cache headers on the form page.A cached page or an expired session holds an old tokenFix
    5. 5Only calls from another service (Stripe, a payment gateway, a mobile app, a script) get 419, and the route lives in routes/web.php.A webhook or outside client posts to a web routeFix

What does the error look like?

  • 419 | Page Expired
  • 419 Page Expired (a POST, PUT, PATCH or DELETE request)
  • CSRF token mismatch.
  • message: "CSRF token mismatch." in a JSON response to an AJAX or fetch call

Cause 1 of 5

The form or the AJAX call sends no token

1Check

Laravel checks every POST, PUT, PATCH and DELETE request that goes through the web middleware group for a token in the request input or in a header. A hand-written form, a Blade file without the directive, or a JS call without the header is enough.

curl -s https://example.com/profile | grep -iE "name=.?_token|csrf-token"
grep -rn "<form" resources/views | grep -iv "csrf" | head

2Fix

Put @csrf in every form that is not a GET form. For fetch or axios calls, put the token in a meta tag and send it in the X-CSRF-TOKEN header. Axios from the default Laravel scaffolding already sends the X-XSRF-TOKEN header from the XSRF-TOKEN cookie on same-origin requests.

{{-- form.blade.php --}}
<form method="POST" action="/profile">
    @csrf
    ...
</form>

{{-- layout head --}}
<meta name="csrf-token" content="{{ csrf_token() }}">

// fetch
await fetch('/profile', {
  method: 'POST',
  headers: {
    'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ name: 'Ada' }),
})

3Verify

The request in the Network tab shows the token, and the response is the page you expect instead of 419.

curl -s https://example.com/profile | grep -ic "name=.\?_token"

Confirmed by laravel.com

Did this fix it?

Cause 2 of 5

The session cannot be stored, or is stored somewhere the next request does not look

1Check

The token lives in the session. The default driver is database, which needs a sessions table; the file driver writes to storage/framework/sessions, which the web server's user must be able to write to; a file driver on several servers gives each server its own sessions.

grep -E "^SESSION_" .env
php artisan migrate:status | grep -i session
ls -ld storage/framework/sessions
tail -n 20 storage/logs/laravel.log

2Fix

Create the missing table, fix ownership of the writable folders, or move to a shared driver such as redis or the database when more than one server answers. Clear a cached config after changing .env.

php artisan make:session-table
php artisan migrate

sudo chown -R www-data:www-data storage bootstrap/cache
sudo chmod -R ug+rwX storage bootstrap/cache

php artisan config:clear

3Verify

Two requests in a row send and receive the same session cookie value, and a form post succeeds.

curl -s -c jar.txt -o /dev/null https://example.com/login && curl -s -b jar.txt -c jar.txt -o /dev/null https://example.com/login && cat jar.txt

Confirmed by laravel.com

Did this fix it?

Cause 3 of 5

The browser does not send the session cookie back

1Check

SESSION_SECURE_COOKIE=true makes the browser return the cookie only over HTTPS, so it is lost on a plain http address. SESSION_DOMAIN must match the host being visited; leaving it unset ties the cookie to the current host. SESSION_SAME_SITE defaults to lax, which blocks the cookie in cross-site embeds and POSTs from other sites.

grep -E "^(APP_URL|SESSION_DOMAIN|SESSION_SECURE_COOKIE|SESSION_SAME_SITE)=" .env
curl -sI https://example.com/login | grep -i "^set-cookie"

2Fix

Make the settings match how the site is reached. Use HTTPS with SESSION_SECURE_COOKIE=true in production; leave SESSION_DOMAIN unset unless subdomains must share a session, and then set it to the shared parent domain. If the app is truly embedded cross-site, SameSite=none works only together with a secure cookie.

# .env, production on HTTPS
APP_URL=https://example.com
SESSION_SECURE_COOKIE=true
SESSION_SAME_SITE=lax
# SESSION_DOMAIN=.example.com   # only when subdomains share the login

php artisan config:clear

3Verify

The session cookie appears in the request headers of the next page and the post goes through.

curl -sI https://example.com/login | grep -iE "^set-cookie" | grep -iE "secure|samesite"

Confirmed by github.com

Did this fix it?

Cause 4 of 5

A cached page or an expired session holds an old token

1Check

A token belongs to one session and changes when the session is regenerated. A page cached by a CDN, a page-cache plugin or the browser's back button can carry a token that is no longer valid, and sessions end after the lifetime set in SESSION_LIFETIME (120 minutes by default).

curl -sI https://example.com/contact | grep -iE "^(cache-control|age|x-cache|cf-cache-status)"
grep -E "^SESSION_LIFETIME" .env

2Fix

Do not cache pages that include forms or tokens: exclude them in the CDN or page cache and send a no-store header. For long-lived forms, refresh the token with a small request, or catch the 419 and ask the user to reload instead of losing their input.

// routes/web.php
Route::get('/contact', fn () => response()
    ->view('contact')
    ->header('Cache-Control', 'no-store, private'));

3Verify

The page response carries no-store and no cache hit headers, and different visitors get different tokens.

curl -sI https://example.com/contact | grep -i "^cache-control"

Confirmed by laravel.com

Did this fix it?

Cause 5 of 5

A webhook or outside client posts to a web route

1Check

Those callers cannot know a visitor's session token. Routes in routes/web.php get CSRF checking and sessions; outside callers belong on routes that do not use them and that are authenticated another way (API tokens, signed webhooks).

php artisan route:list --path=webhook
grep -n "webhook" routes/web.php

2Fix

Move the endpoint to routes/api.php, or exclude only that URI from the check. Verify the caller another way, for example by the provider's signature. Never exclude everything. Laravel 12 uses validateCsrfTokens, Laravel 13 uses preventRequestForgery.

// bootstrap/app.php, Laravel 12
->withMiddleware(function (Middleware $middleware): void {
    $middleware->validateCsrfTokens(except: ['stripe/*']);
})

// bootstrap/app.php, Laravel 13
->withMiddleware(function (Middleware $middleware): void {
    $middleware->preventRequestForgery(except: ['stripe/*']);
})

3Verify

The provider's test delivery gets a 2xx response, while a normal form post without a token still gets 419.

curl -s -o /dev/null -w "%{http_code}\n" -X POST https://example.com/profile   # expect 419

Confirmed by laravel.com

Did this fix it?

How do I stop it coming back?

  • Use @csrf in every non-GET form and keep the csrf-token meta tag in the layout for JS calls.
  • Run production on HTTPS with a secure session cookie, and keep sessions in a store every app server can reach.
  • Keep pages with forms out of every cache layer.
  • Exclude specific webhook URIs, never the whole application, and verify those callers some other way.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Security

An HTTPS page is blocked from loading an insecure http:// resource

Mixed Content: The page at 'https://example.com/' was loaded over HTTPS, but attempted to connect to the insecure endpoint 'http://api.example.com/items'. This request has been blocked.

Your page came over HTTPS but something on it points at an http:// address, and the browser refuses the risky kinds (scripts, styles, frames, fetch calls).

4 causes, each with a fix

Security

The browser warns about a SameSite cookie set from PHP

A cookie with SameSite=None is rejected because it lacks the Secure attribute

A cookie sent without a SameSite attribute gets the browser's default, and a cookie marked None is dropped unless it is also Secure.

4 causes, each with a fix

Security

Is my PHP application affected by PHP2Shell, and how do I mitigate it?

A scanner or advisory feed reports CVE-2026-109619 (RCE via stale global state) on your server

You are exposed when the PHP serving web requests is older than 8.2.35, 8.3.36, 8.4.27 or 8.5.12 on its branch, or when a distribution package is older than its vendor's fixed build.

6 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.