1

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

Community
  • 1
  • 1
scott
  • 3,112
  • 19
  • 52
  • 90
  • I think the problem could be with the form's action. Try changing it to `{{ url('add-post-new') }}` or something equivalent, instead of `http://localhost/laravel/public/add-post-new`. – John Bupit Aug 14 '15 at 10:40
  • @JohnBupit.i tried not workinng. – scott Aug 14 '15 at 10:42
  • 1
    You also don't seem to have `` in the form. I'm sure it's explained in one of the tutorials you read. – John Bupit Aug 14 '15 at 10:42
  • @JohnBupit.i also tried blade template token that also not working – scott Aug 14 '15 at 10:43
  • Can you post what you tried, and the error you get when you try it? – John Bupit Aug 14 '15 at 10:43
  • @JohnBupit. in question alreadt included my blade template form and instead of that token i have added many stackoverflow user answers but every time run i get same token mismatch error – scott Aug 14 '15 at 10:46
  • 1
    I have the same error today, it's working fine on my development env but when I push it to the staging server it throws me this error, I have regenerate the key from artisan, and delete all files in sessions folder in storage with no luck !! it's driving me crazy. I'm using the From:: facade so the hidden input of token is there. – Iliyass Hamza Aug 14 '15 at 11:41
  • @tester Did you ever find a solid reason and solution for this issue? – tread Oct 28 '15 at 22:16

4 Answers4

3

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

fireball70
  • 368
  • 1
  • 4
  • 14
  • Thanks. I had the same issue and it turned out to be caused by an empty space before the opening – Daniel Kratohvil Nov 28 '15 at 18:56
  • Oh lord. I have been googling around for like 1 hour and THIS was the error! A stupid little space in routes.php. I was going crazy here. Thanks a LOT. – Myone Feb 12 '16 at 09:51
1

try adding this line after opening a form

<input type="hidden" name="_token" value="{{ csrf_token()}}"/>
Sid
  • 5,693
  • 3
  • 33
  • 50
  • except tokenmismatch exception, is everything working fine? and is there any special reason you providing form action like that instead using laravel default action rule? what if you just do /add-post-new in form action? what does it result? – Sid Aug 14 '15 at 10:58
  • .working.looks like silly mistake but took long time for me.thank you.is there any reason for that ? – scott Aug 14 '15 at 10:58
  • Well i got into a same problem before and took me a week to solve it. if and only if my answer helps you, help me by marking it as a correct :) – Sid Aug 14 '15 at 11:00
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/86982/discussion-between-sid-and-tester). – Sid Aug 14 '15 at 11:02
1

Suddenly I am getting this exception.

Then restarting and clean cache works for me.

To clear the cache use :php artisan cache:clear

Md Sirajus Salayhin
  • 4,974
  • 5
  • 37
  • 46
  • I was just struggling with this issue and this suggestion resolved the issue for me. It took me an hour to find this so I'm very thankful. – Michael Aug 20 '15 at 20:42
1

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.

meluluji
  • 319
  • 1
  • 2
  • 15
  • This is not a good way of fixing it, in fact it's no fix at all. The error shouldnt appear, but this is just a way of saying " Hey let's not check our form at all". – killstreet May 31 '16 at 17:40