419 Page Expired Laravel 5.8 - After Login

I Created one Project in Laravel 5.8. In my Local Environment(PHP 7.2) its working good. when i hosted this project in to my server(PHP 7.1) using cpanel after login its return 419 Page Expired Error.

Mylogin Form Code :

<form method="POST" action="{{ route('login') }}" id="login-form">
    @csrf
    <div class="form-group">
      <label for="username">{{ __('Username / Email Address') }}</label>
      <input type="text" class="form-control{{ $errors->has('username') ? ' is-invalid' : '' }} boxed" name="username" id="username" value="{{ old('username') }}" required autofocus>
    </div>
    @if ($errors->has('email'))
       <span class="invalid-feedback" role="alert">
          <strong>{{ $errors->first('email') }}</strong>
       </span>
    @endif

    <div class="form-group">
        <label for="password">{{ __('Password') }}</label>
        <input type="password" class="form-control{{ $errors->has('password') ? ' is-invalid' : '' }} boxed" name="password" id="password" required>
    </div>
    @if ($errors->has('password'))
        <span class="invalid-feedback" role="alert">
           <strong>{{ $errors->first('password') }}</strong>
        </span>
    @endif

    <div class="form-group" style="margin-bottom: 0px; float:left;">
        @if (Route::has('password.request'))
            <a href="{{ route('password.request') }}" class="forgetpwd">
               {{ __('Forgot Your Password?') }}
            </a>
        @endif
    </div>

    <div class="form-group" style="text-align: center;">

        <button type="submit" class="btn btn-warning" style="padding:0.5rem 1.8rem;">Login</button>
    </div>
</form>

I cleared Cache and Cookies but, Same issue Displayed.


Solution 1:

This error occurs due to CSRF token verification failure, misconfigured cache, permissions, improper session settings. This error shows up when a user submits a post request. You can fix it by doing belows:

  1. CSRF token verification failure The most common reason for the 419 error is CSRF token failure. Cross-site request forgery is a unique, encrypted value generated by the server. This is included in the HTTP request of the client. Later the server verifies it. If this fails, it leads to session expired error. So, you check the CSRF setting in the Laravel config.

  2. Session expired error due to cache Sometimes, the cache can also lead to session expired error in front-end. This can be both the server cache and browser cache. So, clear the server cache using php artisan cache:clear.

  3. Laravel file and folder permissions Similarly, improper file or folder permission can also lead to errors. Usually, web servers need write-permissions on the Laravel folders storage and vendor. Also, session storage needs write-permission. So, give permissions as,

    chmod -R 755 storage

    chmod -R 755 vendor

    chmod -R 644 bootstrap/caches

Laravel session setting Last but not least, session settings can also cause a 419 error. The app/config/session.php is the session config file. Check for a few important parameters – domain and secure.

'domain' => env('SESSION_DOMAIN', null),
'secure' => env('SESSION_SECURE_COOKIE', false), // in case of cookie

These step by step approach fixes the error and make Laravel working again.

Solution 2:

In all probably, you are missing @csrf.

Just add @csrf following right after your form opening tag line. It should look like this:

<form class="singn-form" method="POST" action="{{ route('register') }}">
@csrf
....
</form>