天天看點

php統計網站通路次數的一個簡單方法

這裡主要用到了session儲存目前通路者,并将通路次數寫入本地檔案。

<?  
    @session_start();  
    $counter = intval(file_get_contents("counter.dat"));  //建立一個dat資料檔案
    if(!$_SESSION[\'#\'])  
    {  
     $_SESSION[\'#\'] = true;  
     $counter++;  //重新整理一次+1
     $fp = fopen("counter.dat","w");  //以寫入的方式,打開檔案,并指派給變量fp
     fwrite($fp, $counter);   //将變量fp的值+1
     fclose($fp);  
    }  
?>      

輸入代碼

<?php echo "$counter";?>      

案列展示

<?php
    @session_start();  
    $counter = intval(file_get_contents("counter.dat"));  
    if(!$_SESSION[\'#\'])  
    {  
     $_SESSION[\'#\'] = true;  
     $counter++;  
     $fp = fopen("counter.dat","w");  
     fwrite($fp, $counter);  
     fclose($fp);  
    }  
 ?>
<p align="center">您是到訪的第<?php echo "$counter";?>位使用者</p>