[英]TokenMismatchException in VerifyCsrfToken.php line 67: Laravel 5.2
Can someone explain me why I get sometimes this error: 有人可以解释一下为什么有时会出现此错误:
TokenMismatchException in VerifyCsrfToken.php line 67:
in VerifyCsrfToken.php line 67
at VerifyCsrfToken->handle(object(Request), object(Closure))
at call_user_func_array(array(object(VerifyCsrfToken), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
at ShareErrorsFromSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(ShareErrorsFromSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
at StartSession->handle(object(Request), object(Closure))
at call_user_func_array(array(object(StartSession), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
at Pipeline->Illuminate\Pipeline\{closure}(object(Request))
at call_user_func(object(Closure), object(Request)) in Pipeline.php line 32
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
at AddQueuedCookiesToResponse->handle(object(Request), object(Closure))
at call_user_func_array(array(object(AddQueuedCookiesToResponse), 'handle'), array(object(Request), object(Closure))) in Pipeline.php line 136
Sometimes it works fine. 有时效果很好。 I am using middleware in the controllers like this:
我在像这样的控制器中使用中间件:
$this->middleware(['admin', 'user'], ['only' => [
'show',
]]);
$this->middleware('admin', ['only' => [
'index',
'store',
'create',
]]);
I don't have any middleware in route.php, I tried using {!! 我在route.php中没有任何中间件,我尝试使用{!! csrf_field() !!} and/or {!!
csrf_field()!!}和/或{!! csrf_token() !!} inside my forms but I still get the error sometimes.
csrf_token()!!}在我的表单中,但有时还是会出现错误。
This is an example of the log in form 这是登录表单的示例
{!! Form::open(['route'=>'log.store', 'method'=>'POST']) !!}
{!! csrf_field() !!}
{!! csrf_token() !!}
{!!Form::text('user',null,array('placeholder' => 'user'))!!}
{!!Form::password('password',array('placeholder' => 'Password'))!!}<br>
{!!Form::select('logType', [
'A' => 'A',
'E' => 'E',
])!!}
{!!Form::submit('Log in',['class'=>'button'])!!}
{!!Form::close()!!}
https://laravelcollective.com/docs/5.2/html https://laravelcollective.com/docs/5.2/html
If you use the Form::open or Form::model method with POST, PUT or DELETE the CSRF token used by Laravel for CSRF protection will be added to your forms as a hidden field automatically. 如果您对POST,PUT或DELETE使用Form :: open或Form :: model方法,则Laravel用于CSRF保护的CSRF令牌将自动作为隐藏字段添加到您的表单中。
-- -
{!! Form::open(['route'=>'log.store', 'method'=>'POST']) !!} {!! csrf_field() !!} {!! csrf_token() !!} User'))!!}
try changing to 尝试更改为
{!! Form::open(['route'=>'log.store', 'method'=>'POST']) !!}
in the snippet you provide the token is rendered 3 times, just inspect the HTML. 在您提供的代码段中,令牌被渲染了3次,只需检查HTML。 use only the
Form::open
. 仅使用
Form::open
。 calling and rendering multiple times might change the token and the final rendered element overrides the previous but in session it might not have changed. 多次调用和渲染可能会更改令牌,而最终渲染的元素将覆盖前一个元素,但在会话中可能未更改。
Also avoid sending request too often as an mismatch might occur. 另外,还应避免过于频繁地发送请求,否则可能会导致不匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.