Easily count visitors to a page

Sometimes you will like to know how many people visits a specific page in a range of time. That can easily be done on top of the analytics solution without damaging or changing the analytics code.

This is a quick solution that will allow you to see how many clicks a particular page have.

$filename = 'myfiles/count.txt';

 if (file_exists($filename)) 
  $count = file('myfiles/count.txt'); 
  $count[0] ++;
  $fp = fopen("myfiles/count.txt", "w");
  fputs ($fp, "$count[0]");
  fclose ($fp);
  echo '<!-- Yes '.$count[0].'-->';

  $fh = fopen("myfiles/count.txt", "w");
  die("unable to create file");
  fputs ($fh, 1);
  fclose ($fh);
  $count = file('myfiles/count.txt'); 
  echo '<!-- No '.$count[0].'-->';

You can put this code wherever you want,  even to easily track how many people enters into your admin section or how many people gets to your thank you page.

Remember, this is a temporary solution, just to have a quick understanding of what’s happening on an specific page or template.  For a more complete solution better to do it with a google analytics script, for example.


Leave a Reply