Laravel 5 TokenMismatchException on PHP 5.6.9

11,469

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;
    }
Share:
11,469
suncoastkid
Author by

suncoastkid

Full-stack web developer; advertising, marketing & SEM expert.

Updated on July 24, 2022

Comments

  • suncoastkid
    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
    dynamic almost 9 years
    this could be oneline with jquery
  • Ardian Cakolli
    Ardian Cakolli almost 9 years
    yes it could, something like $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('meta[name="csrf_token"]').attr('content') } });
  • ssi-anik
    ssi-anik over 8 years
    just took few hours likely 4 5 :/ :3
  • vvr02
    vvr02 almost 7 years
    It saved me lot of time
  • Amadeo Manouchehri
    Amadeo Manouchehri over 6 years
    This 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
    buckthorn over 4 years
    Finally 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.