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;
?>