Solved

Javascript timed redirect

Posted on 2014-04-16
2
288 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
[X]
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
2 Comments
 
LVL 110

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 56

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

726 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