SoFunction
Updated on 2025-04-09

Use Php to achieve link popularity statistics

First of all, we need to explain: this is not a home page counter, although the principle is the same as it.
1: Prepare a counting file to store the link's URL and number of visits, the format is as follows.
id|url|count                                                                     
id is the index number used to search, url is the address of the actual file, and count is the number of times
Example data:
doc01|docs/|10                                                         
doc02|docs/|10                                                         
2: Prepare the file, the code is as follows:
<?php                                                                            
//Function: Pass in the parameter id, find the corresponding url in the count file, increase the count, and return the actual link.
$countfile="";                                                          
$lines=file($countfile);//Read the count file content into the array $lines
for($i=0;$i<count($lines);$i++)                                                  
{                                                                                
list($sid, $url, $count)=explode("|", $lines[$i]);                               
//Decompose the string to $id,$url,$count
if($sid==$id) //Find the specified id                                                                                                                      �
{                                                                                
$count+=1; //Increase the count
$lines[$i]=$sid . "|" . $url . "|" . "$count" . "\n";//Regenerate counting 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 version. I added some changes and it should be useful now.

Three: The call method of the Super Link in the homepage should be changed to:                                                                                                                      �
<a href="?id=doc01">doc01</a>                                           
The program functions are:
First read the count file content into the array
Then look for the specified id number from the array. If you find it, $url is the real link to the corresponding file, and then add the count by 1.
Regenerate count information
Write count information into file
Return to link

4: The number of visitors quotes is the following sentences. It’s okay to write a function. I’m lazy.
Whoever wrote it and gave me one :)
<?php                                                                            
$countfile="";                                                          
$lines=file($countfile);                                                         
list($sid,$url,$count) = exploit("|",$lines[0]);//$lines[0] corresponds to
//First record                                                                                                                            �
echo $count;                                                                     
?>