How To Cloak Affiliate Links & Click Tracking With PHP

Here’s a sweet bit of code I found to not only cloak affiliate links with php, but also count the clicks:

Hiding Links With PHP and Counting Clicks with MySQL

The nice thing about this bit of code is that you can add an unlimited amount of links to the one file. I’ve set this up (5 minute job) and it works like a dream. If you know how to create a database, you’ll have this up and running lickety split.

I do a little bit of everything when it comes to link cloaking. I’ve used script programs (can be a bit clunky), .htaccess redirects, domain redirects, javascript methods (doesn’t work for non-javascript browsers), other bits & pieces of code. I’m really liking this code though so I’m going to give it a shot.


  • You don’t have to buy a link cloaker/click tracker for *each* domain. This is a free solution, simply install the script for each site if you like.
  • Redirects & tracks more than one link at a time.
  • You could also install this on one single domain and use it as your own personal tinyurl for all your sites. Potential problem with that idea: it will be easy to trace all the domains in your network.

To view your click counts, you just have to go into phpmyadmin, select the database, select the linkcount table and then browse–each link name and count is displayed.

If you want to prevent the search engines from spidering those links (does Google view affiliate links as paid links?), just block them in robots.txt with:

User-agent: *
Disallow: /redirect-filename-you-used.php

That is all it takes. From here you may want to write a front-end to display your click results or you could even display the count next to the specific link in your sites navigation menu to rank the most popular outgoing links.

Maybe if we harrass we can get him to whip up that front end too for us non-codeheads so we don’t have to dig around in phpmyadmin :P. If you use his code, why not drop him a comment or a link as thanks, it’s a keeper.

ETA: Patrick pulled through with some more code–wooohooo! You’ll find the code to view the stats here: Displaying Click Counts with PHP and MySQL. Works GREAT, a big thanks to!

Update: For WordPress users, there’s this handy plugin that will auto-cloak keywords that you set inside the WordPress admin panel (watch video to see how it works).


I've been trying to find my way online for more years than I care to admit.

7 thoughts to “How To Cloak Affiliate Links & Click Tracking With PHP”

  1. Very, very nice Patrick, THANK YOU! I’ve updated the above post to point to the count display code, works great :).

  2. I have been doing something similar. I actually have a javascript redirector that I use and have the url as a vairable. So it is not entirely cloaked, but then I also use a php redirect to get to the javascript in some cases.

    Regardless, this has me considering some changes to how I am redirecting some of my urls.

  3. Cool Terry thanks, I’ll have to check this out.

    I’ve used all types of cloaking/redirecting too: Gotrythis, jump.php, Ninja Link Cloaker…etc

    Gotrythis of course is paid, but its kind of a pain across multiple domains. And its a little time consuming to set up links.

    The best of both worlds would be a cloaker/redirect script that would not only track clicks, but allow for dynamic tracking ids to “see” what search terms the user used to get to your site and also allow you to direct link while cloaking. xtreme conversions is a good tracking solution, but it doesnt work well with cloaks. Maybe by adding your solution it would work…?

  4. I gotta thank you for this nifty piece of code. I didn’t know what I was missing, the cloaking works great. I haven’t gotten into the tracking part of it yet, that piece went straight over my head. I have no clue about mysql and the like maybe at a later date when I pick up more knowledge but the cloaking went without a hitch.

  5. Wow, this will really help with my affiliate links. Thanks a lot. I have searched the internet for this sort of information for so long. Thank you.

  6. Thanks for the great script. I’m going to do a cashback shopping site. I am also wondering how to track whether a member (registered user) actually makes a purchase after clicking my affiliate link. Because I plan to give some cash back to members when they make a purchase through my link.

    I am wondering how to track who makes a purchase at what time? You don’t need to give the script, I only need to know ‘how’.

    Thank you so much!

Comments are closed.