Number of people online with text statistics

Source: Internet
Author: User
Tags count flock mysql database
Statistics | online | number online

This method does not need the MySQL database support, uses the ordinary text to realize the counting function

First, you have permission to read and write files.
This procedure can be run directly, the first error, to Fou painted tip?
$online _log = "Count.dat"; Save the number of documents,
$timeout = 30;//30 in seconds without moving the author, who thinks that the drop line
$entries = File ($online _log);

$temp = Array ();

for ($i =0; $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); Calculate online numbers

$entries = Implode ("", $temp);
Write to File
$fp = fopen ($online _log, "w");
Flock ($FP, LOCK_EX); Flock () does not work in NFS and some other network file systems
Fputs ($fp, $entries);
Flock ($FP, lock_un);
Fclose ($FP);

echo "currently has". $users _online. " People online ";

?>

How to use:

1. Save the above code as count.php

2. In the required page to introduce counters can be



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.