A Free Outbound Link Tracking Script
Introduction
PHPTraK is a simple PHP script that you can use to track how many times a link is accessed. It is designed to track an unlimited number of links, whether they are inbound to your site or outbound from your site. It has a very simple usage, and the backend does not require any database - everything is done using text files.
Further, PHPTraK has some extra security features. Firstly, you can set it to block any tracking that comes from outside your website [but this has a drawback - see later]. Secondly, it can filter out bogus URLs that are passed to it. More on these features below.
Installation and Usage
PHPTraK is easy to install:
- Download PHPTraK.
- Unzip all of the files into a directory.
- Edit phptrak.php to change the default settings. See the Options section below.
- Upload them, all as TEXT to a separate directory on your webhost. For the sake of this example, let us assume that you uploaded them to /phptrak/.
- CHMOD the /phptrak/ directory to 777. CHMOD is the name of the command that sets the file permissions on Unix/Linux hosts. You may not need to do this if your webhost uses Windows servers.
Once you have installed PHPTraK, you test your installation by browsing, using your web-browser, to http://your-website.com/phptrak/test.html. This will bring up a list of URLs that you can use to test the URL filter (if you chose to filter URLs), and/or to test the tracking of PHPTraK (whether it works or not).
To use PHPTraK, suppose you want to track all out-bound links going to http://some-domain.com. The usual way of linking is to have <a href="http://some-domain.com">click here</a>. If you want to track all hits going to that link, you would use instead:
This will add an entry to the new link and it will be tracked from the on. That's it!
Options
The current version of PHPTraK has two settings. Firstly, PHPTraK can filter out 'bogus', or to use a more appropriate word, 'mal-formed' URLs. The filtering is done via a regular expression. PHPTraK comes with a rudimentary URL filter. You can change that as you see fit - for example to forward only to a certain domain. The settings that control this are on lines 25 and 26, and are as follows:
- $CHECK_URL: This is either TRUE or FALSE. If TRUE, then URL checking is active; if it is FALSE, there will be no checking.
- $CHECK_URL_REGEX: This is the regular expression that will be used to check the URLs. You can edit it. The default regular expression is: "/^(http:|ftp:\/\/).*(\.+).*/i". If $CHECK_URL is FALSE, then $CHECK_URL_REGEX is ignored.
The second setting allows PHPTraK to check the HTTP REFERER variable. This checks where the request to forward came from. You can set that so that you can force all requests to come from your site only, and therefore, other sites cannot use your copy of PHPTraK. This has two drawbacks:
- If this setting is active, you cannot track hits coming from other sites. Suppose you want to track all hits to your homepage (for example) from a forum. You can post to the forum the home page URL to be: www.your-website.com/phptrak.php?http://www.your-website/index.html. This will track all hits coming in from the forum, but if the HTTP REFERER checking is active, this will not work.
- In an attempt to increase privacy, some people use programs that clear the HTTP REFERER that is sent by the browser. Therefore to a small population of web users, when they try to use forwarding via PHPTraK with HTTP REFERER checking turned on, they will fail.
To use HTTP REFERER checking, you need to set the following variables correctly:
- $CHECK_REFERER: This is either TRUE or FALSE. If TRUE, then HTTP REFERER checking is active; if it is FALSE, there will be no checking.
- $REFERER_SUBSTRING: This is a simple substring that forms part of your website's URL. PHPTraK simply checks whether this substring exists within the HTTP REFERER variable. For example, for my website, http://www.ekstreme.com, a simple value for $REFERER_SUBSTRING would be 'ekstreme.com'. If $CHECK_REFERER is set to FALSE, then $REFERER_SUBSTRING is ignored.
History
Version 2 is a major restructuring of the script. The way the info is stored and displayed has been re-written. PHPTraK is much more scalable now.
Feedback
I welcome any questions, comments, and any suggestions for future enhancements. You can contact me by filling in the form on this page.
Directory Listings
PHPTraK is listed on several directories. If you know of a directory that PHPTraK is not listed on, then please contact me. Thanks!
