Piya Desai February 2016

TokenMismatchException in VerifyCsrfToken.php line 53 in laravel 5 while uploading a .csv file

I am reading a csv file and sending the contents to database. I get tokenmismatch error even though I tried mentioning the csrf token.

Answers


Gaurav Dave February 2016

Try below snippet in your view:

<input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">

OR

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

If that doesn't work then, comment this line:

\App\Http\Middleware\VerifyCsrfToken::class

in app/Http/Kernel.php file.

Since this VerifyCsrfToken Middleware makes it easy to protect your application from cross-site request forgery (CSRF) attacks. It's not recommended to comment that line, But for testing purpose you can do that.

See, if that works.


Tariq Khan February 2016

CSRF tokens are only placed on routes that handle form submissions. I think its likely that you are reading the CSV and saving in the database that doesn't require you to submit a CSRF token, I would recommend to place that route where CSRF token is disabled so it doesn't require you to submit a CSRF token. You can do that by doing something like this.

In your App/Http/Midlleware folder go to VerifyCsrfToken.php and include the specific route to the exception as follows:

/**
 * The URIs that should be excluded from CSRF verification.
 *
 * @var array
 */
protected $except = [
    'csvParserRoute',
];

This will disable the CSRF token requirement for this route only and the error should go away.

Post Status

Asked in February 2016
Viewed 1,832 times
Voted 5
Answered 2 times

Search




Leave an answer