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 | countid is the index number used for searching. the url is the actual file address, count is the number of instance data: doc01 | docsdoc01.html | 10doc02 | docsdoc02.html | 10 2: Prepare count. 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:
// 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 {
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 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:
Doc01
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 :)
$ 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.