First were going to make the sql table.
SQL
CREATE TABLE affiliates ( id int(11) NOT NULL auto_increment, countout smallint(3) NOT NULL, countin smallint(3) NOT NULL, wb_name varchar(20) NOT NULL default '', wb_email varchar(30) NOT NULL default '', ws_name varchar(20) NOT NULL default '', ws_desc varchar(20) NOT NULL default '', ws_link varchar(75) NOT NULL default '', ws_button varchar(75) NOT NULL default '', PRIMARY KEY (id), KEY id (id) ) TYPE=MyISAM;
All this is, is the information for the site you are linking. WB stands for Web Master. WS stands for web site DUH!!!1 and the id like always is unique like a snow flake there shall be no 2 the same. The countin and countout are the hits generated in and out from that specific site.
Now for the counting of the hits in and out, were going to call this counter.php
Counter.php
<? // We need to connect to the sql db include "connect.php"; // Affiliates linking to you should do so like this: http://yoursite.com/counter.php?inid=1 // So it will count the hits coming from the affiliate website with the ID = 1! if (isset($inid)){ // This will count for that website! $update = mysql_query("UPDATE affiliates SET countin = countin + 1 WHERE id='$inid'"); // After that redirects to your website. $url = "http://yoursite.com"; header("Location: $url"); mysql_close(); } // The variable $outid you´ll use to link to your affiliates. // This variable will be in the url on the affiliates´ buttons in your site! if (isset($outid)){ // Count it $update = mysql_query("UPDATE affiliates SET countout = countout + 1 WHERE id='$outid'"); // Select the website url with the given ID $result = mysql_query("SELECT ws_link FROM affiliates WHERE id='$outid'"); $row = mysql_fetch_array($result); $url = $row['ws_link']; // And redirect to it header("Location: $url"); mysql_close(); } ?>
Pretty much the commenting does it all, for people linking to you, they use the counter.php?inid=# of the affiliate linking to you. And when someone clicks the link off there site it updates it on your site. Also for linking on your site it is the same concept, counter.php?outid=# to affiliate you are linking to.
And now but not least, affiliates.php!
Affiliates.php
<? include "connect.php"; $result = mysql_query("SELECT * FROM affiliates ORDER BY id"); while($row = mysql_fetch_array($result)){ echo "<p><a href=\"http://yoursite.com/counter.php?outid=".$row['id']."\" target=\"_blank\"> <img src=\"".$row['ws_button']."\" width=88 height=31 alt=\"".$row['ws_name']."\" border=\"0\"></a><br>in: " . $row['countin'] . " | out: " . $row['countout'] . "</p>"; } ?>
To randomize the displaying of the affiliates update affiliates.php to this code.
Affiliates.php
<? include "connect.php"; $result = mysql_query(" SELECT * FROM affilliates ORDER by RAND();"); while($row = mysql_fetch_array($result)){ echo "<p><a href=\"http://yoursite.com/counter.php?outid=" . $row['id'] . "\" target=\"_blank\"> <img src=\"" . $row['ws_button'] . "\" width=88 height=31 alt=\"". $row['ws_name'] ."\" border=\"0\"></a><br>in: " . $row['countin'] . " | out: " . $row['countout'] . "</p>"; } ?>
Now to only display a select number of random affiliates use this code.
display.php
<? include "connect.php"; $result = mysql_query("SELECT * FROM affilliates ORDER by RAND() LIMIT 0,3"); while($row = mysql_fetch_array($result)){ echo "<p><a href=\"http://yoursite.com/counter.php?outid=" . $row['id'] . "\" target=\"_blank\"> <img src=\"" . $row['ws_button'] . "\" width=88 height=31 alt=\"". $row['ws_name'] ."\" border=\"0\"></a><br>in: " . $row['countin'] . " | out: " . $row['countout'] . "</p>"; } ?>