Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Javascript timed redirect

Posted on 2014-04-16
2
Medium Priority
?
298 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:J N
[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 111

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 59

Accepted Solution

by:
Julian Hansen earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
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.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

618 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