在 Laravel 5.1 中,VerifyCsrfToken.php 第 53 行中的 TokenMismatchException:(Firefox 浏览器)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32008088/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:11:05  来源:igfitidea点击:

TokenMismatchException in VerifyCsrfToken.php line 53:(Firefox browser) in laravel 5.1?

phplaravellaravel-5laravel-5.1

提问by scott

i am trying to figure out why this error even though its fresh installation.i got this error in my project so i googled ,none of the answers worked for me.so i created new project and copied all controller ,view, and model.its worked fine after few hours once again token mismatch error.why this happen in laravel ?

我想弄清楚为什么这个错误,即使它是全新安装的。我在我的项目中遇到了这个错误,所以我用谷歌搜索,没有一个答案对我有用。所以我创建了新项目并复制了所有控制器、视图和模型。它几个小时后再次出现令牌不匹配错误。为什么会在 Laravel 中发生这种情况?

my form

我的表格

<form class="form-horizontal action="http://localhost/laravel/public/add-post-new"  enctype="multipart/form-data" method="POST" accept-charset="UTF-8" >
        <div class="form-group">
            <label for="inputEmail" class="control-label col-xs-2">Title</label>
            <div class="col-xs-10">
                <input type="text" class="form-control" id="post_title" placeholder="Title" name="post_title">
            </div>
        </div>

        <div class="form-group">
            <label for="inputPassword" class="control-label col-xs-2">Content</label>
            <div class="col-xs-10"><textarea class="form-control" style="resize:none" rows="25" name="post_content"></textarea>
            </div>
        </div>
         <div class="form-group">
            <label for="inputPassword" class="control-label col-xs-2">Featured Image</label>
            <div class="col-xs-10"><input type="file" class="filestyle" data-buttonText="Find" name="featured_image">
            </div>
        </div>
     <div class="form-group">
            <label for="inputPassword" class="control-label col-xs-2">Post Images</label>
            <div class="col-xs-10"><input type="file" class="filestyle"  name="post_gallery[]" multiple />
            </div>
        </div>  

     <div class="form-group">
            <label for="inputPassword" class="control-label col-xs-2">Select Category</label>
            <div class="col-xs-10" >
            <select  class="form-control" name="cat_id">
                <?php $data=Category::all(); ?>
            <option value="0">Default Category</option>
            @foreach($data as $value)
                 <option value="{{$value->id}}">{{$value->cat_name}}</option>
              @endforeach 
        </select>

            </div>
        </div>      
 <div class="form-group">
            <label for="inputPassword" class="control-label col-xs-2">Publish Post</label>
            <div class="col-xs-10">
           <label class="radio-inline">
  <input type="radio" name="published" id="inlineRadio1" value="1"> Publish 
</label><br>
<label class="radio-inline">
  <input type="radio" name="published" id="inlineRadio2" value="2"> UnPublish
</label><br>
<label class="radio-inline">
  <input type="radio" name="published" id="inlineRadio2" value="3"> Draft
</label><br><br>

            </div>
        </div>      
 <div class="form-group">
            <label for="inputPassword" class="control-label col-xs-2">Slider Post</label>
            <div class="col-xs-10">
           <label class="radio-inline">
  <input type="radio" name="slider_post" id="inlineRadio1" value="1"> Slider Post 
</label><br>
<label class="radio-inline">
  <input type="radio" name="slider_post" id="inlineRadio2" value="2"> Not required
</label><br><br>


            </div>
        </div>  
        <div class="form-group">
            <div class="col-xs-offset-2 col-xs-10">
                <input name="_token" type="hidden" value="{{ csrf_token() }}"/>

                <button type="submit" class="btn btn-primary">Submit Post</button>
            </div>
        </div>

before asking question i have read many tutorial

在提问之前,我已经阅读了很多教程

Laravel 5, Forms, TokenMismatchException in VerifyCsrfToken.php line 46

Laravel 5、Forms、VerifyCsrfToken.php 第 46 行中的 TokenMismatchException

Laravel 5 Auth Post Submit - TokenMismatchException in VerifyCsrfToken.php line 46

Laravel 5 Auth Post Submit - VerifyCsrfToken.php 第 46 行中的 TokenMismatchException

TokenMismatchException in VerifyCsrfToken.php line 53 in Laravel 5.1

Laravel 5.1 中 VerifyCsrfToken.php 第 53 行中的 TokenMismatchException

TokenMismatchException in VerifyCsrfToken.php line 46

VerifyCsrfToken.php 第 46 行中的 TokenMismatchException

Laravel 5, ajax, 500 Internal Server Error, TokenMismatchException in VerifyCsrfToken.php line 46:

Laravel 5、ajax、500 内部服务器错误、VerifyCsrfToken.php 第 46 行中的 TokenMismatchException:

Laravel 5 TokenMismatchException in VerifyCsrfToken.php line 46

Laravel 5 TokenMismatchException 在 VerifyCsrfToken.php 第 46 行

Encountering "TokenMismatchException in VerifyCsrfToken.php" error

遇到“VerifyCsrfToken.php 中的 TokenMismatchException”错误

Laravel TokenMismatchException

Laravel 令牌不匹配异常

http://laravel.io/forum/01-30-2015-laravel5-tokenmismatchexception-in-verifycsrftoken

http://laravel.io/forum/01-30-2015-laravel5-tokenmismatchexception-in-verifycsrftoken

TokenMismatchException when uploading a Video?

上传视频时出现 TokenMismatchException?

updated: enter image description here

更新: 在此处输入图片说明

回答by fireball70

This may help someone. Check your php files that not start with empty line or space! It cost me much trouble. Including that above!

这可能会帮助某人。检查不以空行或空格开头的 php 文件!这给我带来了很多麻烦。包括上面的!

回答by meluluji

I had this painful error and this is what I did to fix it: 1. Go to \Http\Controllers\Middleware\VerifyCsrfToken.php2. In the protected $exceptadd your route to be excluded from this verification. Example:

我遇到了这个痛苦的错误,这就是我为修复它所做的: 1. 转到\Http\Controllers\Middleware\VerifyCsrfToken.php2. 在protected $except添加要从此验证中排除的路由中。例子:

protected $except = [
        'user*'
    ];
  1. And if you still get this error, add this function below the protected $except

    public function handle($request, Closure $next) {
    
    $regex = '#' . implode('|', $this->except) . '#';
    
    if ($this->isReading($request) || $this->tokensMatch($request) || preg_match($regex, $request->path())) {
        return $this->addCookieToResponse($request, $next($request));
    }
    
    throw new TokenMismatchException;
    

    }

  1. 如果仍然出现此错误,请在受保护的 $except 下方添加此函数

    public function handle($request, Closure $next) {
    
    $regex = '#' . implode('|', $this->except) . '#';
    
    if ($this->isReading($request) || $this->tokensMatch($request) || preg_match($regex, $request->path())) {
        return $this->addCookieToResponse($request, $next($request));
    }
    
    throw new TokenMismatchException;
    

    }

And I think that's it. I hope to help someone.

我认为就是这样。我希望能帮助某人。

回答by Sid

try adding this line after opening a form

打开表单后尝试添加此行

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

回答by Md Sirajus Salayhin

Suddenly I am getting this exception.

突然间我得到了这个例外。

Then restarting and clean cache works for me.

然后重新启动和清理缓存对我有用。

To clear the cache use :php artisan cache:clear

要清除缓存使用:php artisan cache:clear