user3170450
user3170450

Reputation: 405

Javascript redirect when cookie gets expired

I want my users to redirected to login page as soon as cookie gets expired. Scenario is if the system is idle and php session has been expired automatically login screen should come without connecting back to PHP server. I am setting a cookie (value is same as expiry) from server with every request which has its expiry time and gets increased with every request.

Is there a way by which I can watch over a cookie and whenever an expiry time is reached I should be able to call an action.

Upvotes: 0

Views: 939

Answers (1)

cdoshi
cdoshi

Reputation: 2832

Well, there is no callback fired automatically when a cookie expires. You can have a setInterval setup that checks for presence of that cookie. If not present, you can redirect user to the login page.

Pseudo code would be something like this. It checks for the cookie every second.

var timer = setInterval(function () {

// Check if cookie is present, 
if not present {
   // clear the timer and redirect to login page
} else {
  // do nothing
}
}, 1000); 

Upvotes: 1

Related Questions