明輝手游網(wǎng)中心:是一個免費提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

用Php完成鏈結(jié)人氣統(tǒng)計

[摘要]首先要說明:這個不是主頁計數(shù)器,雖然原理和它相同。 一:準備一個計數(shù)文件,存放鏈結(jié)的url和訪問次數(shù),格式如下 id url count ...
首先要說明:這個不是主頁計數(shù)器,雖然原理和它相同。                               
一:準備一個計數(shù)文件,存放鏈結(jié)的url和訪問次數(shù),格式如下                          
id url count                                                                     
id是用來查找的索引號,url為實際文件的地址,count為次數(shù)                           
實例數(shù)據(jù):                                                                       
doc01 docs/doc01.html 10                                                         
doc02 docs/doc02.html 10                                                         
二:準備count.php文件,代碼如下:                                                
<?php                                                                            
//功能:傳入?yún)?shù)id,在計數(shù)文件查找相應(yīng)的url,增加計數(shù),并返回實際的鏈接           
$countfile="count.txt";                                                          
$lines=file($countfile);//將計數(shù)文件內(nèi)容讀到數(shù)組$lines中                         
for($i=0;$i<count($lines);$i++)                                                  
{                                                                                
list($sid, $url, $count)=explode(" ", $lines[$i]);                               
                                        //分解字符串為$id,$url,$count            
if($sid==$id) //查找指定id                                                       
{                                                                                
$count+=1; //增加計數(shù)                                                            
$lines[$i]=$sid . " " . $url . " " . "$count" . "\n";//重新生成計數(shù)字符串        
break;                                                                           
                                                                                 
}                                                                                
}                                                                                
//寫入計數(shù)信息                                                                   
$fp=fopen($countfile, "w");                                                      
for($i=0;$i<count($lines);$i++)                                                  
fputs($fp, $lines[$i]);                                                          
fclose($fp);                                                                     
Header("Location: $url");                                                        
?>                                                                               
注:這個是phpuser.com上的不完全版本,我加了一些改動,現(xiàn)在應(yīng)該是能用的。          
                                                                                 
三:主頁中超鏈結(jié)的調(diào)用方式應(yīng)該改為:                                             
<a href="count.php?id=doc01">doc01</a>                                           
程序功能是:                                                                     
首先將計數(shù)文件內(nèi)容讀到數(shù)組中                                                     
再從數(shù)組中查找指定id號,如找到則$url為相應(yīng)的文件的真正鏈接,再將計數(shù)加1,        
重新生成計數(shù)信息                                                                 
將計數(shù)信息寫入文件中                                                             
返回鏈接                                                                         
                                                                                 
四:訪問人數(shù)引用就是下面這幾句,寫個function也可以,我偷了把懶,                 
                      誰寫好了給我一個  :)                                       
<?php                                                                            
$countfile="count.txt";                                                          
$lines=file($countfile);                                                         
list($sid,$url,$count) = explode(" ",$lines[0]);//$lines[0]對應(yīng)count.txt中       
                                                //第一條記錄                     
echo $count;                                                                     
?>