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

gnsjhenjie's avatar

Prevent extending session lifetime on specific route

I've written a function to check if the user is currently logged in. But when the user is accessing this function, I don't want the session lifetime to be extended. Is it possible to let users access a route without extending their session lifetime? Thanks~

0 likes
4 replies
Snapey's avatar

don't think so. The session exists for as long as the user keeps sending requests, so sending a request (as a user) will extend their session.

You could call an api route and this would not extend the session, but it would also not check that the user is still logged in.

Perhaps you could hold off checking until the session lifetime is reached (default 2 hours) and then check.

1 like
Sinnbeck's avatar

You could perhaps use an api request (no session) to check the database for an active session manually. Will require that you use the database driver. But as there is no session, you need some other way to find the right record

Edit: sorry snapey. Didn't see your answer was almost the same :)

1 like
gnsjhenjie's avatar

@snapey @sinnbeck Thanks for your reply. It seems that there isn't an easy way to deal with that. Holding off checking until the session lifetime reaching might not work on my website. Because they usually need to open more than one tab on my website.

Snapey's avatar

@gnsjhenjie but thats the point. A user is working happily in one tab, and another tab should NOT expire their session. Nor should it poll so frequently that it keeps the session going.

It should wait until the session might have closed and then test it.

Perhaps if you explained WHY you want to do this, we might be able to suggest a better solution.

Please or to participate in this conversation.