大网数据广告活动
网站运营与故障问答

Is it possible to count the number of hits or loads of a webpage without anything fancy?

· 52CE 我爱测

I know very little, but I do know that I have some server space given by my University with a file called public_html which anyone can load html and other files from. So is there a simple way to add some file and edit it with vim…

问题原文

原帖链接 · Leonardo · CC BY-SA 3.0

I know very little, but I do know that I have some server space given by my University with a file called public_html which anyone can load html and other files from. So is there a simple way to add some file and edit it with vim or give it a special name and have it collect the number of times a web browser loads a particular html file in public_html? I figure since there are a lot of settings files such as .vimrc or .forward which simply require a name and maybe input. I am hoping there is something like this which will record each time html is accessed in my public_html folder.

Is there such a thing, or am I too hopeful? It seems like a practical feature but like I said I really do not know much about this stuff. Thanks.

I actually do not know if this server space is apache or not, but my engineer account definitely says apache 2.2.15 (Red Hat) at the bottom of any 404 error, so I am really just guessing.

已采纳解答

原帖链接 · Marco Demaio · CC BY-SA 4.0

Supposing your server space supports PHP, you can:

1) Place into the public_html folder a file called idious-counter.php with this code into it:

<?php

define("COUNTER_START_VALUE", 0);
define("COUNTER_LOG", "idious-counter.log"); //name of file you want to use to save the counter value

/*************************************************************************************************/
function IncrementCounter() 
{   
   $create_file = !file_exists(COUNTER_LOG);

   if( !($fh = fopen(COUNTER_LOG, $create_file ? "x+b" : "r+b")) )
      return "Error";       
   //do an flock here, maybe, I don't know :-)

   //Reading current value of counter:
   if($create_file)
      $count = COUNTER_START_VALUE;
   else
   {
      $count = (int)fread($fh, 9); //reads 9 digits (supposing max 1 billion count)   
      rewind($fh);
   }

   //Writing new counter value:
   if(!fwrite($fh, ++$count))
      return "Error";
   if(!fclose($fh))
      return "Error";       

   return str_pad($count, 9, '0', STR_PAD_LEFT);
}

?>

2) In your index.html file add somewhere this line:

<h3>You are visitor number: <?php include("idious-counter.php"); echo IncrementCounter(); ?></h3>

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2013-02-15)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。