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

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

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