Quick and Dirty Link Hider and Counter
I’ve been doing some affiliate marketing recently and was in need of a script that could hide my affiliate link and also give me a count of the times it was clicked so I could calculate stuff like conversion rates and such accurately. I looked around for one a lot but couldn’t find one so I went ahead and made one myself. As I’m currently rather new to PHP this is rather dirty but it works.
I thought I’d share it as well. Basically it makes a text file in the directory that you put it in that just has the number of clicks through this link, then it forwards the user on to the link. I recommend putting it in a directory like www.yourdomain.com/recommends/link.php and linking to that. I make a separate one for each link I want to track. In the future I plan to upgrade this to provide more tracking and such but it’s pretty useful now so hopefully someone can put it to use.
<?php
// customize these things
$linkCountFile = ‘<counter file name>.txt’;
$linkToGo = ‘<your link here>’;
// leave this alone
$clickCount = 0;
if (file_exists($linkCountFile)) {
$fh = fopen($linkCountFile, ‘r’);
$clickCount = fgets($fh);
fclose($fh);
}
$clickCount++;
$fh = fopen($linkCountFile, ‘w’);
fwrite($fh, $clickCount);
//echo “clicks: $clickCount<br>”;
//echo “going to: $linkToGo”;
header( “Location: $linkToGo” ) ;
?>



















