TokenMismatchException in VerifyCsrfToken.php line 53 in Laravel 5.1

前端 未结 13 1182
独厮守ぢ
独厮守ぢ 2020-12-11 16:47

When I try to login show me token error. I have checked token in view form it\'s right and when comment \\App\\Http\\Middleware\\VerifyCsrfToken::class, in the

相关标签:
13条回答
  • 2020-12-11 17:02

    This solution worked for me:

    Add {{ csrf_field() }} anywhere in the form.

    0 讨论(0)
  • 2020-12-11 17:03

    Well I think all missed the CSRF Token creation while logout!

    As I have solved out the problem.

    Just add below code to the header.

    <meta name="csrf-token" content="{{ csrf_token() }}">
    <script type=text/javascript>
        $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
        });
     </script>
    

    And if you use {!!Form::open()!!} it will automatically create the token. Otherwise you can use

    <input type="hidden" name="_token" id="_token" value="{!! $csrf_token !!}}" />
    

    or

    {!! csrf_field() !!}
    

    just immediate form open. Most importantly use return Redirect::to(''); on controller function or a page reload or ajax reload that the token can be created!

    Like:

    public function logout() {
        Session::flush();
        Auth::logout();
    
        return Redirect::to('/');
    }
    

    For ensure the token properly created or not check "view page source" on browser and it will shows like:

    <meta name="csrf-token" content="TbgWTQZhTv0J4eFBQNU4rlM3jOlmBeYlTgf0waZB">
        <script type=text/javascript>
        $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
        });
        </script>
    
    
    <form method="POST" action="/login-process" accept-charset="UTF-8" class="form-inline"><input name="_token" type="hidden" value="TbgWTQZhTv0J4eFBQNU4rlM3jOlmBeYlTgf0waZB">   
    

    I think it might solve the problem as it worked for me!

    0 讨论(0)
  • 2020-12-11 17:05

    I was also having this problem when trying to upload a file. Turned out the max_post_size was being exceeded, in which case apparently all POST variables are cleared and therefore no token is being received.

    0 讨论(0)
  • 2020-12-11 17:09

    You did not post your sample code in your question.

    Therefore check your code with the following options,

    try with hidden input field value:

    {!! csrf_token() !!} or {{ csrf_token() }}
    

    You can also use form blade template:

    {!! Form::open(array('method' => 'GET/POST','url' => 'YOUR_URL',)) !!}
    

    This will automatically add CSRF Code in your html script

    One more thing to include in <head> section is:

    <meta name="csrf-token" content="{{ csrf_token() }}">
    
    0 讨论(0)
  • 2020-12-11 17:12

    Remove App\Http\Middleware\VerifyCsrfToken::class from $middleware in Kernel.php.

    0 讨论(0)
  • 2020-12-11 17:13

    With a fresh install of Laravel 5.1, without just a composer update from version 5.0 to 5.1 I see some differences and one in the Middleware folder.

    EncryptCookies.php are a new Middleware, check if you have it.

    So, I don't have tested again, I tranfert at the moment my files from my version 5.0 to a new installation of version 5.1 but im pretty sure that can be the solution for this problem, EncryptCookies.php was in the stack of the token mismatch error.

    0 讨论(0)
提交回复
热议问题