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

eludic's avatar

Redirect on Session expire

Hello, I have a weird situation and hopefully someone can provide me with a solution.

My application has independent login pages for the user depending to the company_id. After the user logs in, this company_id is set in the user session. When the session auto expires, he is to be redirected to the login page.

The login page URL is something like http://app.test/login/company/{id}

Now with the session expiring, how do I auto redirect him to the correct login page?

0 likes
6 replies
sr57's avatar

Done automatically by your middleware if auth is used.

Snapey's avatar

when the session expires, the company id in the session will obviously be unavailable.

If the user leaves the browser open, you can redirect them to the login page when nearing session expiry with code such as this in the head:

<meta http-equiv="refresh" 
content="{{ config('session.lifetime') * 60 }};url=/login/company/{{ session('company_id') }}">

What this should do is reload the page with the correct url if it is left idle for the session lifetime. It gets around loss of the session because the code is inserted into the page head when the page was loaded, not when it expired.

This is a variation on my tip https://talltips.novate.co.uk/laravel/csrf-and-expired-login-forms

eludic's avatar

Thank you for your replies. Will look into that URL

martinbean's avatar

@eludic You don’t. The session’s expired. If you set the company ID in the session and the session ends, the company ID’s gone as well.

eludic's avatar

@martinbean Yes that was my original concern hence I implemented @snapey's solution. Hopefully that is a way around it.

martinbean's avatar

@eludic A way around what? If a session ends then all the data it included goes too. There is no “way around” data not existing any more.

Please or to participate in this conversation.