Home » Php » laravel 5 – TokenMismatchException in VerifyCsrfToken.php line 67 only on live server

laravel 5 – TokenMismatchException in VerifyCsrfToken.php line 67 only on live server

Posted by: admin July 12, 2020 Leave a comment

Questions:

I’m getting this error when I try to use auth in Laravel 5.2. That had happened after moving to live server. On my local server everything works fine.

Token inside form and is Session:token() are the same.

UPD: I put dd($request) in laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php

and the _token value NOT matches _token input that is in form.

I tried:

  • Disable CSRF for auth routes, added routes in except array in
    Midd/VerifyCsrfToken.php.

    • Set up 777 rights to /storage /bootstrap folders.
    • Used {!! csrf_field() !!} and name=”_token” value=”{{ csrf_token() }}”>.
    • Changed domain in config/session.php.
    • php artisan key:generate.
    • Re-installed Auth.
      Also routes where I have middleware = auth.basic I’m getting “Invalid credentials” even without asking to log in at first.

On my local server it works fine.

How to&Answers:

Does your handler look like this?

<?php 
...
public function handle( $request, Closure $next )
{
    if (
        $this->isReading($request) ||
        //$this->runningUnitTests() ||
        $this->shouldPassThrough($request) ||
        $this->tokensMatch($request)
    ) {
        return $this->addCookieToResponse($request, $next($request));
    }

    // redirect the user back to the last page and show error
    return Redirect::back()->withErrors('Sorry, we could not verify your request. Please try again. (You waited too long to submit your form)');
}

Answer:

Try it it will solve your problem, It is because of http and https conflict.

Goto “session.php” file change this:

'secure' => env('SESSION_SECURE_COOKIE', false),

to this:

'secure' => true,