Swarfega Posted January 8, 2013 Share Posted January 8, 2013 Hey. Recently did this to my site: 1 - Added a new Column to the Users Database using Hour-Minute-Second 2 - New PHP which runs everytime a user Loads a page = Updates the last_activity column 3 - New PHP Which runs everytime a suer Loads a page = Checks for Offline users and marks them as offline This works, to some degree. I'm having an issue tho. If a user doesn't logon for, lets say, 12 hours or a whole day, the system does not recognize them as having been oflfine that long, because their last saved Activity would still be something like "15:03:52" and because of this the System randomly puts them online when they're not really online. I was wondering if anyone has a decent fix for this? These are the two PHPs I'm using update_timer.php <?php session_start(); require_once('config.php'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $getdate = date('H:i:s'); $SQL = "UPDATE potatis SET last_activity='".$getdate."' WHERE uid='".$_SESSION['SESS_MEMBER_ID']."'"; mysql_query($SQL) or die(mysql_error()); ?> logout_timer.php <?php require_once('config.php'); $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } //Select database $db = mysql_select_db(DB_DATABASE); if(!$db) { die("Unable to select database"); } $getdate = date("H:i:s"); $date = explode(":", $getdate); $date[0] = $date[0] * 60 * 60; $date[1] = $date[1] * 60; $totalsecondsnow = $date[0] + $date[1] + $date[2]; $SQL = "SELECT uid, last_activity FROM potatis"; $RESULT = mysql_query($SQL); while($row = mysql_fetch_assoc($RESULT)) { $thendate = explode(":", $row['last_activity']); $thendate[0] = $thendate[0] * 60 * 60; $thendate[1] = $thendate[1] * 60; $totalsecondsthen = $thendate[0] + $thendate[1] + $thendate[2]; $totalsecondsallowed = $totalsecondsthen + 900; if($totalsecondsnow > $totalsecondsallowed) { $qry = "UPDATE potatis SET connected='offline' WHERE uid='". $row['uid'] . "'"; mysql_query($qry) or die(mysql_error()); } } ?> Quote Link to comment Share on other sites More sharing options...
MDCode Posted January 8, 2013 Share Posted January 8, 2013 Personally, I would just use the php time() function and avoid the whole collision possibility. Quote Link to comment Share on other sites More sharing options...
Swarfega Posted January 8, 2013 Author Share Posted January 8, 2013 Well I decided to simply add another Column for the Current Date Year-Month-Day, and add that to the While loop of logout-timer, so it selects only where the date=current date. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 8, 2013 Share Posted January 8, 2013 Your column needs to be a DATETIME and you can do all the filtering directly in the update query, no need to select, loop, filter, and update. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted January 8, 2013 Share Posted January 8, 2013 All that code in your logout_timer.php script would become ONE query - $qry = "UPDATE potatis SET connected='offline' WHERE last_active < NOW() - INTERVAL 15 MINUTES"; Quote Link to comment Share on other sites More sharing options...
haku Posted January 9, 2013 Share Posted January 9, 2013 You are also adding overhead to each page load by checking for and logging out users on every page load. If you really want to do this, you are best off creating a script to do this, and having it executed using cron runs in the background. 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.