We all want to track incoming and outgoing hits that other web sites give us, so we all use some kind of link like
http://mydomain.com/aff.php?out=12 and that really does nothing to improve your search engine ranking if a web sites links to you like that.
So as modern scripting languages improve, so does imaginations. With the help of normal PHP scripting, and AJAX code, we can make an affiliation script that's user friendly and search engine friendly.
Read more to view the tutorial. Please note, this tutorial is not for newbies.
Backup your entire affiliate database, and script files before you start editing them!
Okay, so you obviously already have an affiliate script up and running that uses a MySQL database. The first thing you need to do is write down the database name, analyse the table where your affiliate information is stored and write down the fields for:
- The field for the unique ID for each row
- The field for the URL of the affiliate
- The IN field which tracks how many hits the affiliate has sent you
- The OUT field which tracks how many hits you've sent them
Now that all those field names have been written down you can open your favorite HTML editor and depending on if you already have javascript code on your web site, you'll ned this code:
var xmlHttp
function goAff(affID)
{
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
return;
}
xmlHttp.open("POST", 'http://yourdomain.com/go.php?aID=' + affID, true);
xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttp.send(null);
}
function GetXmlHttpObject(handler)
{
var objXMLHttp=null
if(window.XMLHttpRequest)
{
objXMLHttp=new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return objXMLHttp;
}
That code can be placed on your affiliate page or in a separate javascript file. Please remember if it's going directly on your affiliate page, you'll need to add the javascript script tags above and below the code!
Next you need to change the following to your own domain name:
xmlHttp.open("POST", 'http://yourdomain.com/go.php?aID=' + affID, true);
Save the file your javascript went in. Now open up another blank page and put the following inside:
<?php
$MySQL_Connection = @mysql_connect('localhost', 'my_username', 'my_password');
$MySQL_Database = @mysql_select_db('my_database', $MySQL_Connection);
$oURL = mysql_query("SELECT * FROM `affiliate_table` WHERE `unique_aff_id` = '" . addslashes($_GET['aID']) . "' LIMIT 1");
if(mysql_num_rows($oURL) != 0)
{
$oURL = mysql_fetch_array($oURL);
mysql_query("UPDATE `affiliate_table` SET `out` = '" . ($oURL['out'] + 1) . "' WHERE `unique_aff_id` = '" . addslashes($_GET['aID']) . "'");
}
?>
You have a few things to change in this, so keep your eyes open. You need to change the following information:
- my_username - The username used to gain access to the database
- my_password - The password used to gain access to the database
- my_database - The name of the database where the affiliate table is located
- affiliate_table - The name of the table where your affiliates are listed.
- unique_aff_id - The field that identifies a row as a unique row (normal named 'id')
- out - The field that tracks hits you send to the affiliate (could be named 'out')
Once you've changed them to the correct information, save the file as
go.php. If you name it something different remember to alter the javascript above.
So at the moment, we've covered the tracking script that records hits out to your affiliates and we've placed the javascript code to make sure they get tracked. Now we need to track the affiliates' hits they send us and alter the affiliate page which displays the images and links.
The below PHP script should go JUST after a MySQL database connection to your database has been made
(if you don't have a mysql connection on any of your pages, use the two lines in the go.php file). This snippet can go on every page to track how many hits your affiliates send to you. For example, they might send you some hits via a direct link to one of your pages, and this would track it. If you don't like that idea, just add it to your main page where it will track direct links to your homepage.
<?php
if(!empty($_SERVER['HTTP_REFERER']))
{
$aBits = parse_url($_SERVER['HTTP_REFERER']);
if(substr($aBits['host'], 0, 4) == "www.")
$nURL = substr($aBits['host'], 4);
else
$nURL = $aBits['host'];
if($nURL != "yourdomain.com")
{
$aQuery = mysql_query("SELECT * FROM `affiliate_table` WHERE `affiliate_url` LIKE '%" . $nURL . "%'");
if(mysql_num_rows($aQuery) != 0)
{
$aQuery = mysql_fetch_array($aQuery);
mysql_query("UPDATE `affiliate_table` SET `in` = " . ($aQuery['in']+1) . " WHERE `affiliate_id` = " . $aQuery['affiliate_id']);
}
}
}
?>
You need to change a few things in this file too. Change the following:
- yourdomain.com - Type your real domain. Do NOT include http:// OR www. OR an a trailing slash (/)
- affiliate_table - The name of the table where your affiliates are listed.
- affiliate_url - The field name which contains the affiliate URL for each row.
- in - The field that tracks hits the affiliate sends to you (could be named 'in')
- affiliate_id - The unique field that identifies an affiliate (normally named 'id')
Remember to place the above code somewhere after a database connection has been made!
Now that's all done, we need to alter your current affiliate script to track the real hits in and out. You'll need to open your affiliate script which outputs the image tags so they are viewable. It may look something like this:
<a href="aff.php?out=' . $aRow['id'] . '" target="_blank"><img src="affiliates/' . $aRow['id']) . $aRow['type'] . '" border="0"></a>
What we need to add is the following code to the image tag:
onClick="javascript: goAff('' . $aRow['id'] . '');"
So, taking the example from above, and placing the code we need on the image tag, it will look something like this:
<a href="aff.php?out=' . $aRow['id'] . '" target="_blank"><img src="affiliates/' . $aRow['id']) . $aRow['type'] . '" border="0" onClick="javascript: goAff('' . $aRow['id'] . '');"></a>
However, that's not all we need to do. Your current affiliate script will use a link to your web site that tracks the hits. We need to remove this link and just place the affiliates URL there instead. By using the field name for the affiliates URL, you can change the above HTML code, so this:
<a href="' . $aRow['aff_URL'] . '" target="_blank"><img src="affiliates/' . $aRow['id']) . $aRow['type'] . '" border="0" onClick="javascript: goAff('' . $aRow['id'] . '');"></a>
You obviously need to change
aff_URL to the correct field name. Once that's all done, save, upload then test.
I will be releasing a downloadable zip file that contains an automatic installer and a few other features like my web site uses. You can either attempt to alter your current script, or wait for mine which could arrive in a week or more.
Posted by Steven Sullivan on 11th August, 2006 - 15:00:36 GMT