Laravel 5.4 TokenMismatchException in VerifyCsrfTo

2019-04-15 11:58发布

问题:

I know it's one of the most asked question around here, but I really read everything on the first 10 pages results on google and nothing solved my problem.

On a fresh installation of laravel 5.4 , generated the auth controller, views, etc. via php artisan make:auth when I try to register (or login) in some device I get the following error:

TokenMismatchException in VerifyCsrfToken.php line 68

I'm running this application in local on debian 8.7 and php7 (could it be related to the problem in some way ?) From my pc, iphone 7, another windows 10 desktop it works perfectly, but on a Huawei p8 lite, ipad 2 and lubuntu (quite old version) I get that error. So it's really weird because it just happends with some device (no matter whaht O.S. no matter what browser).

I can already tell you that I tried grant permissions to sessions folder, change session_domain, clearing cache (but as I said I'm trying this on a fresh installation), change {{ csrf_token }} to {!! csrf_token !!} , etc.

Just to give you more information about my system, this is the php version i'm using:

php --version
PHP 7.0.17-1~dotdeb+8.1 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.17-1~dotdeb+8.1, Copyright (c) 1999-2017, by Zend 
Technologies

I'm really stuck on this for days. Got no more ideas.

回答1:

I'm assuming you added $this->middleware('auth'); inside the constructor of your controller to get the authentication working. Add the following at the top as well, under your login/register forms, if you are using {!! Form::someElement !!}:

{!! csrf_field() !!}

Or if you are using input tags inside your forms, just add this just after the <form> tag:

<input type="hidden" name="_token" value="{{ csrf_token() }}">