TokenMismatchException in VerifyCsrfToken.php line

2019-09-19 05:30发布

I am trying to submit a form using curl post method.I have set csrf token properly to the form.

<form  action="{{URL::route('the-route')}}"   method="post"><input name="_token" type="hidden" value="{{csrf_token()}}">

But it shows TokenMismatchException in VerifyCsrfToken.php line 46:.I am stuck on it using shared hosting, i include my larvel 5 project in myapp folder and laravel public folder in public_html

As a test I have also tried as some other posts suggested commenting out App\Http\Middleware\VerifyCsrfToken, in app/Http/kernal.php to see what would happen. After doing this every time I submit a form I get a message which says redirecting to: /auth/login or /auth/register depending on where I came from with no success.

3条回答
劳资没心,怎么记你
2楼-- · 2019-09-19 05:41

I found the best solution for me. What you need to do is:

Go to -> app -> Exceptions -> Handler.php

and copy and paste this in you render method:

public function render($request, Exception $e)
{
if ($e instanceof TokenMismatchException) {
    Auth::logout();
    return redirect('login')->withErrors('Sorry, your session has expired.     Please login again.');
  }

return parent::render($request, $e);
}

This will handle the TokenMismatchException for you and display a friendly message on the page :). Hope it helps anyone out there!

查看更多
女痞
3楼-- · 2019-09-19 05:41
I just got it working removing the line:

'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken'

from /app/Http/Resquests/Kernel.php

thank @Mahmoud Nassar

查看更多
Emotional °昔
4楼-- · 2019-09-19 05:57

I solved my issue:

in my case i remove'Illuminate\Foundation\Http\Middleware\VerifyCsrfToken' from app/Http/kernal.php but it's not work but this is best solution of TokenMismatchException in VerifyCsrfToken.php line 46

i hosted laravel 5 in a shared hosting, As a test I have also tried as some other posts suggested commenting out change my folder structure create a folder in outside public_htmland add files in that folder then put public(laravel) folder in public_html After doing this every time I submit a form I get a message which says redirecting to: /auth/login or /auth/register depending on where I came from with no success.

so i delete all files and folders include my app and laravel

last i create laravel 5 in public_html and Add following code in .htaccess (if not exist create a .htaccess on laravel root directory)

 RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]

this .htaccess help you to remove public from url

查看更多
登录 后发表回答