问题原文
原帖链接 · 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>
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
