gaza165 Posted April 15, 2009 Share Posted April 15, 2009 I am trying to write a script to let me see who is online, who is idle and who is offline. This is the script I have so far. <?php session_start(); include("dbconnect.php"); $sql = mysql_query("SELECT last_activity,username FROM users"); echo "<ul class='online'>"; while($row = mysql_fetch_assoc($sql)) { $usertime = strtotime($row['last_activity']); $difference = $usertime - time(); $idletime = $difference - 150; if($difference < -500) { echo $row['username']." is offline<br/>"; } else if($idletime <= 0) { $idletime++; echo $row['username']." is idle<br/>"; } else { echo $row['username']." is online<br/>"; } echo "--------------------------------<br/>"; } ?> Everytime a message is sent, i run the script below to update the users last activity timestamp ahead so it knows there has been activity from the user so get it to say they are online. After an amount of inactivity i want it to say they are idle. <?php session_start(); include('dbconnect.php'); $username = $_SESSION['login']['username']; $sql = mysql_query("UPDATE users set last_activity = CURRENT_TIMESTAMP + 100 WHERE username = '$username'"); ?> The problem I am having is when the user sends the message, im not sure how much i have to update the time by for the countdown to start again. Thanks Garry Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 15, 2009 Share Posted April 15, 2009 Why are you trying to mess with future times? Simply set the last_activity to be NOW(). Then all you need to do is decide what the threshold of time is for you to consider the users to be Online/ Idle/ Offline. Your $difference should be $time - $usertime. This will be a value in seconds. It's up to you to decide how many seconds in the past activity indicates that they are online vs. Idle vs Offline. Just to raise the question -- so if I don't post I'm not "online"? Seems to me that online/offline usually is about login to a system. Quote Link to comment Share on other sites More sharing options...
gaza165 Posted April 15, 2009 Author Share Posted April 15, 2009 Ok.... The reason for the ONLINE/IDLE/OFFLINE thing is that it is a Chat Application written in JQuery/PHP. Much like the popular facebook, when a user has been inactive for a certain amount of time, it shows that user to be idle. Then after a longer amount of time, it times the user out and they need to log in again. This is what I am trying to achieve here. I have updated the code to this.. userstatus.php <?php session_start(); include("dbconnect.php"); $sql = mysql_query("SELECT last_activity,username FROM users"); echo "<ul class='online'>"; while($row = mysql_fetch_assoc($sql)) { $usertime = strtotime($row['last_activity']); $difference =time() - $usertime $idletime = $difference - 150; echo $difference." is the difference"; if($difference < -500) { echo $row['username']." is offline<br/>"; } else if($idletime <= 0) { $idletime++; echo $row['username']." is idle<br/>"; } else { echo $row['username']." is online<br/>"; } echo "--------------------------------<br/>"; } ?> then the update.php <?php session_start(); include('dbconnect.php'); $username = $_SESSION['login']['username']; $sql = mysql_query("UPDATE users set last_activity = CURRENT_TIMESTAMP WHERE username = '$username'"); ?> Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 19, 2009 Share Posted April 19, 2009 Ok, this looks good. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.