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

sam_razor's avatar

Session resetting after first page load on new session

I'm working on this blog type of website which has some posts locked behind auth. When trying to access a post that is locked behind auth, in the controller I check if they're logged in, and if not set in the session the URL of the post, and then redirect to login page using the following code. On the login controller I can then get that post URL from the session and redirect to it fine.

session(['login-redirect' => url()->full()]);
redirect()->route('login')->send();

However when doing this process from a fresh session (i.e. open a private window and go straight to the post URL), 'login-redirect' doesn't get retained in the session after redirecting to the login page. It's like the session is reset somewhere in-between. If I go back to the post URL and try again it works, just not on the very first load of the session.

Any ideas why this would be happening?

0 likes
2 replies
sam_razor's avatar

The public/protected setting on posts can be toggled by the site admin so they share the same route and controller etc. I suppose I could move the logic to a middleware anyway, but I'm not sure if that'll solve the main issue of redirecting back to the post after logging in.

Please or to participate in this conversation.