php 网站访问统计量

下面两个代码都已测试,均能使用

代码1(每个页面都加1)

$counterFile= "counter.txt";

$fp =fopen($counterFile,"a+");

$num =fgets($fp,5);

$num +=1;

echo"您是第 $num 位访客";

fclose($fp);

$fpp=fopen($counterFile,"w");

fwrite($fpp,$num);

fclose($fpp);

?>

 

代码2 (利用session,防止简单刷新)

    @session_start(); 

    $counter =intval(file_get_contents("counter.dat")); 

  

    if(!$_SESSION['#']) 

    { 

     $_SESSION['#'] = true; 

     $counter++; 

     $fp =fopen("counter.dat","w"); 

     fwrite($fp, $counter); 

     fclose($fp); 

    } 

 ?>

您是到访的第 $counter ";?>位用户

你可能感兴趣的:(wordpress开发)