Overload
08-11-2009, 01:05 AM
Hey,
This is a basic referral system i typed up while in school, It roughly took 15 mins. So here goes.
We start off with the database structure:
CREATE TABLE `referral` (
`id` int(11) NOT NULL auto_increment,
`URL` varchar(255) NOT NULL default '',
`hits` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
The next part is the actual file to log the clicks. referral.php:
<?
$connect = mysql_connect("localhost","DATABASE USERNAME","DATABASE PASSWORD");
mysql_select_db(DATABASE NAME) or die(mysql_error());
$URL = $_POST[URL];
$check = mysql_query("SELECT * FROM referral WHERE URL='$URL'");
$data = mysql_fetch_array($check);
$check = mysql_num_rows($check);
if($check>0)
{
echo("Error. Refferal URL/ID is invalid.");
exit();
}
$hits = $data[hits]++;
$update = mysql_query("Update referral set hits = '$hits' where id = '$data[id]'");
echo ("<meta http-equiv=\"Refresh\" content=\"0; URL=http://$URL\"/>Thank You! You will be redirected to $URL");
?>
$connect = mysql_connect("localhost","DATABASE USERNAME","DATABASE PASSWORD");
mysql_select_db(DATABASE NAME) or die(mysql_error());
The above code defines the database we intend to connect to. This will be the database you put the above mySQL database structure into.
$URL = $_POST[URL];
$check = mysql_query("SELECT * FROM referral WHERE URL='$URL'");
$data = mysql_fetch_array($check);
$check = mysql_num_rows($check);
The first line of this catches the name of the link that has been clicked so any links will always be like refferal.php?URL=thesitename.com.
The next few lines are different check functions to gather information from the database to declare if the URL is vaild.
if($check>0)
{
echo("Error. Refferal URL/ID is invalid.");
exit();
}
This part will check if there is a row in the database that is the same as the URL gathered by the URL variable. If there is no row like this an error message will appear.
$hits = $data[hits]++;
$update = mysql_query("Update referral set hits = '$hits' where id = '$data[id]'");
echo ("<meta http-equiv=\"Refresh\" content=\"0; URL=http://$URL\"/>Thank You! You will be redirected to $URL");
The first line collects the current number in the database and then adds +1 onto it and then the next line saves the new number in the database. The last line will then finally redirect the person to the website.
Inportant Notes
All url's wising to be counted must start like so referral.php?URL=
You must use PHPmyadmin to enter the links to be counted or another mySQL database manager.
Hope this helps, Adam
This is a basic referral system i typed up while in school, It roughly took 15 mins. So here goes.
We start off with the database structure:
CREATE TABLE `referral` (
`id` int(11) NOT NULL auto_increment,
`URL` varchar(255) NOT NULL default '',
`hits` varchar(255) NOT NULL default '',
PRIMARY KEY (`id`)
) TYPE=MyISAM;
The next part is the actual file to log the clicks. referral.php:
<?
$connect = mysql_connect("localhost","DATABASE USERNAME","DATABASE PASSWORD");
mysql_select_db(DATABASE NAME) or die(mysql_error());
$URL = $_POST[URL];
$check = mysql_query("SELECT * FROM referral WHERE URL='$URL'");
$data = mysql_fetch_array($check);
$check = mysql_num_rows($check);
if($check>0)
{
echo("Error. Refferal URL/ID is invalid.");
exit();
}
$hits = $data[hits]++;
$update = mysql_query("Update referral set hits = '$hits' where id = '$data[id]'");
echo ("<meta http-equiv=\"Refresh\" content=\"0; URL=http://$URL\"/>Thank You! You will be redirected to $URL");
?>
$connect = mysql_connect("localhost","DATABASE USERNAME","DATABASE PASSWORD");
mysql_select_db(DATABASE NAME) or die(mysql_error());
The above code defines the database we intend to connect to. This will be the database you put the above mySQL database structure into.
$URL = $_POST[URL];
$check = mysql_query("SELECT * FROM referral WHERE URL='$URL'");
$data = mysql_fetch_array($check);
$check = mysql_num_rows($check);
The first line of this catches the name of the link that has been clicked so any links will always be like refferal.php?URL=thesitename.com.
The next few lines are different check functions to gather information from the database to declare if the URL is vaild.
if($check>0)
{
echo("Error. Refferal URL/ID is invalid.");
exit();
}
This part will check if there is a row in the database that is the same as the URL gathered by the URL variable. If there is no row like this an error message will appear.
$hits = $data[hits]++;
$update = mysql_query("Update referral set hits = '$hits' where id = '$data[id]'");
echo ("<meta http-equiv=\"Refresh\" content=\"0; URL=http://$URL\"/>Thank You! You will be redirected to $URL");
The first line collects the current number in the database and then adds +1 onto it and then the next line saves the new number in the database. The last line will then finally redirect the person to the website.
Inportant Notes
All url's wising to be counted must start like so referral.php?URL=
You must use PHPmyadmin to enter the links to be counted or another mySQL database manager.
Hope this helps, Adam