Candy, A Journal by a James

Shortstat Everywhere

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

Well, I didn’t do the lat­ter, but the former did bug me, so I found a way around it: Including the shortstat line by util­ising Apache’s .htac­cess possibilities.

Here’s an easy step by step guide:

  • Check your server is run­ning Apache. If not, bad luck.
  • Install shortstat
  • Create the file “append-me.php” and place the fol­low­ing code in it:

    <?php
    $findus   =  array('shortstat');
    $excludes = array('xxx.xxx.xxx.xxx');
    
    $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 .htac­cess and place the fol­low­ing 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 admin­is­trator to turn on .htac­cess compatibility.
  • Done.

If you’re won­der­ing what the code actu­ally does, here’s a quick run-down:

Firstly, the .htac­cess is asked what to do when someone hits a page on your site. It then tells Apache to pre­pend 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 count­ing 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.

Comments are closed.