Using PHP to achieve link popularity statistics _php tutorial

Source: Internet
Author: User
The first thing to note: This is not the homepage counter, although the principle and it is the same.
A: Prepare a count file, store the URL of the link and the number of visits, formatted as follows
Id|url|count
The ID is used to find the index number, the URL is the address of the actual file, count is the number of
Instance data:
Doc01|docs/doc01.html|10
Doc02|docs/doc02.html|10
Two: Prepare the count.php file with the following code:
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);//reads the contents of the count file into the array $lines
for ($i =0; $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 Fputs ($fp, $lines [$i]);
Fclose ($FP);
Header ("Location: $url");
?>
Note: This is an incomplete version on phpuser.com, I added some changes, it should be available now.

Third: The hyperlink in the home page should be called as follows:
Doc01
The program functions are:
First, read the contents of the count file into the array
Then find the specified ID number from the array, if found the true link to the corresponding file, and then add 1 to the count,
Regenerate count information
Writes the count information to the file
Return link

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


http://www.bkjia.com/PHPjc/315455.html www.bkjia.com true http://www.bkjia.com/PHPjc/315455.html techarticle The first thing to note: This is not the homepage counter, although the principle and it is the same. A: Prepare a count file, store the link URL and the number of visits, format as follows Id|url|count ID is used to check ...

  • 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.