-2

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.

BentCoder
  • 12,257
  • 22
  • 93
  • 165
  • Answer found here: [Detecting idle time in JavaScript elegantly][1] [1]: http://stackoverflow.com/questions/667555/detecting-idle-time-in-javascript-elegantly – BentCoder Apr 12 '13 at 09:03

2 Answers2

3

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>
tobspr
  • 8,200
  • 5
  • 33
  • 46
1

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

David Houde
  • 4,835
  • 1
  • 20
  • 29
  • Unfortunately this requires user action. – BentCoder Apr 12 '13 at 08:48
  • 1
    This does not require user action. If a user decides to take action, this will determine whether or not his session should expire from non-use. This should be used with javascript to achieve OP's original goal, but he shouldnt rely strictly on client-side code. – David Houde Apr 12 '13 at 08:51
  • That's what I said my post. There won't be user action at all. – BentCoder Apr 12 '13 at 08:53
  • 1
    Okay, please articulate your questions better before asking next time. – David Houde Apr 12 '13 at 08:55
  • 1
    No, your original question asked how to have an inactive site log the user out after a certain period of inactivity. This question has been answered. – David Houde Apr 12 '13 at 08:58
  • "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." If this part itself is not clear enough for you then God bless you. – BentCoder Apr 12 '13 at 09:07
  • 1
    It seems clear to me, but based on your comments on everyones answer I am guessing you don't know what you want. You are not going to get CodeIgniter to determine whether a client is staring at his computer screen or not without access to their webcam. You wont be able to determine mouse movement / non-navigating activity without javascript. Figure out what you want and try again. – David Houde Apr 12 '13 at 09:19
  • Anyway, we would find bits to put blame on each other by focusing on unnecessary things so lets just leave it. Thanks for the answer though. – BentCoder Apr 12 '13 at 09:23