Visitor Affiliate Codes in Links and Forms
A site visitor arrives from an affiliate link. The browser might or might not have cookies enabled.
PHP can be used to insert the affiliate code into links and forms on the web page the visitor arrives at. The code can also be printed as content on the web page itself.
The affiliate code in links ensures the code travels with the visitor when the link is clicked.
The affiliate code in forms makes the code available to:
-
Insert it into URLs of email spawned by the form submission.
-
Store it in a database when the form is submitted. (Which may be used to create affiliate links in later email or other promotion. And which may be consulted when a sale is made to give the affiliate their due.)
-
Embed it in a coupon code presented on the form submission thank-you page.
These instructions assume the visitor arrives with the affiliate code appended to the end of a URL. Examples (where "123xyz" is the affiliate code):
http://example.com/page.php?code=123xyz
http://example.com/page.php?aff=123xyz
http://example.com/page.php?123xyz
You'll find out how to use PHP to insert the affiliate codes brought by visitors into the web page's links and forms — so the affiliate code survives even if the visitor has cookies turned off.
Implementation is two steps:
-
Read the affiliate code.
-
Insert the affiliate code.
Here are the implementation steps:
Read the affiliate code.
The four lines of PHP code that reads the affiliate code needs to be in the web page somewhere before the first point the affiliate code will be inserted.
Putting the four lines of PHP code above or within the HEAD area of the web page will ensure the requirement is met even if the first affiliate code insertion point is moved in the future.
There are two different sets of 4-line PHP code for reading the affiliate code. Which you use depends on how the affiliate code is appended to the URL.
The 1st set:
The first set of 4-line PHP code is used when the affiliate code is appended in name=value format. That's like the first two of the examples further above, reprinted here:
http://example.com/page.php?code=123xyz
http://example.com/page.php?aff=123xyz
The "name" part of the name=value set is the part before the equal sign ("="). In the above examples, it's either "code" or "aff". The "name" part can be whatever your affiliate link uses.
Here's the 4-line PHP code for that:
<?php $DefaultAffilateCode = "098abc"; $AffiliateCode = empty($_GET["NAME"]) ? $DefaultAffilateCode : $_GET["NAME"]; ?>
Replace the affiliate code colored red in the above PHP code with a default affiliate code. If there is no default affiliate code, specify a null string (the two quotation marks with nothing between them).
In 2 places, replace NAME in the above PHP code, colored blue, with the "name" part of the name=value set.
When 098abc is replaced with your default affiliate code (or made blank) and NAME is replaced in both places, the 4-line PHP code is ready to be pasted into your web page.
The 2nd set:
This second set of 4-line PHP code is used when the affiliate code is appended to the URL by itself, not in name=value format. That's like the last of the three examples further above, reprinted here:
http://example.com/page.php?123xyz
Here's the 4-line PHP code for that:
<?php
$DefaultAffilateCode = "098abc";
$AffiliateCode = empty($_SERVER["QUERY_STRING"]) ? $DefaultAffilateCode : urldecode($_SERVER["QUERY_STRING"]);
?>
Replace the affiliate code colored red in the above PHP code with a default affiliate code. If there is no default affiliate code, specify a null string (the two quotation marks with nothing between them).
When 098abc is replaced with your default affiliate code (or made blank), the 4-line PHP code is ready to be pasted into your web page.
Whichever you use, the 1st or 2nd set of 4-line PHP code, it needs to be inserted into the web page before the "Inset the affiliate code" section will work.
Insert the affiliate code.
This part is easy. Really easy.
Wherever you want the affiliate code inserted, paste this code in place:
<?php echo($AffiliateCode) ?>
That's it :)
Below are a few examples.
Affiliate code inserted into a hidden form field:
<input type="hidden" name="affiliate_code" value="<?php echo($AffiliateCode) ?>">
Affiliate code inserted into a link:
<a href="http://example.com/anotherPage.php?aff=<?php echo($AffiliateCode) ?>"> Click for another page! </a>
Affiliate code embedded into a coupon code:
<p> Use coupon code "BA<?php echo($AffiliateCode) ?>ZY" </p>
The affiliate code can travel with the visitor when a link is clicked or a form submitted — whether or not the visitor's browser accepts cookies.
(This article first appeared in Possibilities newsletter.)
Will Bontrager