Solved

Javascript timed redirect

Posted on 2014-04-16
2
282 Views
Last Modified: 2014-04-21
Hi,

i am working on a content management system which incorporates mainly php and mysql. I have developed a timed outed out functions which kicks the user out on inactivity. however, if the user is just sitting on a page and never interacts with the page no php request is sent the server and the user is still 'on line' even though they have timed out(this would be the same if they closed the browser). I want to develop a function that detects any page interaction (ie scrolling, clicking, button pressing etc) if these actions are being performed than do -nothing. if they are not being done than start a time limit and once it is reached call a php page and log them out.

Essentially i have an area where you can see online users. I do not want it to show inactive individuals instead i want all inactive individuals to be kicked off.

What is the best way to accomplish this?

thanks a bunch in advance
0
Comment
Question by:M. Jayme Nagy
2 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 40004666
You first might want to understand the interaction of HTTP client / server protocols.  There is no such thing as a logged-in user; there are only clients that return cookies.  Usually this is done with a PHP session cookie.  A PHP session that is not "refreshed" for 24 minutes is lost, causing the client to appear logged out.  These articles may be helpful.

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/A_11271-Understanding-Client-Server-Protocols-and-Web-Applications.html

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11909-PHP-Sessions-Simpler-Than-You-May-Think.html

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_2391-PHP-login-logout-and-easy-access-control.html
0
 
LVL 54

Accepted Solution

by:
Julian Hansen earned 500 total points
ID: 40005961
In answer to your question - if I understand you correctly you want to pickup any activity on the page and update the session timeout?

How are you timing the page out at the moment?

Here is a small sample that demonstrates how you could achieve this
<!doctype html>
<html>
<head>
<title>Fred</title>
<script src="http://code.jquery.com/jquery.js"></script>
<script type="text/javascript">

// OUR PULSE VARIABLE
var pulse = true;

$(function() {
  
  // START OUR HEARTBEAT MONITOR TO BEAT EVERY
  // 5 SECONDS. ADJUST AS NECESSARY

  setInterval("heartbeat()", 5000);
  
  // MONITOR CLICK AND MOUSE MOVE EVENTS
  // SET OUR PULSE TO TRUE IF FIRED TO 
  // INDICATE THAT WE ARE ALIVE
  $(document).click(function() {
    pulse = true;
  });
  $(document).mousemove(function() {
    pulse = true;
  });
});

// HEARTBEAT FUNCTION
// IF WE HAVE HAD A PULSE THEN CALLBACK TO THE
// SERVER TO ADJUST THE TIMEOUT

function heartbeat()
{
  if (pulse) {
    $.post("heartbeat.php");
  }

  // CLEAR THE PULSE  
  pulse = false;
}
</script>
</head>
<body>
</body>
</html>

Open in new window

We use the heartbeat function on a setInterval because we don't want to flood the server with heartbeats. The internal storage of the pulse means we keep track of activity locally and on the heartbeat we inform the server that we are still alive - you can tune this by changing the setInterval timeout value.

The heartbeat.php call should be changed to whatever script you use to reset your timeout on the server side. This could be as simple as
<?php
session_start();
?>

Open in new window

If you are using PHP managed sesions.
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article discusses four methods for overlaying images in a container on a web page
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

831 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