【问题标题】:Visitors Counter for simple web sites like VinaoraVinaora 等简单网站的访客计数器
【发布时间】:2012-06-23 16:18:30
【问题描述】:

有没有人知道一个网站的访客计数器,它是在不使用 CMS(Joomla、Drupal)但使用简单的 HTML、CSS、JS、PHP 的情况下构建的?最好有一个类似 Vinaora 访客柜台的 joomla 柜台,可在here

谢谢。

【问题讨论】:

  • 对于基本计数器,您可以使用 php 进行文件操作

标签: html hitcounter


【解决方案1】:

不知道任何不依赖数据库来存储信息并将其精美地呈现给访问者的网站。

依赖于文本文件的解决方案可以这样执行:

<?php

  // file name and file path
  $fileName = 'counter.txt';
  $filePath = dirname(__FILE__).'/'.$fileName;


  /*
   * If the file exists
   */
  if (is_file($filePath)) {

    $fp = fopen($filePath, "c+");            // open the file for read/write
    $data = fread($fp, filesize($filePath)); // ready entire file to variable
    $arr = explode("\t", $data);             // create array

    // run by each array entry to manipulate the data
    $c=0;
    foreach($arr as $visit) {
      $c++;
    }

    // output the result
    echo '<div id="visits">'.$c.'</div>';

    // write the new entry to the file
    fwrite($fp,time()."\t");

    // close the file
    fclose($fp);

  /*
   * File does not exist
   */
  } else {

    $fp = fopen($filePath, "w"); // open the file to write
    fwrite($fp, time()."\t");    // write the file

    // output the data
    echo '<div id="visits">1</div>';

    // close the file
    fclose($fp);

  }

?>

此解决方案使用 PHP fwrite()fread()fopen()fclose() 存储每次访问的 PHP time(),并用 \t 分隔。 使用存储的时间,您可以执行一些计算并在访问板上显示您需要的详细信息。

上面的例子说明了所有这些,但只显示了总访问量。

【讨论】:

  • 我也在尝试使用 HTML、CSS、JS、PHP 等实现命中计数器。我已经成功地在我的本地 Web 服务器上实现了您的解决方案,通过 PHP5 运行 Apache2 ?php 包括... ?> 它在我的 index.html 的页脚标记中。但是,由于某种原因,当我将它上传到我的网站时它不起作用。我什至尝试用 $_SERVER['DOCUMENT_ROOT'] 替换 __FILE__,但还是不行。我已确保所有路径都是正确的。任何建议,将不胜感激。请注意,我可以在我的网站上执行其他 PHP 脚本。
猜你喜欢
  • 2019-12-05
  • 1970-01-01
  • 2017-09-23
  • 1970-01-01
  • 2012-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多