Lamez Posted January 14, 2010 Share Posted January 14, 2010 Back, again, So I have this function to check users if they are inactive for a certain length of time, then it marks them as offline, and vise-versa (did I spell that right?). The problem is it is marking everyone as offline! I checked the logic, and the queries. It all checks out. I do not grasp the complete concept of the time() function, other than it produces some sorta time stamp. From my understanding, it is in seconds. So to get a 5 minuets ahead you would do: time()+(5*60); Right? Well any who, here is my function: <?php //Note: The echos are for debuggin' purposes function checkStaleUsers($checkInt, $idleTime){ $time = time(); if(!isset($_SESSION['~serverTimeCheck'])){ $_SESSION['~serverTimeCheck'] = time()+($checkInt*60); } if(time() >= $_SESSION['~serverTimeCheck']){ unset($_SESSION['~serverTimeCheck']); $q = mysql_query("SELECT * FROM ".TBL_PEOPLE." WHERE timestamp > '0' AND activated = '1' AND ban = '0'"); $n = mysql_num_rows($q); if($n > 0){ while($f = mysql_fetch_array($q)){ if($f['timestamp'] <= $time+($idleTime*60) && $f['online'] == 1){ mysql_query("UPDATE ".TBL_PEOPLE." SET online = '0' WHERE id = '".$f['id']."'"); echo "SET OFFLINE FOR: ".$f['email']."<BR>"; }else if($f['timestamp'] >= $time+($idleTime*60) && $f['online'] == 0){ mysql_query("UDPATE ".TBL_PEOPLE." SET online = '1' WHERE id = '".$f['id']."'"); echo "SET ONLINE FOR: ".$f['email']."<BR>"; }else{ echo $f['online']; echo "<BR>"; echo $f['email']; echo "<BR>"; echo $f['timestamp']; echo "<BR>"; echo $time; echo "<HR>"; } } } } } checkStaleUsers(5, 10); //Check every five minuets for users that are idle for ten minuets or more. ?> -Thanks, again. Quote Link to comment https://forums.phpfreaks.com/topic/188468-is-it-my-math-i-bet-it-is-my-math-it-is-always-my-math/ Share on other sites More sharing options...
ignace Posted January 14, 2010 Share Posted January 14, 2010 What's wrong with a simple query? $q = 'UPDATE ' . TBL_PEOPLE . ' SET online = 0 WHERE timestamp + 300 < now()'; Quote Link to comment https://forums.phpfreaks.com/topic/188468-is-it-my-math-i-bet-it-is-my-math-it-is-always-my-math/#findComment-995007 Share on other sites More sharing options...
Lamez Posted January 14, 2010 Author Share Posted January 14, 2010 I consider my self semi-advanced, drifting from the noob stage. I know more PHP than I do SQL, so if the code could be condense, I am all for it. Could you explain your query to me a little more? What is this timestamp + 300?, what is this now(), is it like time()? -Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/188468-is-it-my-math-i-bet-it-is-my-math-it-is-always-my-math/#findComment-995011 Share on other sites More sharing options...
PHP Monkeh Posted January 14, 2010 Share Posted January 14, 2010 now() will insert a timestamp in to the field for literally, right now. So when you insert the user as being "online", use now() to generate the timestamp. What ignace's query does, is compares the timestamp field, plus 300 (60 seconds x 5 for 5 minutes) to the time now. So if when they logged in, plus 5 minutes, is less than the time now - they're no longer logged in. Hopefully that makes a bit more sense ! Quote Link to comment https://forums.phpfreaks.com/topic/188468-is-it-my-math-i-bet-it-is-my-math-it-is-always-my-math/#findComment-995029 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.