Tutorials
> PHP > Affiliate System > Page 2
A walkthrough showing how to create your affiliation system.
Note: This tutorial walkthrough is intended for those who have a solid
foundation of PHP basics.
4. Now, we need to log the clicks to count hits. Replace http://yoursite.com with your domain name. Save this file as click.php This can be used to ways, to count hits in, and hits out. For incomming hits, give your affiliates yourdomain.com/click.php?mode=in&id=theirid, for outgoing hits, it is taken care of in the show affiliates code.
Code:
------------------------------------------------------------------------
<?php
// include the connect.php
include "connect.php";
// count clicks
$mode = $_GET['mode'];
// get the mode
// a switch is like a series of ifs and elses, but in less space, and more efficent
switch ($mode) {
case "in":
// for incomming hits, log and redirect to site index
// get id, and protect it
$id = htmlspecialchars($_GET[id]);
// check db
$get = mysql_fetch_assoc(mysql_query("SELECT * FROM `affiliates` WHERE `id` = '$id' LIMIT 1"));
// increment hits
$insert = mysql_query("UPDATE `affiliates` SET `in` = 'in+1' WHERE `id` = '$id'");
// redirect
header("Location: http://yoursite.com");
break;
case "out":
// for outbound hits, log and redirect to affiliates site
// get id and protect it
$id = htmlspecialchars($_GET[id]);
// check db
$get = mysql_fetch_assoc(mysql_query("SELECT * FROM `affiliates` WHERE `id` = '$id' LIMIT 1"));
// increment hits
$insert = mysql_query("UPDATE `affiliates` SET `out` = 'out+1' WHERE `id` = '$id'");
// redirect
header("Location: $get[url]");
break;
}
?>
------------------------------------------------------------------------
5. Finally to show the affiliates, to do this simply put this code where you want your affiliates to be displayed.
Code:
------------------------------------------------------------------------
<?php
// include the connect.php
include "connect.php";
// view affiliates
// to get affiliates that are approved from new to old.
$show = mysql_query("SELECT * FROM `affiliates` WHERE `active` = '1' ORDER BY `id` DESC");
// loop to show affiliates
while ($r = mysql_fetch_assoc($show))
{
// get affiliate info
$name = $r['name'];
$in = $r['in'];
$out $r['out'];
$img = $r['banner'];
echo "<a href='click.php?mode=out&id=$r[id]'><img src='$img' width='88' height='31' style='border: 0px; margin: 1px;' /></a>";
}
?>
Go back to page 1.