I have a simple question.

How can i make an page immediately redirect to other page in example www.google.com when a specific session is expired or destroyed.

Here is how i get an session in PHP:

$Logged = $_SESSION['LoggedIn'];

There are two important things that the jQuery script must do:

  1. Without any refresh/reload needed on the page where the script is listening to make a redirect when the session is destroyed for example from another tab in the browser/or the session was expired by a period of time.
  2. The script must do a check on every 5 seconds for example.

I am new to jQuery so i'll be very thankfull to you if you can help me for the creation of this script.

Thanks in advance!

2 Answers 2


This question is likely to be closed as off-topic or too broad - but Ill think you're trying to ask:

How can I redirect someone to the login page after their session has expired because their cart will no longer be available.


How can I force an admin's screen to redirect to the login page after their session has expired?

And the answer to both of these is, bluntly, you can't. You can't listen to an event that says "hey their session expired". Why? Because their "session" on the PHP side may or may not still be valid.

Banks do this all the time. They set a javascript timeout that is about 60 seconds shorter than the session expiration time and then they display a warning:

enter image description here

How do they force the logout? Easy. By redirecting to the actual logout route. In the case of Magento it's:


Just give me the code

If all you want to do is display a warning to the user that they'll be logged out then all you have to do is this and you don't even need jQuery:

      location.href = '/customer/account/logout';

Hope that helps!


In addition to Phil's answer, keep in mind that that won't work if you have any AJAX requests on the page (such as one page checkout) - the session will be renewed, but the timeout doesn't change.

In addition to that, some mobile OSses halt Javascript execution when you switch tabs or lock your screen. setInterval works better than setTimeout in that regards.

There could be more side-effects that I'm not addressing.. in any case, we came up with a library (not open source unfortunately) that fetches the cookie lifetime dynamically, proxies AJAX calls which updates a HTTP stack/queue which gets processed at specific intervals which.. eventually leads to a message and a window.location redirect. The redirect is different as well depending on whether your persistent cookie is active or not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.