一个简单的文本型计数器的代码:
 
复制代码 代码示例:
<?php
$f=fopen("count.dat","r");
$c=fread($f,3);
echo '人数:'.$c;
fclose($f);
$f=fopen("count.dat","w");
fwrite($f,$c);
fclose($f);
?>
另外,为大家提供三个php文本计数器的实例代码,有需要的朋友,参考下吧。
php计数器代码1,
 
复制代码 代码示例:
<?php 
//计数器 
function countx($file="count.dat"){ 
if(
file_exists($file)){ 
$fp=fopen($file,"r"); 
$numx=fgets($fp,10); 
fclose($fp); 
$numx++; 
//以上四行代码可以用一条表达式代替:$numx=
file_get_contents($file)+1; 
} 
else{ 
$numx=1;} 
file_put_contents($file,$numx);//当文件不存在时,这函数会自动创建文件,而且会自动把参数转成字符串写入。 
echo $numx; 
/*整个函数体可以用两条表达式代替:file_exists($file)?file_put_contents($file,file_get_contents($file)+1):file_put_contents($file,"1");readfile($file);
*/ 
} 
//函数调用 
countx(); 
?>
php计数器代码2,
 
复制代码 代码示例:
<?php
  $counterfile = "balong.txt";//存储数值的文件名几路径 
   
  function displaycounter($counterfile) { 
   $fp = fopen($counterfile,"rw"); 
   $num = fgets($fp,5); 
   $num += 1; 
   print "您是第 "."$num"." 位访问者"; 
   exec( "rm -rf $counterfile"); 
   exec( "echo $num > $counterfile"); 
  } 
   
  if (!file_exists($counterfile)) { 
   exec( "echo 0 > $counterfile"); 
  } 
   
  displaycounter($counterfile); 
?>
php计数器代码3,
 
复制代码 代码示例:
<?php
  $counterfile = "www.jb200.com.txt";//存储数值的文件名几路径 
   
  function displaycounter($counterfile) { 
   $fp = fopen($counterfile,"rw"); 
   $num = fgets($fp,5); 
   $num += 1; 
   print "您是第 "."$num"." 位访问者"; 
   exec( "rm -rf $counterfile"); 
   exec( "echo $num > $counterfile"); 
  } 
   
  if (!file_exists($counterfile)) { 
   exec( "echo 0 > $counterfile"); 
  } 
   
  displaycounter($counterfile); 
?>