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

symos's avatar
Level 1

Session variable *sometimes* lost after redirect

Hi all

I have the following code which grabs a GET variable, saves it in the session and then redirects to the same page without the GET variable:

if ($request->get('var')) {
            Session::put('var', $request->get('var'));

            Session::save();
            
            return redirect()->route('user.shop',
                ['slugName' => $slugName])
                ->header('Cache-Control',
                    'must-revalidate, no-cache, no-store, post-check=0, pre-check=0, private');
        }

Most of the time, I can then pull 'var' from the Session, but once in a while, it's not there!

I've tried both solutions I found online (saving the session before redirect and moving the StartSession middleware class out of $middlewareGroups and into $middleware in Kernel.php), but the problem persists.

If it was something that happened all the time it'd be easier to debug, but it only happens once in a while so I don't know where to look.

I could of course write this using different logic and be done with it. I'm just curious what's causing this weird behavior.

0 likes
0 replies

Please or to participate in this conversation.