Jump to content

Checking if user closed window/session still in place


rofl90

Recommended Posts

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.

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 />';
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.