下面两个代码都已测试,均能使用
代码1(每个页面都加1)
<?php
$counterFile= "counter.txt";
$fp =fopen($counterFile,"a+");
$num =fgets($fp,5);
$num +=1;
echo"您是第 <font color=red> $num </font> 位访客";
fclose($fp);
$fpp=fopen($counterFile,"w");
fwrite($fpp,$num);
fclose($fpp);
?>
代码2 (利用session,防止简单刷新)
<?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);
}
?>
<palign="center">您是到访的第<?php echo "<font color=red> $counter </font>";?>位用户</p>
本文提供了两种使用PHP实现网站访问计数器的方法:一种是通过直接读写文件递增计数,另一种则利用session机制避免因用户刷新而导致重复计数的问题。
919

被折叠的 条评论
为什么被折叠?



