Laravel 5.2 Token Mismatch Error Fix

Lots of people talking about this error. After investing hours I finally reached at solution. Actual solution is describing here at StackOverflow. Stackoverflow is always helpful when we stuck somewhere. Here are some of issue:

The possible reason was due to hidden iframe injection by browser add-ons. However I could not find it my web page. To resolve issue all you need to add P3P header in VerifyCsrfToken.php middleware file. Your issue should be gone once you add P3P header. Check below VerifyCsrfToken.php code that resolve my issue.

namespace App\Http\Middleware;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier;
use Closure;

class VerifyCsrfToken extends BaseVerifier
{
    /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        //
    ];
    
    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;
    }    
    
}

I added following two section.

use Closure;

handle function to add header

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;
}

	



Written by Bala Krishna

Bala Krishna is web developer and occasional blogger from Bhopal, MP, India. He like to share idea, issue he face while working with the code.