This article mainly introduces php's simple method of counting online users, involving php's reading and writing of text files and related mathematical computation skills, for more information about how to count php online users, see the example in this article. We will share this with you for your reference. The details are as follows:
<? Php // first, you must have the permission to read and write files. // this program can be run directly. The first error is reported? $ Online_log = "count. dat "; // the file that saves the number of people, $ timeout = 30; // The author is not moved within 30 seconds. the file is considered to be offline $ entries = file ($ online_log ); $ temp = array (); for ($ I = 0; $ I
Time () {array_push ($ temp, $ entry [0]. ",". $ entry [1]. "\ n"); // retrieve the information of other viewers, remove the timeout, and save it to $ temp} array_push ($ temp, getenv ('remote _ ADDR '). ",". (time () + ($ timeout )). "\ n"); // update the viewer's time $ users_online = count ($ temp); // calculate the number of online users $ entries = implode ("", $ temp ); // write the file $ fp = fopen ($ online_log, "w"); flock ($ fp, LOCK_EX); // flock () fputs ($ fp, $ entries), flock ($ fp, LOCK_UN), and fclose ($ fp) cannot work normally in NFS and other network file systems ); echo "existing ". $ users_onli Ne. "people online";?>
Usage:
Save the preceding code as count. php. where to use it:
include("count.php");