TokenMismatchException in VerifyCsrfToken.php line

2019-02-28 08:00发布

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 ?

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 Auth Post Submit - TokenMismatchException in VerifyCsrfToken.php line 46

TokenMismatchException in VerifyCsrfToken.php line 53 in Laravel 5.1

TokenMismatchException in VerifyCsrfToken.php line 46

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

Laravel 5 TokenMismatchException in VerifyCsrfToken.php line 46

Encountering "TokenMismatchException in VerifyCsrfToken.php" error

Laravel TokenMismatchException

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

TokenMismatchException when uploading a Video?

updated: enter image description here

4条回答
冷血范
2楼-- · 2019-02-28 08:29

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

查看更多
劳资没心,怎么记你
3楼-- · 2019-02-28 08:42

Suddenly I am getting this exception.

Then restarting and clean cache works for me.

To clear the cache use :php artisan cache:clear

查看更多
不美不萌又怎样
4楼-- · 2019-02-28 08:49

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

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;
    

    }

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

查看更多
太酷不给撩
5楼-- · 2019-02-28 08:54

try adding this line after opening a form

<input type="hidden" name="_token" value="{{ csrf_token()}}"/>
查看更多
登录 后发表回答