<?php$file = "counter.txt";
//File name//That's itif( ! file_exists( "$file" ) ) {
touch( "$file" );
chmod("$file",0777);
$fp=fopen("$file","r+");
fseek($fp,0);
$compteur = 0;
fputs($fp,$compteur);
}if( is_readable( "$file") and is_writable( "$file") ) {$fp=fopen("$file","r+");
$compteur=fgets($fp,9999);
fseek($fp,0);
$compteur++;
fputs($fp,$compteur);
fclose($fp);
print $compteur;
} else {
chmod("$file",0777);
}?>

解决方案 »

  1.   

    去搜前面的帖子!
    贴过n次了。唉。$userip=$REMOTE_ADDR;//访问计数
    $row3=@mysql_fetch_array(@mysql_query("select * from homepage where title='count'"));
    if($row3[content]<>$userip) @mysql_query("update homepage set hit=hit+1,content='$userip' where title='count'");
    这是数据库类型的。自建一个表先。
      

  2.   

    楼上的两位已经把两种不同方式的源代码写的很清楚了,大体逻辑是这个样子的:
    if(ip地址不同 or (当前访问时间 - 同一IP上次访问时间) > 300){
    #其中300为5分钟,具体楼主可以根据需要修改
       $counter += 1;
       update 该IP最后访问时间
    }//以上即为记数过程
    echo $counter;//显示访问人数
      

  3.   

    <?php
    session_start();
    if(! isset($_SERVER['counter'])) {
      $_SERVER['counter'] = 1;  //这里放任意计数器代码,比如Amely(海贝) 的}
    ?>
      

  4.   

    大家都写完了,补充一点.防刷新不一定要记录IP,主要是记录一下后,给加一个标志.如果有那个标志的,就不记录了,
    可以使用的有cookie or session都可以.但使用cookie session有一个缺点,就是如果,别人把页面关了,重新打开,就又可以算了.
    (当然,你可以设定cookie时间,如半个小时的样子)最好的方法当然是记录IP了,但楼主要求的是文本的.可能做不到.
      

  5.   

    但是有一点要补充的是, Amely(海贝) 的代码是刷新的php文本计数器.
    你一刷新他就自动增加.可以考虑唠叨大哥的写法..
      

  6.   

    真是人才济济呀。小弟佩服。学习ing。
      

  7.   

    用一个session判断,就可以有效防止“刷新”。
      

  8.   

    我要给各位再加100分,请告诉我怎么加分啊。谢谢诸位的回复。请问最后的代码是否正确。
    <?php  
    session_start();  
    if(!  isset($_SERVER['counter']))  {  
       $_SERVER['counter']  =  1;  
     
      $file  =    "counter.txt  ";  
    //File  name  
     
    //That's  it  
     
    if(  !  file_exists(    "$file  "  )  )  {  
    touch(    "$file  "  );  
    chmod(  "$file  ",0777);  
    $fp=fopen(  "$file  ",  "r+  ");  
    fseek($fp,0);  
    $compteur  =  0;  
    fputs($fp,$compteur);  
    }  
     
    if(  is_readable(    "$file  ")  and  is_writable(    "$file  ")  )  {  
     
    $fp=fopen(  "$file  ",  "r+  ");  
    $compteur=fgets($fp,9999);  
    fseek($fp,0);  
    $compteur++;  
    fputs($fp,$compteur);  
    fclose($fp);  
    print  $compteur;  
    }  else  {  
    chmod(  "$file  ",0777);  
    }  
     
     
    }  
    ?  >
      

  9.   

    我要给各位再加100分,请告诉我怎么加分啊。谢谢诸位的回复。请问最后的代码是否正确。
    <?php  
    session_start();  
    if(!  isset($_SERVER['counter']))  {  
       $_SERVER['counter']  =  1;  
     
      $file  =    "counter.txt  ";  
    //File  name  
     
    //That's  it  
     
    if(  !  file_exists(    "$file  "  )  )  {  
    touch(    "$file  "  );  
    chmod(  "$file  ",0777);  
    $fp=fopen(  "$file  ",  "r+  ");  
    fseek($fp,0);  
    $compteur  =  0;  
    fputs($fp,$compteur);  
    }  
     
    if(  is_readable(    "$file  ")  and  is_writable(    "$file  ")  )  {  
     
    $fp=fopen(  "$file  ",  "r+  ");  
    $compteur=fgets($fp,9999);  
    fseek($fp,0);  
    $compteur++;  
    fputs($fp,$compteur);  
    fclose($fp);  
    print  $compteur;  
    }  else  {  
    chmod(  "$file  ",0777);  
    }  
     
     
    }  
    ?  >
      

  10.   

    记录IP并不一定准确
    有人用网关上网的,一群人用同一个公网IP设置Session过期时间更准确一些
    Cookie并不是关掉浏览器就失效,Cookie设置时间很方便
      

  11.   

    各位程序出现如下错误:&iexcl;&iexcl; Warning: session_start(): Cannot send session cookie - headers already sent by (output started at /www/users/glanet-sha.com/countwrite.php:12) in /www/users/glanet-sha.com/countwrite.php on line 13Warning: session_start(): Cannot send session cache limiter - headers already sent (output started at /www/users/glanet-sha.com/countwrite.php:12) in /www/users/glanet-sha.com/countwrite.php on line 13
     
      

  12.   

    我不太懂php ,希望能够获得完整代码。谢谢各位帮助。
      

  13.   

    xuzuning(唠叨) Amely(海贝)我把你们的代码接合了不过好象刷新还是加数据啊,能帮我看看吗?
      

  14.   

    session_start()需要放在php文件的最前面,哪怕是他前面有一个空格都有可能报错的。
      

  15.   

    是放到最前面了,也没有报错,只是刷新仍然加数据。<?php  
    session_start();  
    if(!  isset($_SERVER['counter']))  {  
       $_SERVER['counter']  =  1;  
     
      $file  =    "counter.txt  ";  
    //File  name  
     
    //That's  it  
     
    if(  !  file_exists(    "$file  "  )  )  {  
    touch(    "$file  "  );  
    chmod(  "$file  ",0777);  
    $fp=fopen(  "$file  ",  "r+  ");  
    fseek($fp,0);  
    $compteur  =  0;  
    fputs($fp,$compteur);  
    }  
     
    if(  is_readable(    "$file  ")  and  is_writable(    "$file  ")  )  {  
     
    $fp=fopen(  "$file  ",  "r+  ");  
    $compteur=fgets($fp,9999);  
    fseek($fp,0);  
    $compteur++;  
    fputs($fp,$compteur);  
    fclose($fp);  
    print  $compteur;  
    }  else  {  
    chmod(  "$file  ",0777);  
    }  
     
     
    }  
    ?  >
      

  16.   

    把$_SERVER['counter'] 改成$_SESSION['counter']
      

  17.   

    记得asp有个application对象,哪个php变量有这种效果呢?