Skip to content
FR
  1. LuzineDebug
  2. PHP & Laravel
  3. 419 | Page Expired
PHP & Laravel Beginner ≈ 10 min to fix

Fix "419 Page Expired" in Laravel (CSRF token mismatch)

Error message
419 | Page Expired
  • {"message": "CSRF token mismatch."}
  • Illuminate\Session\TokenMismatchException: CSRF token mismatch.
  • POST https://example.com/login 419 (unknown status)
  • Request failed with status code 419
In short

Laravel rejected a POST, PUT, PATCH or DELETE request because the CSRF token is missing or does not match the one stored in the session. Add @csrf to your forms, send the X-CSRF-TOKEN header in AJAX calls, and check that the session cookie is kept.

Be the first to confirm this fix

What this error means

To block CSRF attacks, Laravel generates a secret token per session and requires it to be sent back with every request that changes data. The ValidateCsrfToken middleware (VerifyCsrfToken before Laravel 11) compares the token it receives, through the _token field, the X-CSRF-TOKEN header or X-XSRF-TOKEN, with the one in the session. If they differ, it throws a TokenMismatchException, rendered as a 419 "Page Expired" page or as {"message": "CSRF token mismatch."} for a JSON request. The token may be missing, but the session itself may also have expired or never been stored.

Common causes

  1. Missing @csrf in a Blade form that sends a POST request.
  2. Expired session: the page stayed open longer than SESSION_LIFETIME (120 minutes by default), or sessions were cleared.
  3. AJAX request without a CSRF header: fetch does not send the token on its own, unlike axios which reads the XSRF-TOKEN cookie on the same domain.
  4. Session cookie not stored: wrong SESSION_DOMAIN, SESSION_SECURE_COOKIE=true over HTTP, storage/framework/sessions not writable, or a changed APP_KEY.
  5. Misconfigured SPA with Sanctum: missing call to /sanctum/csrf-cookie, or front-end domain missing from SANCTUM_STATEFUL_DOMAINS.

Step-by-step fix

  1. Add @csrf to the form

    Every Blade form that is not GET must contain @csrf, which outputs a hidden _token field. For PUT, PATCH or DELETE, add @method too.

    php
    <form method="POST" action="{{ route('profile.update') }}">
        @csrf
        @method('PUT')
        <input type="text" name="name" value="{{ old('name', $user->name) }}">
        <button type="submit">Save</button>
    </form>
  2. Send the token in AJAX requests

    Put <meta name="csrf-token" content="{{ csrf_token() }}"> in your layout’s <head>, then read it and send it in the X-CSRF-TOKEN header.

    javascript
    const token = document.querySelector('meta[name="csrf-token"]').content;
    
    await fetch('/profile', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        Accept: 'application/json',
        'X-CSRF-TOKEN': token,
      },
      body: JSON.stringify({ name: 'Alice' }),
    });
  3. Check that the session cookie is kept

    In DevTools (Application > Cookies), the session cookie (laravel_session by default) and XSRF-TOKEN must exist and stay the same between requests. If not, check .env then clear the config cache with php artisan config:clear.

    ini
    SESSION_DRIVER=database
    SESSION_LIFETIME=120
    # leave empty for the current host, or ".example.com" for subdomains
    SESSION_DOMAIN=
    # true only when the site is served over HTTPS
    SESSION_SECURE_COOKIE=false
  4. Make the sessions folder writable

    With SESSION_DRIVER=file, PHP must be able to write to storage. On Linux, give ownership to the web server user (often www-data) instead of using chmod 777. With SESSION_DRIVER=database, the sessions table must exist (php artisan migrate).

    bash
    sudo chown -R www-data:www-data storage bootstrap/cache
    sudo chmod -R 775 storage bootstrap/cache
  5. SPA with Sanctum: initialize the CSRF cookie

    Call /sanctum/csrf-cookie before logging in, enable cookies and the XSRF token in axios, and add the front-end domain to SANCTUM_STATEFUL_DOMAINS (for example localhost:5173). Both apps must share the same top-level domain.

    javascript
    import axios from 'axios';
    
    axios.defaults.withCredentials = true;
    axios.defaults.withXSRFToken = true;
    
    await axios.get('http://localhost:8000/sanctum/csrf-cookie');
    await axios.post('http://localhost:8000/login', { email, password });
  6. Handle session expiry gracefully

    For a session that really expired, send the user back to the form with a message instead of the raw 419 page (Laravel 11+, in bootstrap/app.php).

    php
    use Illuminate\Foundation\Configuration\Exceptions;
    use Illuminate\Http\Request;
    use Illuminate\Session\TokenMismatchException;
    use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
    
    ->withExceptions(function (Exceptions $exceptions) {
        $exceptions->render(function (Throwable $e, Request $request) {
            $expired = $e instanceof TokenMismatchException
                || ($e instanceof HttpExceptionInterface && $e->getStatusCode() === 419);
    
            if ($expired && ! $request->expectsJson()) {
                return back()
                    ->withInput($request->except('password', '_token'))
                    ->with('error', 'Your session expired. Please try again.');
            }
        });
    })

Did this fix work for you?

Confirm it in one click: it will rise for the next developers stuck on the same error.

Keep it from coming back

  • Use a shared layout with the csrf-token meta tag and a central HTTP client that adds the header automatically.
  • Only exclude a route from CSRF checks ($middleware->validateCsrfTokens(except: ['stripe/*'])) for signed external webhooks, never for your own forms: that would reopen the door to CSRF attacks.
  • Do not cache (CDN, full-page cache) pages that contain a form: the token served would belong to another session.

Frequently asked questions

Why do I get a 419 error on the login form only in production?

Most often the session cookie is not stored: SESSION_DOMAIN or SESSION_SECURE_COOKIE do not match the real domain or protocol, or an HTTPS proxy is not declared as a trusted proxy.

Should I disable CSRF protection to fix the 419 error?

No. It stops third-party sites from sending requests on your behalf. Fix how the token or session is sent; only exclude external webhooks that have their own signature.

How do I extend the time before "Page Expired"?

Increase SESSION_LIFETIME (in minutes) in .env, then run php artisan config:clear. A very long lifetime increases the risk if a device stays logged in.

Useful tools for this error

More PHP & Laravel errors

All PHP & Laravel errors →