Solved

Javascript timed redirect

Posted on 2014-04-16
2
276 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 108

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 51

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
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.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now