laravel 5.1 auth csrf 令牌不匹配
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32004037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel 5.1 auth csrf token mismatch
提问by Mohsen Safari
before make any judgment I read all the related questions related to my problem but none of them fixed it. so here's my problem when I use the authentication facility of laravel 5.1 and want to register a user the csrf token generate twice one when I requesting to show my register form and one when I post the form data to auth/register post route and this cause my to receive a csrf token mismatch exception. here's my register form markup
在做出任何判断之前,我阅读了与我的问题相关的所有相关问题,但没有一个解决它。所以这是我的问题,当我使用 laravel 5.1 的身份验证工具并想要注册用户时,csrf 令牌会在我请求显示我的注册表时生成两次,当我将表单数据发布到 auth/register post 路由时生成两次,这就是原因我收到 csrf 令牌不匹配异常。这是我的注册表单标记
<form method="POST" action="/auth/register" class="ui large form">
{!! csrf_field() !!}
<div class="two fields dirright alignright">
<div class="field" >
<div class="ui right icon input">
<i class="user icon"></i>
{!! Form::text(
'first_name',
Input::old('first_name'),
array(
'class' => 'dirright alignright fontfamily',
'placeholder' => '???'
)
) !!}
</div>
</div>
<div class="field" >
<div class="ui right icon input">
<i class="user icon"></i>
{!! Form::text(
'last_name',
Input::old('last_name'),
array(
'class' => 'dirright alignright fontfamily',
'placeholder' => '??? ????????'
)
) !!}
</div>
</div>
</div>
<div class="field">
<div class="ui left icon input latintext">
<i class="mail icon"></i>
{!! Form::email(
'email',
Input::old('email'),
array(
'class' => 'latintext',
'placeholder' => 'E-mail address'
)
) !!}
</div>
</div>
<div class="field">
<div class="ui left icon input latintext">
<i class="lock icon"></i>
{!! Form::password(
'password',
Input::old('password'),
array(
'class' => 'latintext',
'placeholder' => 'Password'
)
) !!}
</div>
</div>
<div class="ui fluid large primary submit button">??? ???</div>
<div class="ui error message alignright"></div>
</form>
回答by Hasan Tareque
Just add the csrf token as follows in the form :
只需在表单中添加 csrf 令牌如下:
<input type="hidden" name="_token" value="{{csrf_token()}}"/>
it worked for me.
它对我有用。
回答by Kunal Moharir
Assume that your web server has already write access to session directory, in my case 'app/storage/framework/sessions/'.
假设您的 Web 服务器已经对会话目录具有写访问权限,在我的例子中是'app/storage/framework/sessions/'。
Execute:
执行:
$ rm -f {your_web_app}/storage/framework/sessions/*
回答by Ben Chamberlin
There are several possibilities...
有几种可能...
1) If you have any spaces at all in front of your opening <?php
tag, it can cause this error (especially if you're using AJAX). So just double-check to make sure that there's nothing before <?php
in your files.
1) 如果您的开始<?php
标签前面有任何空格,则可能会导致此错误(尤其是在您使用 AJAX 时)。因此,只需仔细检查以确保<?php
您的文件中之前没有任何内容。
2) If you're trying to submit this form data via AJAX, the docs suggest passing the CSRF token like so:
2)如果您尝试通过 AJAX 提交此表单数据,文档建议像这样传递 CSRF 令牌:
Add this meta tag to your <head>
:
将此元标记添加到您的<head>
:
<meta name="csrf-token" content="{{ csrf_token() }}">
And then do this in the AJAX call:
然后在 AJAX 调用中执行此操作:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
回答by Kenziiee Flavius
If your using laravel 5.1 simply adding {{ csrf_field() }}
would do the trick
如果您使用laravel 5.1简单地增加{{ csrf_field() }}
会做的伎俩
回答by dnetix
The csrf token will be added automatically if you use the open and close tags for Form
如果您使用 Form 的打开和关闭标签,csrf 令牌将自动添加
{!! Form::open(['action' => '/auth/register', 'class' => 'ui large form']) !!}
-- Form stuff here --
{!! Form::close() !!}
回答by Rameez Rami
i hope this will help
我希望这个能帮上忙
set meta-tag like follows
设置元标签如下
<meta name="csrf-token" content="{{ csrf_token() }}">
then request like follows
然后请求如下
$.ajax({
data: {data1:'data1',data2:'data2'},
url: '/your/url/goes/here',
type: 'POST',
beforeSend: function (request) {
return request.setRequestHeader('X-CSRF-Token', $("meta[name='csrf-token']").attr('content'));
},
success: function(response){
console.log(response);
}
})