Prakash Chokalingam
Prakash Chokalingam

Reputation: 95

Token Mismatch Exception - Token changes for every call

I don't why its happening my whole project worked good till last week.Day before yesterday I couldn't load the site and there is no laravel error to shown.So updated the composer and packages via "composer update" command.From then where ever I using post methods it projects the

TokenMismatchException in VerifyCsrfToken.php line 53:

error.

So I changed that method to get

<form method="get" action="{{action('SignupController@index')}}">
<input type="hidden" name="_token" value="{{csrf_token()}}">
</form>

and i wrote in controller like

public function index()
 {
     echo Session::token();
     echo '<hr>';
     echo Input::get('_token');
 }

it gave result like ,

     kJqTndLBxOhyzUZd78Zj5IG2K5VIRVwDxepreHXE
     ----------------------------------------
     xKpqbnzb2DoRsw6ZtqTgHgcJFB9SixydkWjLhPJA

every time i reloading the page the new token comes.How can i get out of this ?

Upvotes: 1

Views: 270

Answers (1)

tinyoverflow
tinyoverflow

Reputation: 2031

Be sure to apply the "web"-middleware to your controller / route. The easiest way to do that is to create a route group:

Route::group(['middleware' => 'web'], function() {
    // Your route definitions here.
});

The "web"-middleware initializes the StartSession class wich makes it possible to save sessions. You see what it does in the app/Http/Kernel.php.

Here are more possibilities to register routes: https://laravel.com/docs/5.2/middleware#registering-middleware

Upvotes: 1

Related Questions