Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fant0m's avatar

Laravel session cookie/Token mismatch exception

Hello, I have no idea what's going on here. I'm experiencing some issues with sending requests threw ajax.

  1. I always send token
  2. When I run webpagetest.org on first view it returns 500 error code(token mismatch exception but on repeat view it's working very well(status code 200, so sending correct token is working).

Where can be problem? I deleted all cookies in my browser and then I had same error until second reload. I think it might be problem with laravel_session cookie, like while there's not this cookie it's throwing that error.

Anyone else experienced this problem?

0 likes
3 replies
paradox's avatar

@fant0m Seeing the code would be helpful, wouldn't you agree? Otherwise one's may take some time finding a good crystal ball :P

1 like
jekinney's avatar

Might be checking before it's set. That's why most time you set the token in the header on Ajax calls.

fant0m's avatar

@paradox what code should I post? It's all about laravel's core, there's not code I can post because there's just simple http post request that works correctly. @jekinney huh?

And I forgot to write I'm talking about javascript laravel_session cookie, when it doesn't exists(when I clear all cookies) any http request throws token mismatch error.

Edit: I realized laravel's not creating cookies only when I redirect threw handler.php like so:

public function render($request, Exception $e)
    {
        if($e instanceof \Symfony\Component\HttpKernel\Exception\NotFoundHttpException)
        {
        // always redirect to homepage
        // some code
            return response()->view('design', compact('something'));
        }
        return parent::render($request, $e);
    }

how to update it to return view with cookies? Something like:

 return response()->view('design', compact('something'))->withCookie('laravel_session', howShouldIAccessIt?)->withCookie('XSRF-TOKEN', huh?);

Please or to participate in this conversation.