Php's simple method of counting online users

Source: Internet
Author: User
Tags flock php online
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");

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.