Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[PHP] Refferal System


  • Please log in to reply
6 replies to this topic

#1 Overload

Overload

    CC Regular

  • Member
  • PipPipPip
  • 29 posts

Posted 29 September 2009 - 05:53 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];
$URL = mysql_real_escape_string($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];
$URL = mysql_real_escape_string($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. (Ive added a mysql injection prevention code).
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
  • 3

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 29 September 2009 - 06:00 AM

Very cool, Adam! +rep
  • 0

#3 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 29 September 2009 - 06:56 AM

Very nice! +rep
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#4 debtboy

debtboy

    CC Devotee

  • Just Joined
  • PipPipPipPipPipPip
  • 499 posts

Posted 29 September 2009 - 07:55 AM

Good Tutorial +rep :)
  • 0

#5 amrosama

amrosama

    CC Mentor

  • VIP Member
  • PipPipPipPipPipPipPipPip
  • 2765 posts

Posted 29 September 2009 - 11:06 AM

nice work
thnx for sharing it :D +rep
  • 0
yo homie i heard you like one-line codes so i put a one line code that evals a decrypted one line code that prints "i love one line codes"
eval(base64_decode("cHJpbnQgJ2kgbG92ZSBvbmUtbGluZSBjb2Rlcyc7"));
www.amrosama.com | the unholy methods of javascript

#6 DirectXp7

DirectXp7

    CC Lurker

  • New Member
  • Pip
  • 3 posts

Posted 20 March 2012 - 10:32 AM

nice!! :), Exelent brother :love:

+Rep
  • 0

#7 TalhaChamp

TalhaChamp

    CC Lurker

  • Just Joined
  • Pip
  • 2 posts
  • Programming Language:C, Java, C++, C#, PHP, JavaScript, PL/SQL, Visual Basic .NET, Logo
  • Learning:C, Java, C++, C#, PHP, (Visual) Basic, Python, JavaScript, Visual Basic .NET

Posted 27 June 2012 - 11:55 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];
$URL = mysql_real_escape_string($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];
$URL = mysql_real_escape_string($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. (Ive added a mysql injection prevention code).
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



hey can u pleaseee give me the full source i undrestand but i dont want to mistake please share us you file.php pleaseeee
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download