PHP Simple Statistics Online number of methods _php tips

Source: Internet
Author: User
Tags flock

This article illustrates a simple way to count the number of people online in PHP. Share to everyone for your reference, specific as follows:

<?php
//First of all you have to read and write the permissions of the file//the
program can be directly run, the first error, to fou painting?
$online _log = "Count.dat"; Save the number of files,
$timeout = 30;//30 seconds did not move the author, that the line
$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 "); Remove the other viewer's information and remove the timeout and save it in $temp
 }
Array_push ($temp, getenv (' remote_addr '). ",". ( Time () + ($timeout)). " \ n "); Update the viewer's time
$users _online = count ($temp);//Count online number
$entries = Implode ("", $temp);
Write file
$fp = fopen ($online _log, "w");
Flock ($FP, LOCK_EX); Flock () cannot work properly in NFS and other network file systems
fputs ($FP, $entries);
Flock ($FP, lock_un);
Fclose ($FP);
echo "currently has". $users _online. " People online ";
? >

How to use:

Save the above code as count.php in the location you want to use:

Include ("count.php");

More interested in PHP related content readers can view the site topics: "PHP file Operation Summary", "PHP operation and operator Usage Summary", "PHP Network Programming Skills Summary", "Basic PHP Grammar Introduction Tutorial", "PHP operation Office Document skills Summary (including Word, Excel,access,ppt), "The PHP date and time usage summary", "PHP object-oriented Programming Introduction Tutorial", "PHP string (String) Usage Summary", "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

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.