Principle: calculate the number of online users based on different IP addresses. Implementation Method: you can use a database or text. Here I use text implementation .? Php/*** @ Date2010.04.07 * @ Author Huaxia star PHP100.com * @ Bloghttp: // hi.baidu.com/woaidelphi/blog#/$user
Principle: calculate the number of online users based on different IP addresses.
Implementation Method: you can use a database or text.
Here I use text implementation.
/**
* @ Date 2010.04.07
* @ Author Huaxia star PHP100.com
* @ Blog http://hi.baidu.com/woaidelphi/blog
*/
$ User_online = "count. php"; // The number of objects that are stored
Touch ($ user_online); // If this file is not available, create
$ Timeout = 30; // The author is not moved within 30 seconds.
$ User_arr = file_get_contents ($ user_online );
$ User_arr = explode ('#', rtrim ($ user_arr, '#'); print_r ($ user_arr );
$ Temp = array ();
Foreach ($ user_arr as $ value ){
$ User = explode (",", trim ($ value ));
If ($ user [0]! = Getenv ('remote _ ADDR ') & ($ user [1]> time ())) {// if the user's IP address does not time out, it will be placed in the array
Array_push ($ temp, $ user [0]. ",". $ user [1]);
}
}
Array_push ($ temp, getenv ('remote _ ADDR '). ",". (time () + ($ timeout )). '#'); // save the user's information
$ User_arr = implode ("#", $ temp );
// Write a file
$ Fp = fopen ($ user_online, "w ");
Flock ($ fp, LOCK_EX); // flock () cannot work normally in NFS or some other network file systems
Fputs ($ fp, $ user_arr );
Flock ($ fp, LOCK_UN );
Fclose ($ fp );
Echo "currently". count ($ temp). "online ";
?>