-
<? php -
//First of all, you must have the permission to read and write files. The first access may not be displayed, but it can be refreshed normally -
$online_log = "maplers.dat" ; //Save the file of the number of people to the root directory, -
$timeout = 30; //If the author is not touched within 30 seconds, it is considered offline -
$entries = file( $online_log ); -
$temp = array (); -
for ( $i =0; $i < count ( $entries ); $i ++){ -
$entry = explode ( "," ,trim( $entries [ $i ])); -
if (( $entry [0] != getenv ('REMOTE_ADDR')) && ( $entry [1] > time())) { -
array_push ( $temp , $entry [0]. "," . $entry [1]. "\n" ); //Get the information of other browsers, remove the timeout, and save it in $temp -
}} -
array_push ( $temp , getenv ('REMOTE_ADDR'). "," .(time() + ( $timeout )). "\n" ); //Update viewer's time -
$maplers = count ( $temp ); //Calculate the number of people online -
$entries = implode( "" , $temp ); -
//Write File -
$fp = fopen ( $online_log , "w" ); -
flock ( $fp ,LOCK_EX); //Flock() does not work properly in NFS and other network file systems -
fputs ( $fp , $entries ); -
flock ( $fp ,LOCK_UN); -
fclose( $fp ); -
echo "Number of people online:" . $maplers . People ; -
?>