php实现网站访客数量统计的方法(简单实现,不能防刷新)

2022-12-15 11:18:35 浏览数 (1)

代码语言:javascript复制
方法一:
<?php
  function Counter()//定义函数
  {                                   
    $five = "00000";//声明变量,$five,$four等变量表示零的个数,放在数字前边构成6位数
    $four = "0000";
    $three = "000";
    $two = "00";
    $one = "0";
    $counter = "rec.txt";//存放访问数的目的文件,.dat格式
    if(!file_exists($counter))//判断文件是否存在
    {
      $counter = fopen($counter,"w");
      $num = 1;
      fputs($counter,"1");//写入文件
      fclose($counter);
      print "$five"."$num";
    }else{
      $fp = fopen($counter,"r ");
      $num = fgets($fp,"1024");//如果文件存在则读出文件,并加 1
      $num = $num   1;
      if($num < 10)
        print "$five"."$num";
      elseif($num < 100)
        print "$four"."$num";
      elseif($num < 1000)
        print "$three"."$num";
      elseif($num < 10000)
        print "$two"."$num";
      elseif($num < 100000)
        print "$one"."$num";
      else
        print "$num";
    }
    $fp = fopen("$counter","w");
    fputs($fp,"$num");
    fclose($fp);
  }
?>
<html>
  <head>
    <title>网站访客统计</title>
  </head>
  <body>
    <center>
      <h1>网站访客统计</h1>
      <font size="6" face="隶书" >欢迎访问CMS集中营!您是第</font>
      <B>
        <font size="9" face="Verdana" color="#FF0000"><?php Counter() ?></font> 
      </B> 
      <font size="6" face="隶书" >位访客!</font>
    </center>
  </body>
</html>

方法二:
<html>
  <head>
    <title>网站访客统计</title>
  </head>
  <body>
    <center>
      <h1>网站访客统计</h1>
      <font size="6" face="隶书" >欢迎访问CMS集中营!您是第</font> 
      <B>
      <?php
        $counter=1;
        if(file_exists("rec.txt"))
        {
          $fp=fopen("rec.txt","r");
          $counter=fgets($fp,1024);
          $counter  ;
          fclose($fp);
        }
        $fp=fopen("rec.txt","w");
        fputs($fp,$counter);
        fclose($fp);
        echo "<font size='9' face='Verdana' color='#FF0000'>".$counter."</font>";
      ?>
      </B> 
      <font size="6" face="隶书" >位访客!</font>
    </center>
  </body>
</html>

本文采用 「CC BY-NC-SA 4.0」创作共享协议,转载请标注以下信息:

原文出处:Yiiven https://cloud.tencent.com/developer/article/2193062

0 人点赞