Following is my "users online" PHP script. I have no db access and, because of the mulitlingual architecture of the site (TCL, PHP, XML, SSI-based HTML, etc.) sessions are pretty much out of the picture. Well, here it is:
<?php
require_once("$DOCUMENT_ROOT/php_global_vars.php");
// OBTAIN EXISTING XML FILE FOR USERS
$fileID = @fopen("$DOCUMENT_ROOT/xml/users.xml", 'r');
if ($fileID) {
$stuff = fread($fileID, filesize("$DOCUMENT_ROOT/xml/users.xml"));
fclose($fileID);
}
$usersArray = array();
if (strlen($stuff) > 0) {
$parser = xml_parser_create();
xml_parse_into_struct($parser, $stuff, $usersArray, $tags);
xml_parser_free($parser);
}
//-----------------------------------------------------------------------------------
/*-------------------------------------------------------------------------------------
Check to see how many current users there are online by counting the XML rows where
the timestamp is < 20 minutes from the current time. If their time has not yet
expired you will increase $usersKount as well as add them into a new XML string
variable to repopulate users.xml
---------------------------------------------------------------------------------------*/
$userKount = 0; $foundYou = 0;
for ($i = 1; $i < sizeof($usersArray); $i++) {
if (time() - $usersArray[$i]['attributes']['TIMESTAMP'] <= 1200) {
$usersKount++;
$xmlString .= '<user ip="' . $usersArray[$i]['attributes']['IP'] . '"' .
' timestamp="' . $usersArray[$i]['attributes']['TIMESTAMP'] . '">' .
'</user>';
}
if ($usersArray[$i]['attributes']['IP'] === $REMOTE_ADDR) $foundYou = 1;
}
if (!$foundYou) $usersKount++; // INCLUDE YOU IN COUNT IF YOU ARE NOT ALREADY IN XML
if (!$foundYou && $remoteAddr === $SERVER_ADDR) $usersKount--;
//---END OF COUNT BLOCK----------------------------------------------------------------
/*---------------------------------------------------------------------------------------
Reformat users.xml with the new values in $xmlString and include you into the string
with current time
----------------------------------------------------------------------------------------*/
if (!$foundYou && $SERVER_ADDR !== $REMOTE_ADDR)
$xmlString .= '<user ip="' . $REMOTE_ADDR . '" timestamp="' . time() . '"></user>';
$xmlString = '<?xml version="1.0" encoding="utf-8" ?><users>' .
$xmlString . '</users>';
$fileID = @fopen("$DOCUMENT_ROOT/xml/users.xml", 'w')
or die("Could not open $DOCUMENT_ROOT/xml/users.xml for writing");
fputs($fileID, $xmlString); fflush($fileID); fclose($fileID);
//---END OF XML FILE REFORMATTING BLOCK-------------------------------------------------
// DISPLAY MESSAGE WITH COUNT
$usersString .= $font . 'There ';
$usersString .= ($usersKount == 0 || $usersKount > 1) ? 'are ' : 'is ';
$usersString .= "<b>$usersKount</b> user";
$usersString .= ($usersKount == 0 || $usersKount > 1) ? 's ' : ' ';
$usersString .= "online - Det finns <b>$usersKount</b> bruke";
$usersString .= ($usersKount == 0 || $usersKount > 1) ? 'r ' : ' ';
$usersString .= 'online</font><p>';
echo $usersString;
?>
And on occasions, you will see this on my site upon viewing it (http://valsignalandet.com):
There are users online - Det finns bruker online
Notice what's missing? No number! When I view the actual XML file containing the various users' IP addresses I get this:
<?xml version="1.0" encoding="utf-8" ?><users></users>
Based on what you see so far, any folks out there have some insight as to why this occasionally occurs? It doesn't break the site, nor does it always happens, in fact, upon refreshing ( in less than 20 minutes) the XML file now looks like this:
<?xml version="1.0" encoding="utf-8" ?><users><user ip="68.x.x.x" timestamp="1070736790"></user></users>
(Actual IP disguised to protect the innocent manually by me right now)
And on the site you rightfully see this:
There is 1 user online - Det finns 1 bruke online
Help appreciated! Thanx
Phil