Error "The page has expired due to inactivity" resolved

When working on live server, occasionally we faced the error "The page has expired due to inactivity Please refresh and try again" when submitting the form in POST route. Mostly the issue happened when we forget to add Laravel's csrf-token field.

The _token field is security purpose to prevent brute force attack on form. So in that cases it can be resolved by adding hidden _token field in form.

<form method="post" action="{{ route('login') }}">
    @csrf

    // or input field
    <input type="hidden" name="_token" value="{{ csrf_token() }}" />
    ...
</form>

If you are using Ajax form request, simply add <meta> tag in <head>

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

Then add this token to every request header.

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});

If you have already added _token field to your form, make sure your storage_path is writable, this is the folder where all sessions are stored. You can verify if session is writable with The can be verified with is_writable(config('session.files'))

In that cases you need to change storage folder permission.

sudo chmod -R 775 storage/

Lastly, sometimes you made a change and stil got the error, In these cases you might need to clear cache and compiled views.

php artisan cache:clear && php artisan view:clear

The above tricks should fix the error. Please follow us on Twitter and like us on Facebook.

Tags: