Easy Link Cloaker With PHP
Posted by Greg Bulmash in Online Marketing And SEO, Techno Thoughts, Web Programming, tags: PHPWhat is "link cloaking," you ask? It's when you use an alternative URL to represent the actual URL you're linking to. For example, if you were linking to...
...you might instead cloak it and link to:
The arguments for link cloaking include:
- Easier link/click tracking
- Making links short enough to fit in an e-mail
- Making links harder for spyware to hijack
- Ease of link management (use the cloaked link in 10 places, then change them all with one database edit).
For me, the only time I use link cloaking is in text e-mails. In those cases it can be very useful. If you have a really long link, a lot of mail clients will break it into two or even three lines, but only link up the first line. When that happens, people get a "page not found" error and either have to piece the link back together to get to your page or just give up.
So even if it's only for that one purpose, it can have value. And when you see how easy it is, you'll be shocked.
A simple link cloaking script consists of two parts: "the array" and "the forwarder".
The Array
If you're not familiar with the programming concept of arrays, they're basically a way to keep a list in a single object. Here's how you build your array.
"http://www.fundraw.com/",
"http://www.bulmash.com/",
"http://www.brainhandles.com/",
"http://www.funbabyclothes.com/",
"http://www.insultfinder.com/"
);
Each of those URLs will have a number and the numbering starts at zero. So www.brainhandles.com would be #2.
The Forwarder
Let's say your cloaking script will be called "out.php" and will be in your top level web directory. Then a link to "fundraw.com" would look like this...
Your cloaker script would then begin with the array and have these lines of code following it:
if(isset($_GET["L"])){
$linkval = intval($_GET["L"]); // use intval() to ensure the value is an integer
} else {
error();
}
if(isset($links[$linkval])){ //this if condition uses isset() to make sure the value is valid
$linkout = $links[$linkval];
} else {
error();
}
//This line sends the visitor to the link
header("Location: $linkout");
//This block handles the errors
function error(){
echo "We're sorry, but that was not a valid link";
exit;
}
And That's It
Put the array and forwarder in a PHP script and you've got an easy, simple, link cloaker. The only real downside is that when you want to add a new link, you have to edit the array by hand and upload the revised script to your server. Now, we could make it easier to add links by hooking this up with a couple of forms and some database code, and it's pretty easy to do too, but that's a subject for another post.


Entries (RSS)
You should add this line before the one that sends the location: header -
header("HTTP/1.1 301 Moved Permanently");
That way, search engines will treat links to the "cloaking" URL as a link to the actual URL. If you don't put the 301 header in there, there's a chance that some SEO juice could be lost.