<html><body>
<?php
 $file=array();
 if($fp=@fopen("./log.dat","r")){
  flock($fp,1);
  while($buf=fgets($fp)){
   array_push($file, $buf);
  }
  fclose($fp);
 }
 $counter=trim($file[count($file)-1]);
 printf("we have ".$counter." visitors\r\n");
 array_push($file,($counter+1)."\r\n");
 if($fp = fopen("./log.dat", "w")){
  flock($fp, 2);
  foreach($file as $f){
   fprintf($fp, $f);
  }
 fclose($fp);
}
?>
</body></html>
のような簡単なPHPプログラムを作ったのですが、
どういうわけか1アクセスごとにカウントが2増えます。
教本殆どそのままなのに、なんでこんな事が起きるのかチンプンカンプンです。
原因と解決法を教えて頂けないでしょうか