Laravel 5 TokenMismatchException on PHP 5.6.9
When I realized this was only happening in IE and Chrome, but not Firefox, it led me to the fix. The app was using AddThis share buttons and the javascript was adding an iframe to the pages. This issue is resolved by adding a P3P header to the VerifyCsrfToken Middleware. Hope this saves somebody the hours I lost.
public function handle($request, Closure $next)
{
$response = $next($request);
if (last(explode('\\',get_class($response))) != 'RedirectResponse') {
$response->header('P3P', 'CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"');
}
return $response;
}
suncoastkid
Full-stack web developer; advertising, marketing & SEM expert.
Updated on July 24, 2022Comments
-
suncoastkid almost 2 years
Post requests work fine running Laravel 5 app on PHP 5.4. Post requests on the same app running on PHP 5.6.9 generate:
TokenMismatchException VerifyCsrfToken.php on line 46
This happens on every post request on both WAMP and IIS. Happens using database sessions and file sessions. Did a full reinstall and also tried all suggestions made here: https://laracasts.com/discuss/channels/general-discussion/keep-getting-tokenmismatchexception-verifycsrftokenphp-on-line-46?page=2. Folks are disabling the Csrf middleware as a fix, but that is not a viable solution. Any help appreciated.
-
dynamic almost 9 yearsthis could be oneline with jquery
-
Ardian Cakolli almost 9 yearsyes it could, something like
$.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content') } });
-
ssi-anik over 8 yearsjust took few hours likely 4 5 :/ :3
-
vvr02 almost 7 yearsIt saved me lot of time
-
Amadeo Manouchehri over 6 yearsThis is the only solution which works in the ocean of ridiculous "add csrf_field" answers on the net. Thanks you very much for your working solution.
-
buckthorn over 4 yearsFinally an answer that works! Thank you! I have no idea why this works, but it does the trick not only on Chrome, but Firefox, too.