程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 用Php實現鏈結人氣統計

用Php實現鏈結人氣統計

編輯:關於PHP編程

首先要說明:這個不是主頁計數器,雖然原理和它相同。                               
一:准備一個計數文件,存放鏈結的url和訪問次數,格式如下                          
id|url|count                                                                     
id是用來查找的索引號,url為實際文件的地址,count為次數                           
實例數據:                                                                       
doc01|docs/doc01.html|10                                                         
doc02|docs/doc02.html|10                                                         
二:准備count.php文件,代碼如下:                                                
<?php                                                                            
//功能:傳入參數id,在計數文件查找相應的url,增加計數,並返回實際的鏈接           
$countfile="count.txt";                                                          
$lines=file($countfile);//將計數文件內容讀到數組$lines中                         
for($i=0;$i<count($lines);$i++)                                                  
{                                                                                
list($sid, $url, $count)=explode("|", $lines[$i]);                               
                                        //分解字符串為$id,$url,$count            
if($sid==$id) //查找指定id                                                       
{                                                                                
$count+=1; //增加計數                                                            
$lines[$i]=$sid . "|" . $url . "|" . "$count" . "\n";//重新生成計數字符串        
break;                                                                           

}                                                                                
}                                                                                
//寫入計數信息                                                                   
$fp=fopen($countfile, "w");                                                      
for($i=0;$i<count($lines);$i++)                                                  
fputs($fp, $lines[$i]);                                                          
fclose($fp);                                                                     
Header("Location: $url");                                                        
?>                                                                               
注:這個是phpuser.com上的不完全版本,我加了一些改動,現在應該是能用的。          

三:主頁中超鏈結的調用方式應該改為:                                             
<a href="count.php?id=doc01">doc01</a>                                           
程序功能是:                                                                     
首先將計數文件內容讀到數組中                                                     
再從數組中查找指定id號,如找到則$url為相應的文件的真正鏈接,再將計數加1,        
重新生成計數信息                                                                 
將計數信息寫入文件中                                                             
返回鏈接                                                                         

四:訪問人數引用就是下面這幾句,寫個function也可以,我偷了把懶,                 
                      誰寫好了給我一個  :)                                       
<?php                                                                            
$countfile="count.txt";                                                          
$lines=file($countfile);                                                         
list($sid,$url,$count) = explode("|",$lines[0]);//$lines[0]對應count.txt中       
                                                //第一條記錄                     
echo $count;                                                                     
?>                                                                              


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved