Share the solution to the VerifyCsrfToken error reporting problem in Laravel

黄舟
Release: 2023-03-15 21:02:02
Original
1572 people have browsed it

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))
Copy after login

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() }}" />
Copy after login

Method 2. Add csrf_field in the form

(Same function as the above solution)


{!! csrf_field() !!}
Copy after login

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
Copy after login

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);
 }
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!