1

I'm using below javaScript code from this post to redirect user to logout page due to inactivity.

This won't work if a user opens multiple windows on the same site. Eg, if a user opens one window then opens another one in 10 minutes later. The first window would redirect to logout page to kill the user's session which shouldn't, because the user is still in active mode within the past 15 minutes.

Are there any way to do that?

<body onmousemove="reset_interval()" onclick="reset_interval()" onkeypress="reset_interval()" onscroll="reset_interval()">


<script type="text/javascript">
function set_interval()
{
//the interval 'timer' is set as soon as the page loads
timer=setInterval("auto_logout()",10000);
// the figure '10000' above indicates how many milliseconds the timer be set to.
//Eg: to set it to 5 mins, calculate 5min= 5x60=300 sec = 300,000 millisec. So set it to 3000000
}

function reset_interval()
{
//resets the timer. The timer is reset on each of the below events:
// 1. mousemove   2. mouseclick   3. key press 4. scroliing
//first step: clear the existing timer
clearInterval(timer);
//second step: implement the timer again
timer=setInterval("auto_logout()",10000);
..completed the reset of the timer
}

function auto_logout()
{
//this function will redirect the user to the logout script
window.location="your_logout_script_location_here";
}
</script>
Community
  • 1
  • 1
Osama Jetawe
  • 2,711
  • 6
  • 23
  • 40
  • You're probably better off using cookies to check activity/inactivity. –  Nov 29 '16 at 09:56
  • 1
    Record the timestamp of the last activity in your session. In your logout script, check how much time has passed since then, and depending on that, either log the user out, or don’t. – CBroe Nov 29 '16 at 09:59
  • I would never user your App,.. so if i'm on your app, I cant read my favorite news site on the side. – Masivuye Cokile Nov 29 '16 at 09:59
  • @jeff, thank you. how can I use cookies to check activity/inactivity using setInterval, do you have any sample post . – Osama Jetawe Nov 29 '16 at 10:05

0 Answers0