|
Function getIpAddress () {// obtain the IP address of the current user
If (getenv ('http _ CLIENT_IP ')){
$ Ip = getenv ('http _ CLIENT_IP ');
} Elseif (getenv ('http _ X_FORWARDED_FOR ')){
$ Ip = getenv ('http _ X_FORWARDED_FOR ');
} Elseif (getenv ('remote _ ADDR ')){
$ Ip = getenv ('remote _ ADDR ');
} Else {
$ Ip = $ _ SERVER ['remoe _ ADDR '];
}
Return $ ip;
}
Function writeover ($ filename, $ data, $ method = 'W', $ chmod = 0 ){
$ Handle = fopen ($ filename, $ method );
! Handle & die ("file opening failed ");
Flock ($ handle, LOCK_EX );
Fwrite ($ handle, $ data );
Flock ($ handle, LOCK_UN );
Fclose ($ handle );
$ Chmod & @ chmod ($ filename, 0777 );
}
Function count_online_num ($ time, $ ip ){
$ FileCount = './count.txt ';
$ Count = 0;
$ Gap = 900; // Click it in 15 minutes.
If (! File_exists ($ fileCount )){
$ Str = $ time. "\ t". $ ip. "\ r \ n ";
Writeover ($ fileCount, $ str, 'W', 1 );
$ Count = 1;
} Else {
$ Arr = file ($ fileCount );
$ Flag = 0;
Foreach ($ arr as $ key => $ val ){
$ Val = trim ($ val );
If ($ val! = ""){
List ($ when, $ seti) = explode ("\ t", $ val );
If ($ seti ==$ ip ){
$ Arr [$ key] = $ time. "\ t". $ seti;
$ Flag = 1;
} Else {
$ CurrentTime = time ();
If ($ currentTime-$ when> 900 ){
Unset ($ arr [$ key]);
} Else {
$ Arr [$ key] = $ val;
}
}
}
}
If ($ flag = 0 ){
Array_push ($ arr, $ time. "\ t". $ ip );
}
$ Count = count ($ arr );
$ Str = implode ("\ r \ n", $ arr );
$ Str. = "\ r \ n ";
Writeover ($ fileCount, $ str, 'W', 0 );
Unset ($ arr );
}
Return $ count;
}
$ Time = time ();
$ Ip = getIpAddress ();
$ Online_num = count_online_num ($ time, $ ip );
Echo $ online_num;
?>
|