Shortstat Everywhere

Ever wanted to include lots of files in your stats but the CMS doesn’t play nice with PHP in your templates? Do you cry with agony when you think of the prospect of adding the same line over and over and over again to your static site?

Well, I didn’t do the latter, but the former did bug me, so I found a way around it: Including the shortstat line by utilising Apache’s .htaccess possibilities.

Here’s an easy step by step guide:

  • Check your server is running Apache. If not, bad luck.
  • Install shortstat
  • Create the file “append-me.php” and place the following code in it:
    <?php $findus   =  array('shortstat'); $excludes = array(''); $path = $_SERVER['REQUEST_URI']; $ip = $_SERVER['REMOTE_ADDR']; foreach ($excludes as $exclude) {    if (strstr($ip,$exclude)) {         $found = "yes";     } } foreach ($findus as $find) {    if (strstr($path,$find)) {         $found = "yes";     } } if ($found !== "yes") { @include_once( "/path/to/shortstat/inc.stats.php");     }     else {         echo "<!-- I am not to be counted! -->";     }  ?>
  • Create a file called .htaccess and place the following code in it:
     php_value auto_prepend_file "/path/to/append-me.php"
  • Upload both files to the root of your directory.
  • Hit some pages and see if Shortstat sees them, if not, ask
    your server administrator to turn on .htaccess compatibility.
  • Done.

If you’re wondering what the code actually does, here’s a quick run-down:

Firstly, the .htaccess is asked what to do when someone hits a page on your site. It then tells Apache to prepend the file “append-me.php”. Append-me then checks if the url has Shortstat in it’s name, and if it does it doesn’t include the counter script. This is to stop Shortstat from counting itself every time you check the stats page. If Shortstat isn’t found in the url, the counter script is added to the page.

Simple but effective.