How can I track user idle time

Posted on 2010-08-28
Last Modified: 2012-05-10

I would like to know how can I track whether there has been any activity from user on my site , that is basically what I want to do is if user has not clicked any link say for 5 minutes user should be logged out.

Question by:activesoftware
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 75

Accepted Solution

Michel Plungjan earned 250 total points
ID: 33548508
<script type="text/javascript">
var tId="";
document.onmousemove=function() {
window.onload=function() {
function resetTimer() {
     tId = setTimeout(function() { location.replace('logout.html') },50000);
LVL 12

Expert Comment

ID: 33548845
For each user, keep field last_action in the database.

On each page, first check if time() - $db['last_action'] is bigger than 5*60, and if, log the user out, otherwise update last action to current time.

This is far better solution that one above, because it is solved on the server side and user cannot interfere with it.

Author Closing Comment

ID: 33549054
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 12

Expert Comment

ID: 33549140
I don't think you will be driving far with mplungjan's car.
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33549152
Strange comment. Why would it not be perfectly ok to detect on the client that the user of the page on the client has not done anything on the client? You solution is server intensive, needs at least a session whereas mine just needs a tiny script in an external js file in the head of the page, can be included from the server or not depending.

If the user never clicks on anything, how are you planning to kick them to the homepage automatically? Using server push?

LVL 12

Expert Comment

ID: 33549337
Yes, it indeed has a pros that it detects also mouse movement. But that is easily spoofable. How can you be sure that user even has a javascript on?

Server intensive, come on :) And your comment comment about needing at least a session - how can you even log out without a session :) You need a session anyway.

Your script is intensive with resources (thou not server's), because it calls resetTimer() for every pixel mouse pointer moves. I approximate up to 100 calls per second.

No, if user doesn't click anything for 5 minutes, I'm just going to display them a message: "Your session has expired due to 5 minute inactivity. Please log on again."
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33549451
Seems the asker is not worried about spoofing, just want the user to go away if he does nothing on his site.

User can also ping your server with some ping script

Both solutions work in my opinion

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why isn't my DIV and Form centering? 1 31
embadded search engine in website 4 47
display data from multiple ajax calls 9 44
Javascript Error 10 24
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question