This question already has an answer here:
-
How to detect idle time in JavaScript elegantly?
36 answers
T-mobile already uses this logic but I don't know how to do it.
This is the scenario: I login to my website and don't do anything at all for 2 minutes, just sit and look at the screen. In such case, I should be redirected to logout page where session will be destroyed.
How can I do this?
Examples on web are based on user actions such as check something (last activity etc.) after user activity.
Note: I'm using Codeigniter and its database sessions.
You need javascript:
<script type="text/javascript">
setTimeout(onUserInactivity, 1000 * 120)
function onUserInactivity() {
window.location.href = "onUserInactivity.php"
}
</script>
This will redirect the user after 2 Minutes of inactivity. If you want to make it dependend of the mouse-moving, try:
<script type="text/javascript">
inactivityTimeout = False
resetTimeout()
function onUserInactivity() {
window.location.href = "onUserInactivity.php"
}
function resetTimeout() {
clearTimeout(inactivityTimeout)
inactivityTimeout = setTimeout(onUserInactivity, 1000 * 120)
}
window.onmousemove = resetTimeout
</script>
So, on page load
$now = mktime();
if($now - $_SESSION['last_activity'] < 120)
{
$_SESSION['last_activity']=mktime();
}else{
logout();
}
Has 120 seconds passed since their last activity? Log them out/destroy their session, etc.
For the page to automatically log them out after being idle, you should be using javascript.
If you want this to happen without javascript, and without any action from the user, then put this on all your views:
<meta HTTP-EQUIV="REFRESH" content="120; url=http://www.yourdomain.com/logout.php">
It will redirect them to your logout screen after 120 seconds on the page. If they click of so