laravel 5.1 auth csrf token mismatch

烈酒焚心 提交于 2020-01-11 07:56:33

问题


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

<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>

回答1:


Just add the csrf token as follows in the form :

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

it worked for me.




回答2:


Assume that your web server has already write access to session directory, in my case 'app/storage/framework/sessions/'.

Execute:

$ rm -f {your_web_app}/storage/framework/sessions/*



回答3:


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.

2) If you're trying to submit this form data via AJAX, the docs suggest passing the CSRF token like so:

Add this meta tag to your <head>:

<meta name="csrf-token" content="{{ csrf_token() }}">

And then do this in the AJAX call:

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});



回答4:


If your using laravel 5.1 simply adding {{ csrf_field() }} would do the trick




回答5:


The csrf token will be added automatically if you use the open and close tags for Form

{!! Form::open(['action' => '/auth/register', 'class' => 'ui large form']) !!}

-- Form stuff here --

{!! Form::close() !!}



回答6:


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);
    }
})


来源:https://stackoverflow.com/questions/32004037/laravel-5-1-auth-csrf-token-mismatch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!