Solved

Javascript timed redirect

Posted on 2014-04-16
2
279 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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

22 Experts available now in Live!

Get 1:1 Help Now