Back on September 4th, I wrote about adding ‘live referrer tracking’ to this website using a bit of Perl and PHP. I pointed at a few sources, but didn’t provide any real details on how to do it, so I’m going to try and explain what I did, and how to do it yourself in this article… so that hopefully it takes you less time than it took me.

This all assumes that you know a little about PHP, and nothing about Perl (which is my case, though I’d say, I know a little more than ‘a little’ about PHP, but I’m by no means an expert). It also assumes that you aren’t afraid to break your MT installation, and won’t hold me liable for anything you do to your webserver, or self throughout this process.

1. Download Waxy Backlink v0.5. (Right Click: Save File to Disk… on your computer somewhere, the desktop is a good place).

2. Read these directions and these directions at waxy.org to familiarize yourself with the script’s stock instructions, but don’t get bent around the axle on them.

3. Login to your website using telnet, and navigate using cd to the directory where you want to story your ‘referrer log files’ stored. Type:

mkdir backlink

That will make a directory where you’ll keep your referrer log files that this script will produce.

4. We need to make that directory ‘world writeable’ so that the processes that control Perl on your webserver can write to that directory. Type:

chmod 777 backlink

(This might be considered a security risk by some folks, but to me, it’s not all that big of a risk… as the only think you’ll be keeping in that directory are log files or referrers, which you’ll be displaying publicly anyways, so big deal right?)

5. Open backlink.txt, and then modify the backlink.txt file line that says

my $backlinkdir = '/usr/local/apache/htdocs/backlink';

So that it points to your backlink log file diretory on your server (you should specify the full path to the server directoy).

6. If you’re going to use PHP to include this file then change the line that says

my $php = 0;

to say

my $php = 1;

7. You might want to adjust the number of referrers that are displayed now, and the number of incoming hits it takes prior to displaying the referrer now, and if so, change these lines:

# The total number of links you want to display for a single entry

my $max_links = 10;

# The minimum number of hits before a link is displayed

my $min_hits = 0;

So that the numers are appropriate (I’d wait to do this if I were you, till you know its working).

8. You’re almost done with this file for now. Save, and quite your text editor (I really hope you’re using BBEdit for this).

9. Now that the file is saved, change the name to backlink.cgi and then upload it to your website (it doesn’t matter where you put it really, but the same directory as your index file for your weblog is probaly the best place).

Now we just need to include the file, and check to see if its working.

10. Go back to waxy.org’s instructions on how to include via PHP here. See Brad Choate’s comment about ‘double quotes’. Copy this:

<?php echo `perl /path/to/cgi/backlink.cgi "$HTTP_HOST" "$SCRIPT_NAME" "$HTTP_REFERER"`; ?>

And paste it into a spare text document on your computer. Now, in Movable Type’s template interface, you need to select your ‘Main Template’ and paste that line in the sidebar somewhere, just so you can see if it works. If you’re still using the stock template for MT (v2.21), then you can copy this and paste my code:

<div class="sidetitle">

<a name="referrers"></a>Referrers

</div>

<div class="side">

<?php echo `perl /path/to/cgi/backlink.cgi "$HTTP_HOST" "$SCRIPT_NAME" "$HTTP_REFERER"`; ?>

</div>

But you’ll need to modify the ‘/path/to/cgi/backlink.cgi’ part so that it’s the full path on your webserver to your backlink.cgi file.

11. Save your Main Template, then, check to see that you’re template’s ‘output file’ field has a filename in it ending with ‘.php’ like ‘index.php’ if you’re working on your Main Template. Enter an output file name of ‘index.php’ if there isn’t one, and save the template again.

12. Now rebuild your site using MT’s rebuild function (you really only need to rebuild the index page).

13. Browse to your new index page (if you dindn’t have an index.php file in your Main Template before all of this, you’ll probably have to type it in manually, as you likely still ahve an index.html file that’s getting the traffic to your domain/directory).

You’re looking to see that where you’re including the call to the backlink.cgi file, there aren’t any visible errors right now.

14. Create a link to your index.php file on a domain other than your own (might need a buddy to help you out there). Click on that dummy link, then reload your index.php file to see if the referrer shows up. It should work fine, if you’ve done everything properly, like checked your template output filename, chmod’d the directory of your referrer log files, and not screwed up the .cgi file that you created.

If it’s working, then you should delete your index.html file now. You don’t need it (and don’t want it) if it’s winning in the Apache configs over index.php (apache controls which is served to the visitor first).

15. If its not working, check your work… you probably screwed something up by following the directions I wrote above (at an ungodly hour mind you), or your webserver doesn’t support PHP or Perl. I’m definitely not the guy to help you there, though, I doubt its a Perl issue if you’ve got MT working ;)

Lastly. If this didn’t work, get me on iChat/AIM: johnenglerworkin — I’ll answer you when I can.

You can add the include file and change the template output file name for all of your templates if you like. I’ve only added it to my individual files and the main index so far.

Some more notes:

You’ll eventually want to add things to your blacklist of sites that you don’t want displayed in your referrer list. This is what mine looks like right now:

my @blacklist = ( $localhost, 'lycos.com', 'alltheweb.com', 'srd.yahoo.com', 'mail.yahoo.com', 'search.', '216.32.180.', 'sitemeter.com', 'ureach.com', 'netscape.com', 'bravenet.', 'mail.', 'radiocomments.userland.com/comments', '127.0.', 'www.weblogs.com/top100', 'radio.xmlstoragesystem.com' );

but that’s only based on a week of running the script, and there are things I’d like to add to it in the future, and maybe delete…

Note: All of the credit for this goes to Andy and Brad Choate and all the others that have written before me. I just read what they did, applied a little grey matter and elbow grease, and got it working for me… I don’t claim to be able to get it working for anyone else.


6 Responses to “Your own Referrer Tracking with MT 2.21”  

  1. Gravatar Icon 1 Ask Bjoern Hansen

    (This might be considered a security risk by some folks, but to me, it’s not all that big of a risk… as the only think you’ll be keeping in that directory are log files or referrers, which you’ll be displaying publicly anyways, so big deal right?)

    Actually, you are setting yourself up to have anyone with access to your server overwrite any of your files.

    But if you are in an insecure shared environment then it’s probably fucked already and won’t make a difference.

    - ask

  2. Gravatar Icon 2 john

    ask, yes, you’re right on both counts.. but like I said… it’s really just referrer logs… I suppose someone could try sneaking some malicious code into that directory if they wanted… but then wouldn’t that hose the whole server? not my problem… hosted solution, and I back up every week… I can deal with that ;)

  1. 1 TooMuchSexy.blog
  2. 2 TooMuchSexy.blog
  3. 3 TooMuchSexy.blog
  4. 4 TooMuchSexy.blog


Leave a Reply





Bad Behavior has blocked 10020 access attempts in the last 7 days.