PHP: Affiliates managementLocation: Tutorials > PHP > Here This tutorial will guide you through a basic set of scripts that track affiliate statistics by logging IPs, access times and referer data to text files. I'm assuming you want to get straight into it so here we go. If your site is already in PHP (eg. you have a index.php) then the first part is simple. If you don't have your default page as index.php then you have two options. Change it to index.php or if that is not an option then put the code below in another file eg. affiliates.php and then edit the rest of the script to use affiliates.php instead. Don't worry if you do not know how to do this, at the end there are example scripts that can be used. Here is the code that you put at the top of your index.php or in affiliates.php
<?php
// the two lines below tell PHP to access the appropiate files if there is a remote referer or if an affiliate link has been clicked from your site (explained below), note that www has to be stripped to allow for URLs with and without www. if(isset($_GET['affiliate'])) include 'out.php'; if(isset($_SERVER['HTTP_REFERER']) AND !ereg(str_replace('www.','',$_SERVER['HTTP_HOST']),$_SERVER['HTTP_REFERER'])) include 'in.php'; ?> Explanation: The function include means to interpret the code in the file included. So include 'inc/affiliate.php'; gets the code of inc.affiliate.php and it "pastes" it into the current file. A remote referer exists when a link is clicked from another site (eg. the site you are doing a link exchange with). The data will be logged in text files so make a directory called stats and create two blank files: ins.txt and outs.txt Now for the main code. Make a file called out.php and put this code in it:
<?php
$urls = array( // this is the list of affiliates, remember to use the form: // 'affiliatename' => 'http://affiliatewebsite.com/', 'affiliate1name' => 'http://affiliate1website.com/', 'affiliate2name' => 'http://affiliate2website.com/', 'affiliate3name' => 'http://affiliate3website.com/', 'affiliate4name' => 'http://affiliate4website.com/', ); // this accesses the stats file and logs the URL accessed, the IP of the user who clicked the link and the date and time $url = $urls[strtolower($_GET['affiliate'])]; $filename = 'stats/outs.txt'; $file = fopen($filename,'a+'); if(!is_writable($filename)) exit; $log = "[{$url}] [{$_SERVER['REMOTE_ADDR']}] [".date("j-m-Y H:i:s")."] \n"; fwrite($file,$log); fclose($file); if($url){ header("Location: $url"); exit; } ?> And then the code to handle links leading to your site. Make a file called in.php and put this code in it: <?php
// open a session and check if it is set or if 60 seconds have elapsed, the reason for this is explained after the code session_start(); if(!isset($_SESSION['lastrefcheck']) OR $_SESSION['lastrefcheck'] > time()-60){ // access the stats file and log the referer (the site where the user came from), the user's IP, the page they accessed and the date $filename = 'stats/ins.txt'; $file = fopen($filename,'a+'); if(!is_writable($filename)) exit; $log = "[{$_SERVER['HTTP_REFERER']}] [{$_SERVER['REMOTE_ADDR']}] [{$_SERVER['PHP_SELF']}] [".date("j-m-Y H:i:s")."] \n"; fwrite($file,$log); fclose($file); // set the time in session $_SESSION['lastrefcheck'] = time(); } ?> Most of that should be self explanatory. The reason sessions are used is to prevent duplicate records. If a user goes to your site twice from the same place in the space of 60 seconds, there is no point in logging it as two visits. And that is it, if you have any problems with files not found and errors like that, you may have missed some files that should have been created. Below is a structure of the files that should be in the folder you are putting the script in: index.php or affiliates.php
in.php (handles users coming "in" from other sites) out.php (handles links "out" from your site) stats (folder) - ins.txt (log of users in) - outs.txt (log of users out) A working example can be seen on the NexusFX home page, if you look to the right, you will notice small affiliate banners. Every time you click on one of these, a log entry is created. If you don't want anyone to see your logs, you can use obscure filenames like inshidden01.txt instead. If you do this, change the code above to access the new filename by simply changing 'stats/ins.txt' to 'stats/insxxxxx.txt'. Another option is to forbid access to the folders, this can be done via HTACCESS. Please note that there are no privacy issues concerning this script. Your IP is only used to identify you as a unique user. Another point of consideration is that the links may not be search engine friendly. Here is alternate method of linking that makes it look like a normal link but clicking on it results in it using the out.php file. The code below shows how this is done.
<a href="http://affiliate-url.com/" onclick="window.location='affiliates.php?affiliate=affiliatename';return false;">
Make sure return false; is included as it cancels out the normal action of the link. If you are using index.php, replace affiliates.php with index.php. This is a simple script with room for expansion. The data can be used to monitor your affiliates. If they are getting hits from your site and not getting any back, consider putting their link in a less visible spot or vice versa, asking your affiliate to show it in a more visible spot. It can also be used to sort your affiliates list based on number of hits they have given you as an incentive for them to try and get more clicks to your site. Download ExampleThe zip file contains all the files. It uses an affiliates.php to manage the script but it can be renamed to index.php if you want (or you can copy the contents of affiliates.php to the top of your existing index.php). Download affiliates.zip |