How to solve the page expired (419) error in Laravel?

Laravel 419 Page Expired Error

Infinitbility
3 min readDec 9, 2019

Hey Guys,

To fix 419 page expired error in laravel, you have to use the CSRF token carefully in your project. below we have discussed cases when laravel show page expired error and their appropriate solution.

So, let’s start with what is CSRF token?

What is CSRF?

Cross-site request forgeries are a type of malicious exploit whereby unauthorized commands are performed on behalf of an authenticated user. Thankfully, Laravel makes it easy to protect your application from cross-site request forgery (CSRF) attacks. — Laravel

Table of content

  1. Page expired 419 error on Form
  2. Page expired 419 error on Ajax
  3. Remove CSRF protection on specific URL

Condition 1

If you are getting an error after submitting the form then you need to add the CSRF field in your form.

<form method="POST" action="/profile">    @csrf <!-- add csrf field on your form -->    ...</form>

Condition 2

If you are getting an error after calling the AJAX then you need to add a header like below.

  • In your head tag
<meta name="csrf-token" content="{{ csrf_token() }}">
  • In Your Script tag
$.ajaxSetup({   headers: {      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')   }});

Condition 3

Note: disable CSRF protection use only for webhooks

disable CSRF protection field for routes group or specific routes

open file VerifyCsrfToken.php on your project

dir — App\Http\Middleware\VerifyCsrfToken.php

<?phpnamespace App\Http\Middleware;use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;class VerifyCsrfToken extends BaseVerifier{  protected $except = [    'payment/*', // routes group    'specific-route', // specific route  ];}

🙏 Please Subscribe US 🙏

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

It’s motivate us to work.

Sometimes caching issues also the reason for the (419) page expired issue to clear your application cache to follow the below article.

Original post published on infinitbility.com

More For Laravel

--

--