Using PHP to realize link popularity statistics

Source: Internet
Author: User
Tags count explode php file prepare
Let's start by saying that this is not a home counter, although the principle is the same as it is.
One: Prepare a count file, store the link URL and the number of visits, the format is as follows
Id|url|count
ID is the index number to find, URL is the address of the actual file, count is the number of times
Instance data:
Doc01|docs/doc01.html|10
Doc02|docs/doc02.html|10
Two: Prepare the count.php file, the code is as follows:
<?php
Function: Pass in the parameter ID, find the corresponding URL in the Count file, increment the count, and return the actual link
$countfile = "Count.txt";
$lines =file ($countfile);//Read the contents of the count file into the array $lines
for ($i =0; $i <count ($lines); $i + +)
{
List ($sid, $url, $count) =explode ("|", $lines [$i]);
The decomposition string is $id, $url, $count
if ($sid = = $id)//find the specified ID
{
$count +=1; Increase Count
$lines [$i]= $sid. "|" . $url. "|" . "$count". "\ n";//Regenerate Count string
Break

}
}
Write count information
$FP =fopen ($countfile, "w");
for ($i =0; $i <count ($lines); $i + +)
Fputs ($fp, $lines [$i]);
Fclose ($FP);
Header ("Location: $url");
?>
Note: This is the incomplete version on the phpuser.com, I added some changes, it should be able to use now.

Third: The hyperlink in the home page should be called to:
<a href= "COUNT.PHP?ID=DOC01" >doc01</a>
Program features are:
First read the contents of the count file into the array
Then look for the specified ID number from the array, such as find the true link to the $url file, and then add the count to 1,
Regenerate count information
Write count information to the file
Back to link

Four: The number of visitors quoted is the following sentences, write a function can also, I stole the lazy,
Who wrote it, give me a:)
<?php
$countfile = "Count.txt";
$lines =file ($countfile);
List ($sid, $url, $count) = Explode ("|", $lines [0]);//$lines [0] corresponds to Count.txt
First record
Echo $count;
?>




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.