DeanWhitehouse Posted July 15, 2008 Share Posted July 15, 2008 I have a code to display an online list, that checks if someone is online in the db, how can i make it change to offline after a certain amount of inactivity? Quote Link to comment Share on other sites More sharing options...
samshel Posted July 15, 2008 Share Posted July 15, 2008 store login time... and run a cron to logout all customer whose start up time is greater than specified time ? Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted July 15, 2008 Author Share Posted July 15, 2008 ok, one i have never done crons, and two is there not a way to do it without them? Quote Link to comment Share on other sites More sharing options...
DoddsAntS Posted July 15, 2008 Share Posted July 15, 2008 Store their last click time and then use this to filter the results of your query/ Quote Link to comment Share on other sites More sharing options...
mmarif4u Posted July 15, 2008 Share Posted July 15, 2008 For cronjob read my blog: http://anl4u.freeweb7.com/blog/index.php?cat=13 And i think no other way to do it.you can use a logout button for users.but that is not time specific and also not reliable because some time user close browser,so the online field in table will always 1. Quote Link to comment Share on other sites More sharing options...
samshel Posted July 15, 2008 Share Posted July 15, 2008 i think cron is best way to do it, because you want something asyncronous that will reset your flags. Crons are simple PHP script, which run periodically as per settings done..so i think that is safest way. you can get to know more about crons here http://www.unixgeeks.org/security/newbie/unix/cron-1.html also google around for more info. Example would be entry in crontab like 30 * * * * php /path/to/file/resetting/flag.php Your file will run every 30 mins. Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted July 15, 2008 Author Share Posted July 15, 2008 would it work if i just check for a cookie, with out a short timeout , that is set on everypage? Quote Link to comment Share on other sites More sharing options...
samshel Posted July 15, 2008 Share Posted July 15, 2008 What is the user does not refresh his page for 2 hours...the code which checks the cookie will be executed after 2 hours.. and your user wont log out till then also if the browser is closed directly the flag is not set to logout... Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted July 15, 2008 Author Share Posted July 15, 2008 going to go with DoddsAntS idea. i have got the db table to use a timestamp, now how can i check how long ago they where active? Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted July 15, 2008 Author Share Posted July 15, 2008 how can i check to see if they have be inactive for more than two minutes, using a mysql timestamp as the time? Quote Link to comment Share on other sites More sharing options...
DoddsAntS Posted July 15, 2008 Share Posted July 15, 2008 Hi, Add some filtering to your query (Where clause). if you have any issues post again but try first Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted July 15, 2008 Author Share Posted July 15, 2008 ok, i judst had an idea to do it, need some help with logic though i am going to get the timestamp from the db, then do if($timestamp <= date("") - 2) { } would that work, and what would i do in the date ? do i need to convert the time stamp fisrt? Quote Link to comment Share on other sites More sharing options...
DoddsAntS Posted July 15, 2008 Share Posted July 15, 2008 Do the filtering in the query, that way you dont have to loop through all the rows, are you using a unixtime stamp or a date/time stamp ? Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted July 15, 2008 Author Share Posted July 15, 2008 this is the main part of the code <?php $sql = "SELECT * FROM users WHERE lastactive = '???' LIMIT $offset, $rowsperpage"; $result = mysql_query($sql); // while there are rows to be fetched... while ($list = mysql_fetch_assoc($result)) { if($list['lastactive'] date // echo data echo "<tr><td class='NormalContent' style='border:thin solid #AA4B00;'><a href='users.php?id=".$list['ID']."'>".$list['Username']."</a></td></tr>"; } // end while ?> timestamp is a mysql one. displayed as 2008-07-15 04:32:21 Quote Link to comment Share on other sites More sharing options...
DoddsAntS Posted July 15, 2008 Share Posted July 15, 2008 okay, take a look at mktime() function strotime() function date() function you want to make a time stamp that is x mins in the past and check for lastactive times greater than that, you'll need to format the timestamp so it displays like the mysql timestamp. Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted July 15, 2008 Author Share Posted July 15, 2008 ok, still not sure how to do it, would i do something like this $date = date("hours,min,seconds");//odviously correct terms then, but something like this? $sql = "SELECT * FROM users WHERE lastactive <= '$date' LIMIT $offset, $rowsperpage"; $result = mysql_query($sql); // while there are rows to be fetched... while ($list = mysql_fetch_assoc($result)) { // echo data echo "<tr><td class='NormalContent' style='border:thin solid #AA4B00;'><a href='users.php?id=".$list['ID']."'>".$list['Username']."</a></td></tr>"; } Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted July 15, 2008 Author Share Posted July 15, 2008 is this the correct thing $time = mktime(date("s"), date("i")-2, date("h"), date("m"), date("d"), date("Y")); $sql = "SELECT * FROM users WHERE lastactive >= '$time' LIMIT $offset, $rowsperpage"; $result = mysql_query($sql); // while there are rows to be fetched... while ($list = mysql_fetch_assoc($result)) { // echo data echo "<tr><td class='NormalContent' style='border:thin solid #AA4B00;'><a href='users.php?id=".$list['ID']."'>".$list['Username']."</a></td></tr>"; } Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted July 15, 2008 Author Share Posted July 15, 2008 i think it works, not sure though Quote Link to comment Share on other sites More sharing options...
DoddsAntS Posted July 15, 2008 Share Posted July 15, 2008 Almost, something like $time = date("Y-m-d H:i:s", strtotime("-2 mins")); will give you the correct format for the timestamp Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted July 15, 2008 Author Share Posted July 15, 2008 ok, thanks for all the help, i think it works now, 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.