gaza165 Posted April 14, 2009 Share Posted April 14, 2009 <?php 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']); $currenttime = time(); echo "usertime is ".$usertime; echo "<br/>current time is ".$currenttime."<br/><br/>"; if($usertime < $currenttime) { echo "IDLE"; } else { echo $row['username']."ONLINE"; } } echo "</ul>"; ?> The first thing i do is get the current time, i then compare that with the users last activity and if it is less than the current time, i know the user is idle, however if the last activity is greater than the current time then we know the user is online. I also am running another script everytime a message it set to update the users last activty to the current time * 100 so it puts the time in the future so that we know the user is still online and not 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'"); ?> can someone tell me where I am going wrong with this script, what have i missed? thanks Garry Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/ Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 what exactly is the problem/question ? Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809460 Share on other sites More sharing options...
gaza165 Posted April 14, 2009 Author Share Posted April 14, 2009 Do you think this script is right for what I am trying to achieve??? Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809463 Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 looks alright, if it's working how you want it to, then there isn't anything wrong with Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809479 Share on other sites More sharing options...
gaza165 Posted April 14, 2009 Author Share Posted April 14, 2009 Well it works out how to change between online and idle but I want it to check if a user has been inactive for more than 10 min to have their session timeout and log them out Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809482 Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 check if the last logged time for the user is <?php if($lastLoggedtime < (time() * 60 * 10)) { // I think session_destroy(); } Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809490 Share on other sites More sharing options...
gaza165 Posted April 14, 2009 Author Share Posted April 14, 2009 <?php 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']); $currenttime = time(); if($usertime < $currenttime) { echo $row['username']."IDLE<br/>"; } else { echo $row['username']."ONLINE<br/>"; } else if($usertime < ($currenttime * 60 * 10)) { echo $row['username']."OFFLINE"; } } echo "</ul>"; ?> I get 'Parse error: parse error in C:\XAMPP\htdocs\Shoutbox\config\guests_online.php on line 21' Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809503 Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 change this... <?php if($usertime < $currenttime) { echo $row['username']."IDLE<br/>"; } else { echo $row['username']."ONLINE<br/>"; } else if($usertime < ($currenttime * 60 * 10)) { echo $row['username']."OFFLINE"; } } to... <?php if($usertime < $currenttime) { echo $row['username']."IDLE<br/>"; }elseif($usertime < ($currenttime * 60 * 10)) { echo $row['username']."OFFLINE"; } else { echo $row['username']."ONLINE<br/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809506 Share on other sites More sharing options...
gaza165 Posted April 14, 2009 Author Share Posted April 14, 2009 Yeah there is something wrong with if($usertime < (time() * 60 / 10)) { it says that the user is offline when they are online i need it to check if it is 10 minutes in the past Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809511 Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 time() * 60 * 10 gives your 10 mins. Take that 10 mins away from the last logged time, and if 10mins(+unix time stamp) is less then the logged time, then they are offline. Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809514 Share on other sites More sharing options...
gaza165 Posted April 14, 2009 Author Share Posted April 14, 2009 $offlinetime = $usertime - time() * 60 * 10; if($usertime < $currenttime) { echo $row['username']."IDLE<br/>"; }elseif($usertime < $offlinetime) { echo $row['username']."OFFLINE"; } else { echo $row['username']."ONLINE<br/>"; } Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809518 Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 is that working for you? Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809522 Share on other sites More sharing options...
gaza165 Posted April 14, 2009 Author Share Posted April 14, 2009 nope this is what ive got <?php 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']); $currenttime = time(); $offlinetime = $usertime - time() * 60 * 1; if($usertime < $currenttime) { echo $row['username']."IDLE<br/>"; }elseif($usertime < $offlinetime) { echo $row['username']."OFFLINE"; } else { echo $row['username']."ONLINE<br/>"; } } echo "</ul>"; ?> it works between ONLINE and IDLE just not offline!! Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809525 Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 what is actually inserted into the last_activity column? is it time(); ? Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809527 Share on other sites More sharing options...
gaza165 Posted April 14, 2009 Author Share Posted April 14, 2009 <?php session_start(); include('dbconnect.php'); $username = $_SESSION['login']['username']; $sql = mysql_query("UPDATE users set last_activity = CURRENT_TIMESTAMP + 100 WHERE username = '$username'"); ?> this code is rang everytime a new message is sent to show that the user is still online!! Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809528 Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 why the +100 ? that means it is adding 100 seconds. Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809530 Share on other sites More sharing options...
gaza165 Posted April 14, 2009 Author Share Posted April 14, 2009 this means it will set the $usertime into the future, then they will be online!! Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809533 Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 here's a script that does what you want your script to do. Worth taking a look at to see how else this can be done. http://www.hotscripts.com/listing/a-better-whos-online-script/ Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809542 Share on other sites More sharing options...
gaza165 Posted April 14, 2009 Author Share Posted April 14, 2009 I really cannot guage anything from this as it is ASP.... What do you think is the best thing to update the time with everytime a message is sent. 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'"); ?> guestsonline.php <?php 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']); $currenttime = time(); if($usertime < $currenttime) { echo "online"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809553 Share on other sites More sharing options...
MasterACE14 Posted April 14, 2009 Share Posted April 14, 2009 sorry about the link, I assumed it was PHP :S Here is 1 way of doing it, which can be easily integrated into your own script. http://www.codewalkers.com/c/a/Counters-Code/TG-Whos-Online/ Quote Link to comment https://forums.phpfreaks.com/topic/154001-using-time-to-work-out-online-status/#findComment-809558 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.