This tutorial is intended for those with a basic knowledge of PHP, most of the code will have comments.
First things first, we need to run an SQL query, to set up our database.
CREATE TABLE `affiliates` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL default '',
`banner` text NOT NULL,
`url` text NOT NULL,
`email` varchar(255) NOT NULL default '',
`in` int(11) NOT NULL default '0',
`out` int(11) NOT NULL default '0',
`active` int(1) NOT NULL default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM;
Now that thats done, we can start coding the system.
Create a new file and name it connect.php
<?php
$host ="localhost"; // Default value, 99% of the time you won't need to change it
$user = "username"; // Database username
$pass = "password"; // Database password
$db = "database"; // The name of your SQL database
@mysql_connect($host,$user,$pass) or die("Could not connect<br />")".mysql_error());
@mysql_select_db($db) or die("Could not connect to MySQL database $db");
?>
All this does is connect us to our database, if for any reason it fails the error message will be shown.
Next, create a file called add.php. This is where your viewer can submit his/her application.
<?php
include("connect.php"); // Includes our connect file
$go = $_POST['add'];
if ($go)
{
// If the form has been submitted, then process it
// Get all the variables from the form
$name = htmlspecialchars(addslashes($_POST['name']));
$url = htmlspecialchars(addslashes($_POST['url']));
$image = htmlspecialchars(addslashes($_POST['image']));
$email = htmlspecialchars(addslashes($_POST['email']));
// Insert the info into the database
mysql_query("INSERT INTO `affiliates` (`name`,`banner`,`url`,`email`,`active`) VALUES ('$name','$image','$url','$email','0');") or die("Insert Query Failed");
// The query worked, tell your user
echo "Your affiliate request has been submitted, but it must be approved before it will show up on this site. You will recieve an email once you have been accepted.";
}
else
{
// If the form hasn't been submitted yet
// Show the form
echo "<form action='' method='post'>
Your Name: <input type='text' name='name' /><br />
Your Email: <input type='text' name='email' /><br />
Site URL: <input type='text' name='url' /><br />
Affiliate Image: <input type='text' name='image' /><br />
<input type='submit' name='add' value='Apply' />";
}
?>
Now we need a file to count the amount of clicks out. Incoming hits is done through the display page.
[code]<?php
// Includes our connect file
include "connect.php";
// Count the clicks
$mode = $_GET['mode'];
// Gets the mode
// A switch is like a series of ifs and elses, but in less space, and more efficient
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 the user
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]);
// Checks the database
$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 the user
header("Location: $get
[url]");
break;
}
?>
[/code]
You will have to replace
http://yoursite.com with the URi to your site.
And the last file of this tutorial, is the show code. Put this code wherever you would like your affiliates to be displayed!
<?php
include "connect.php"; // Includes our connect file
// View affiliates
// This gets the affiliates that are approved from newest to oldest.
$show = mysql_query("SELECT * FROM `affiliates` WHERE `active` = '1' ORDER BY `id` DESC");
// Loop to show our 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>";
}
?>
Written by Brady