Use Php to achieve link popularity statistics

Source: Internet
Author: User

First, you must note that this is not a homepage counter, although the principle is the same as it.
1. Prepare a counting file to store the link url and the number of visits. The format is as follows:
Id | url | count
Id is the index number used for searching. The url is the address of the actual file, and count is the number of times.
Instance data:
Doc01 | docs/doc01.html | 10
Doc02 | docs/doc02.html | 10
2. Prepare the count. php file. The Code is as follows:
<? Php
// Function: input the parameter id, search for the corresponding url in the counting file, increase the count, and return the actual link
$ Countfile = "count.txt ";
$ Lines = file ($ countfile); // read the content of the counting file to the array $ lines.
For ($ I = 0; $ I <count ($ lines); $ I ++)
{
List ($ sid, $ url, $ count) = explode ("|", $ lines [$ I]);
// The string to be decomposed is $ id, $ url, $ count
If ($ sid = $ id) // find the specified id
{
$ Count + = 1; // increase the count
$ Lines [$ I] = $ sid. "|". $ url. "|". "$ count". "\ n"; // regenerate the 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 an incomplete phpuser.com version. I added some changes and it should be usable now.

3. The method of calling the super link on the home page should be changed:
<A href = "count. php? Id = doc01 "> doc01 </a>
Program functions:
First, read the content of the counting file to the array.
Find the specified ID number from the array. If yes, $ url is the real link of the corresponding file, and add the count to 1,
Generate counting information again
Write the Count information to the file.
Return Link

4. the reference of the number of accessors is the following sentence. You can write a function. I am so lazy,
Who wrote it for me :)
<? Php
$ Countfile = "count.txt ";
$ Lines = file ($ countfile );
List ($ sid, $ url, $ count) = explode ("|", $ lines [0]); // $lines[0] count.txt
// The 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.