Now you are going to use one PHP page to manage multiple link cloak/redirections. Again feel free to copy and paste the to make things easier.
The single page PHP redirect method removes all three of the drawbacks found in the original HTML version but adds a challenge of it’s own:
1. You have to add a parameter to the posted URL
For those of you not familiar with what that means here’s an example:
http://www.realcooldomain.com/hotproduct?g=31
The question mark signals the beginning of a parameter string (you can add multiple parameters but that’s for later). In the example above g is the parameter’s variable name and 31 is the value.
We’ll see how that works out in a minute.
One advantage of this method is code can be added to log visits directly to a database, like MySQL, which makes tracking and analysis easier.
Another advantage is a centralized page for managing a set of related cloaked links (or all of them if you want).
It will be up to you to decide if these advantages outweigh the need for ?g=31
IMPORTANT REMINDER:
It is very important that the PHP code containing the header command show up in your document before any HTML. Get into the habit of putting all redirects before even the “DOCTYPE” HTML line.
Make sure there are no blank lines at the top of the page. This is probably the number one mistake that causes the redirect to fail.
Redirecting multiple links on one page is not as straight forward as one line php redirects. Instead of a direct step by step I will show you how the sample code (at the bottom for you to copy and paste) is built. This will help you understand how you can add a new link direct with one simple copy and paste operation.
The first line:
switch ($g) {
tells PHP to look at the value of the variable g. So if your link is:
http://www.realcooldomain.com/recommends.php?g=hotproduct
PHP will use the value hotproduct in the lines that follow.
The next three lines:
case “hotproduct”:
header(’Location: http://www.iphone.com’);
exit;
ask 1. if the variable g is hotproduct 2. then redirect the browser to http://www.iphone.com and 3. stop running php.
If the variable g is not hotproduct then the next line starting with case, if it exists, will be evaluated.
The exit; is very important. Without it the lines that follow will be run and only the default redirection will occur (see below)
The three lines shown above are technically enough to make this work. If you leave off the default section and the variable is not set to hotproduct then the HTML at the bottom will be shown. You can use any standard HTML page below the PHP code.
To create additional redirects just copy the three lines of code, paste above default: and below the last exit;, and change:
http://www.iphone.com
to the URL you want people to end up at.
For this example I decided I wanted:
http://www.realcooldomain.com/recommends.php?g=apple
to go directly to the Apple home page. The result is:
case “hotproduct”:
header(’Location: http://www.iphone.com’);
exit;
case “apple”:
header(’Location: http://www.apple.com’);
exit;
The final three lines of PHP in the sample code below are optional. If no variable is matched you can leave them at this page and they will see any HTML you enter below the PHP.
If you want everyone to go to a specific site, like an offer that converts well on general traffic, keep this:
default:
header(’Location: http://twitter.com/mfartr’);
exit;
and change:
http://twitter.com/mfartr
to the page you want everyone sent to.
Next up, using .htaccess for all your link cloaking and tracking needs.
Here’s the code to copy.
<?php
switch ($g) {
case “hotproduct”:
header(’Location: http://www.iphone.com’);
exit;
case “apple”:
header(’Location: http://www.apple.com’);
exit;
default:
header(’Location: http://twitter.com/mfartr’);
exit;
}
?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″ />
<title>Untitled Document</title>
</head>
<body>
whatever you want here
</body>
</html>
July 14th, 2008
Technorati:
affiliate link link cloaking php redirect redirect