Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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>
share|improve this answer
add comment

Yes, it is definitely possible to count the number of hits by just using a few lines of HTML Code. But then you might also need a bit of PHP code in the root directory of the site.

But if you don't want to go with all these hassles then just search for free website hit counter in any search engine and you will be served with a ready made code of html which you can place at the bottom of index.html page and the rest is taken care by itself. One such quick search bought me to this website: http://www.simplehitcounter.com/

And if you want to get into the details of exactly what your visitors are doing and their geo-location and other details then you might have to go for higher options like Google webmaster tools or Piwik.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.