rofl90 Posted March 7, 2008 Share Posted March 7, 2008 I have a user system, but how do I check if a user has randomly closed so I an set offline to them being offline? Link to comment https://forums.phpfreaks.com/topic/94923-checking-if-user-closed-windowsession-still-in-place/ Share on other sites More sharing options...
deadonarrival Posted March 7, 2008 Share Posted March 7, 2008 Whenever any script runs insert the current timestamp into a table (the user table would do) with the field "lastclick" Then when checking for offline status, see if their last click is more than 300 seconds ago (for 5 mins, just number of minutes * 60 for any other value) and if so, display offline. Link to comment https://forums.phpfreaks.com/topic/94923-checking-if-user-closed-windowsession-still-in-place/#findComment-486236 Share on other sites More sharing options...
pocobueno1388 Posted March 7, 2008 Share Posted March 7, 2008 Your going about checking if they are on/offline the wrong way. Here is the best way of doing it: Make a field in the users table called "last_active" as a datetime On every page update that field with the current time, using the function NOW() (example below). This code is best put in a header file that is included on every page. To check which users, or if a specific user is online, it only takes a simple query. I will also give examples of this. EXAMPLES: To update the last_active field in the database UPDATE users SET last_active=NOW() WHERE userID=users_ID_Here Here is how you would display if someone is currently online (meaning they have clicked within the last 5 minutes). <?php $query = mysql_query("SELECT COUNT(*) FROM users WHERE last_active > (NOW() - INTERVAL 5 MINUTE) AND userID=users_ID_here"); $num = mysql_result($query, 0); if ($num > 0){ echo "User is ONLINE"; } else { echo "User is OFFLINE"; } ?> Here is how to get ALL users online within the last 5 minutes <?php $query = mysql_query("SELECT username FROM users WHERE last_active > (NOW() - INTERVAL 5 MINUTE)"); while ($row = mysql_fetch_assoc($query)){ echo $row['username'] . '<br />'; } ?> Link to comment https://forums.phpfreaks.com/topic/94923-checking-if-user-closed-windowsession-still-in-place/#findComment-486239 Share on other sites More sharing options...
rofl90 Posted March 7, 2008 Author Share Posted March 7, 2008 Thankyou for your very thorough response, it worked like a charm! Thankyou again! Link to comment https://forums.phpfreaks.com/topic/94923-checking-if-user-closed-windowsession-still-in-place/#findComment-486275 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.