Home  >  Article  >  Backend Development  >  Share the solution to the VerifyCsrfToken error reporting problem in Laravel

Share the solution to the VerifyCsrfToken error reporting problem in Laravel

黄舟
黄舟Original
2017-09-09 10:05:171537browse

This article mainly introduces to you the solution to the VerifyCsrfToken error problem in the Laravel framework. The article introduces it in detail through the example code. It has a certain reference for everyone's study or work. Friends who need it follow the small Let’s learn together.

Preface

This article mainly introduces the relevant content about the VerifyCsrfToken error reporting problem in the Laravel framework, and shares it for your reference and study. Below Not much to say, let’s take a look at the detailed introduction.

Error reporting

When the form submits data in post mode, the following error reporting is encountered.


TokenMismatchException in VerifyCsrfToken.php line 67:
in VerifyCsrfToken.php line 67
at VerifyCsrfToken->handle(object(Request), object(Closure))

post data submission error

Reason

Laravel It is recommended to register the VerifyCsrfToken Middleware globally to automatically verify whether all Post, Put, and Delete requests contain legal _csrf tokens.

Solution

Method 1. Add the following hidden field code in the form


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

Method 2. Add csrf_field in the form

(Same function as the above solution)


{!! csrf_field() !!}

Method 3. Comment Kernel.php code

Open app\Http\Kernel.php and comment out the following code in the file


\App\Http\Middleware\VerifyCsrfToken::class

Method 4. Modify handle( ) method

Open \app\Http\Middleware\VerifyCsrfToken.php, add or modify the handle() method as follows:


##

 public function handle($request, \Closure $next)
 {
  // 使用CSRF
  //return parent::handle($request, $next);
  // 禁用CSRF
  return $next($request);
 }

Additional csrf introduction

csrf diagram explanation

Summary

The above is the detailed content of Share the solution to the VerifyCsrfToken error reporting problem in Laravel. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn