HomeMySQL Page 6 - Building a Simple Affiliate System in PHP/MySQL
Adding Affiliate Code to Your Site - MySQL
Setting up an affiliate system on your website can be very lucrative. While there are a lot of details involved, it need not be overly complicated. This article walks you through the steps of creating and setting up a basic affiliate system.
There are two types of codes we have to set up for tracking affiliates.
The first is called "click.php." This file is called when visitors click a link on an affiliate's website.
<? include("common.php");
if( $_REQUEST['refid'] ){ $mykey = makeKey(10); SetCookie ("affid",$mykey); $_SESSION['affid'] = $mykey; mysql_query("UPDATE affiliates SET clicks=clicks+1 WHERE akey='".$_REQUEST['refid']."'",DBH); mysql_query("INSERT INTO affstats SET akey='".$_REQUEST['refid']."',type='1', OrderID='".$mykey."'",DBH); } header("Location: $redirecturl"); ?>
Now, create a file called "sale.php":
<? include("common.php");
$affid = $_SESSION['affid']; if (!$affid){ $affid = $HTTP_COOKIE_VARS['affid']; } if (!$affid){ exit; }else{ mysql_query("UPDATE affstats SET type='2',price='{$affpay}',paid='0' where OrderID = '{$affid}'",DBH); } ?>
The file sale.php is called by adding it to your site like this:
<img src="/affiliates/sale.php">
Managing Affiliates
Managing affiliates is important. We need to have a way to manage affiliates so we can set them as being paid, and perform other tasks. For the Affiliate management part, we will create five files.
The first file is "accesscontrol.php." This file is similiar to login.php but works for the management area.
<? if($_GET['setaspaid']){ mysql_query("UPDATE affstats SET paid='1' WHERE ID='{$_GET['setaspaid']}'",DBH); } if($_GET['setasunpaid']){ mysql_query("UPDATE affstats SET paid='0' WHERE ID='{$_GET['setasunpaid']}'"); } $sql = "SELECT * FROM affiliates WHERE ID='{$_GET['userId']}'"; $lo = mysql_query( $sql ); $affi = mysql_fetch_assoc($lo); ?> <? if($affi['ptype']){ ?> Requested Payment Method: <?=( strtolower($affi ['ptype']) == "paypal" ? "PayPal" : "Check")? ><br> <? } ?> Payment Account: <?=( strtolower($affi ['ptype']) == "paypal" ? $affi['pemail'] : nl2br($affi['address']) )?><br> <br> <table align=center width=350 border=1> <? $q1 = "select * from affstats WHERE akey= '{$affi['akey']}' AND type=2 GROUP BY OrderID"; $r1 = mysql_query($q1,DBH) or die(mysql_error()); while($a1 = mysql_fetch_assoc($r1)){ ?> <tr> <td>Amount Earned:</td> <td>$<?=$a1['price']?></td> <? if($a1[paid] == 1){ ?> <td><a href="manage.php? mode=payout&userId=<?=$_GET['userId']? >&setasunpaid=<?=$a1['ID']?>">Set As Not Paid</a></td> <? }else{ ?> <td><a href="manage.php? mode=payout&userId=<?=$_GET['userId']? >&setaspaid=<?=$a1['ID']?>">Set As Paid</a></td> <? } ?> </tr> <? } ?> </table>
Paying Your Affiliates
I've given you an interface to use to see how much affiliates have made and set those earnings to paid, but you still have to pay them using the method they specify. If affiliates ask to be paid via check, then send them a check.
One good practice is to set a minimum payment level, so that affiliates only get paid when they reach $20.00 or something like that.
In Conclusion
So, you now have a basic affiliate system. This can be made more complicated by modifying the code to allow banners, add more payout options, and so on. But I was aiming to give you something more simple to work with as a start.
Affiliate systems can be very complex sometimes and this article was intended to give you a system that wasn't complex at all.