Check if user is still there

Hi all,

Not sure if I posted this in the right area since its a Javascript/PHP related problem.

I have no clue how to check if a user didn’t leave the site.
Ofcourse you can use the Onunload() or Beforeunonload() function.
But some browsers like Opera and Safari and who know what other browsers don’t support it. And I don’t want to have false information about users still being there while they left.

Is there any other alternative?
Like expecting server side each X seconds a AJAX request from the user?
If the user doesn’t send a request, the user has left.

Any idea how to check this server side?



I am now trying to do this with sessions.
Each time the chat updates it issues a request to a php script wich handles the chat updating. But I also want to tell the script the session needs to be reset with a new time. Since the update interval of the AJAX request is 3 seconds.
I have set the SESSION expire on 5 seconds.

If server side the SESSION is expired and the user doesnt send a update to reset it. Remove his IP from a text file.

if (isset($_SESSION['LAST_ACTIVITY']) && (time() -  $_SESSION['LAST_ACTIVITY'] > 15)) {
session_destroy();   // destroy session data in storage
session_unset();     // unset $_SESSION variable for the runtime
$current_viewers = file('currentviewers.txt');
if(count($current_viewers) != 0){
if(in_array($ip. "\
", $current_viewers)){
$key = array_search($ip . "\
", $current_viewers);
file_put_contents("currentviewers.txt", "");
$fp = fopen("currentviewers.txt", w);
foreach($current_viewers as $key => $value){
fwrite($fp, $value);

This is not working though.
Any ideas?

We use some simple php code to keep track of online operators for a video messenger chat. This may be of use.

The chat application calls a status script from time to time and this saves a file with online username:

$dfile = fopen($dir.“/$session”,“w”);

Then based on that file datetime, keep track of online operators and return a list of operators online in that department (room):


if (file_exists(“$dir/$file/online”))
if (is_dir(“$dir/$file/online”))
while (($file2 = readdir($h2))!==false) //scan all online operators
if (!is_dir(“$dir/$file/online/$file2”))
if ($ftime>$onlinetime)
$oplist.= ($oplist?“, “:””) . implode(file(“$dir/$file/online/$file2”));
else @unlink(“$dir/$file/online/$file2”); //remove offline operator

		if ($opcount) 
		$html.="<a href='$url/client.php?department=$file' target='_blank'>$imgonline <b>$file</b> is online.</a>";
		$html.="$imgoffline <b>$file</b> is offline.";
	} else;
	else $html.="$imgoffline <b>$file</b> is currently offline.";

You can download the php live video support edition for free from our site if you want to take a look at full php code.