Jump to content


Photo
* * * - - 2 votes

Affillate System


  • Please log in to reply
6 replies to this topic

#1 liveman

liveman

    Young Padawan

  • Members
  • Pip
  • 246 posts
  • Location:New Jersey

Posted 09 March 2006 - 12:04 AM

Introduction:
This script will be geared to a person who has a general idea of php, common functions will not be told what they do, the same goes with SQL. Also I am assuming you know how to insert an SQL table into a database, and you know what is needed to connect to a database.
With that said I will now tell you what you should excpet out of this tutorial:
  • A joining system
  • A unique linking system which will allow them to gain a point for each click they get
the joining system has:
  • Register Page
  • Login Page
  • Members Page
Begining
Well lets begin, I will comment in the script itself and also add an additional comments after the page

here is your sql information that you need
 CREATE TABLE `log` (
  `ID` int(11) NOT NULL auto_increment,
  `ip` text NOT NULL,
  `date` text NOT NULL,
  `aff_ID` text NOT NULL,
  UNIQUE KEY `ID` (`ID`)
) TYPE = MyISAM;

CREATE TABLE `users` (
  `ID` int(11) NOT NULL auto_increment,
  `user` text NOT NULL,
  `pass` text NOT NULL,
  `email` text NOT NULL,
  `name` text NOT NULL,
  `activate` text NOT NULL,
  `number` text NOT NULL,
  UNIQUE KEY `ID` (`ID`)
) TYPE=MyISAM;
Ok, well now we can continue with the system
Functions.php
<?PHP
function redirect($url)
   { 
 header ("LOCATION: $url");
  exit;
   }
function connect()
{
 $db = 'DATABASE';
 $dbhost = 'Host';
 $dbuser = 'User';
 $dbpass = 'Pass';
 mysql_connect($dbhost, $dbuser, $dbpass) or die(mysql_error());
 mysql_select_db($db) or die(mysql_error());
}
function config();
{
$config['site']['on'] = 'True'; // Set to False to shut off!
$config['site']['url'] = 'www.sitename.com'; // Give url to main folder
}
function rec($variable,$text)
{
 if(empty($variable))
  {
 redirect('Http://' . $config[site][url] . 'register.php&error=1&problem=' . $text . '&sessions=true');
 exit;
  }
}
?>
Ok, well that wasn't hard... I hope, really I think you should know them so I wont even comment.

Next: login.php
<?PHP
if($submit) {
 // Please Note: the session_start() may need to be moved to your index to prevent an error!
session_start();
require_once 'functions.php';
connect();
config();
$user  = $_POST['user']; 
$pass = $_POST['pass']; 
// Check for errors now,
   // First: Missing field!
  if(empty($user) || empty($pass))
   {
 redirect('Http://' . $config['site']['url'] . '/login.php?error=1');
exit;   
 }
 // Second: Username or Password incorrect
 $en = sha1($pass);
 $num = mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `username` = '$user' AND `password` = '$en'")) or die(mysql_error());
 if($num == '0')
  {
 redirect('Http://' . $config['site']['url'] . '/login.php?error=2');
exit;   
 }
 // Third: Not activated!
 $num2 = mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `username` = '$user' AND `activate` = 'No'")) or die(mysql_error());
 if($num2 == '0')
  {
 redirect('Http://' . $config['site']['url'] . '/login.php?error=3');
exit;   
 }
$_SESSION['affuser'] = $user;
$_SESSION['affpass'] = $en;
if(empty($return))
 {
 $return = 'Http://' . $config['site']['url'] . '/userpage.php';
 }
redirect($return);
exit; 
}
else
{
 switch($error)
 { 
 case  '1':
 echo '<font color = "red"><b><center>Error:</b>A required field is missing!</font></center>';
break;
case  '2':
 echo '<font color = "red"><b><center>Error:</b>You have entered incorrect information!</font></center>';
break;
case  '3':
 echo '<font color = "red"><b><center>Error:</b>Your account is not activated!</font></center>';
break;
 }
echo '
 <form action = "login.php" method = "post">
<table border = "0">
 <tr> 
 <Td>Username:</td>
 <td><input type = "text" name = "user"></td>
 </tr>
 <tr>
 <td>Password:</td>
 <td><input type = "password" name = "pass"></td>
 </tr>
 <tr>
 <td><input type = "submit" value = "Login"></td>
 </tr>
 </table>
 </form>
Ok, this page checks and if everything is good logs you in, also it shows the login form! Reallly, nothing wierd hear except the redirect, but I am sure you know what that does.. if you add any more checks remember an exit; after the redirect or it wont redirect.

Ok now we will work on the register.php
<?PHP
  if(!empty($_SESSION['affuser']) and !empty($_SESSION['affpass']))
   {
// Check to see if already logged in 
 if(empty($return))
 {  
redirect('Http://' . $config['site']['url'] . '/userpage.php');
exit;
 } 
 // Ok, they aren't lets continue 
if($submit)
 {
 require_once 'functions.php';
connect();
config();
$user = $_POST['user'];
$pass = $_POST['pass'];
$email = $_POST['email'];
$name = $_POST['name'];
// Set sessions in case an error is returned
$_SESSION['user']= $_POST['user'];
$_SESSION['pass']= $_POST['pass'];
$_SESSION['email']= $_POST['email'];
$_SESSION['name']= $_POST['name'];
// done, lets see if we got any errors
rec($user, 'username'); // I will explain aftewards
rec($pass, 'password');
rec($email, 'email');
rec($name, 'name');
// Is the username available?
$num = mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `username` = '$user'")) or die(mysql_error());
 if($num == '1')
  {   
 // No, lets break the news
redirect('Http://' . $config['site']['url'] . '/register.php&error=2');
 exit;
 }
// Yes, it is we can move on..

$en = sha1($pass); // Encrypts the password using Sha1()
$n = rand(111111,9999999);
$number = sha1($n);
mysql_query("INSERT INTO `users` VALUES ('', '$user', '$en', '$email', '$name', 'No', '$number')") or die(mysql_error());

// Ok, well errors are done, inserted user to db now lets send activation email!
$header = "From: YOUR SITE HERE";
$sub = "Activate (" . $user . ") Account";
$message = "
\n\n\n
Dear " . $name . "\n
\n
 Below is your account information:\n
   Username:" . $user . "\n
   Password:" . $pass . "\n
\n
You can login using that information any time, after you activate your account! To activate your account click on the link below\n
\n
Http://" . $config['site']['url'] . "/activate.php&username=" . $user . "&number=$number";
mail($email, $sub, $message, $header);
// Letter sent, lets tell me and end.
redirect('Http://' . $config['site']['url'] . '/wait.php');
exit;
} else {
// Echo any error messages we have
 switch($error) 
 {
  case 1: 
 echo '<font color = "red"><b><center>Error:</b>the ' . $problem . ' field is required!</font></center>';
 break;
 case 2: 
 echo '<font color = "red"><b><center>Error:</b>That username has already been taken</font></center>';
 break;
 }
?>
<form action = "register.php" method = "post">
<table border = "0">
 <tr>
 <td>Full Name:</td>
 <td><input type = "text" name = "name" value = "<? echo $_SESSION['name']; ?>"></td>
 </tr>
 <tr>
 <td>UsernameL</td>
 <td><input type = "text" name = "user" value = "<? echo $_SESSION['user']; ?>"></td>
 </tr>
 <tr>
 <td>Password:</td>
 <td><input type = "password" name = "pass"></td>
 </tr>
 <tr>
 <td>Email:</td>
 <td><input type = "text" name = "email" value = "<? echo $_SESSION['email']; ?>"></td>
 </tr>
 <tr>
 <td><input type = "submit" value = "Register" name = "submit"></td>
</tr>
</table></form>
<? } ?>

Ok, well the only thing which might be hard here is the rec() which is a function I created, basicly it checks if the first part (the variable) is empty, if so it outputs the message (the second part). It will save you alot of time...trust me! Well there are only two pages for the login system (then we will work on the member page) The user will get his link in the member page. Well lets work on wait.php first because thats easy :D
wait.php
<? session_start(); ?>
  <html>
  <head></head>
 <body>
	<center><font color = "red">Successfully registered!</font><br>
Congradulations, however before you can start using our system you must first activate your account! We have sent an email to 
<?PHP echo $_SESSION['email']; ?> there is a link in that email, after this link is clicked your account will be active!<br>
<Br>
<Br>
 </body>
 </html>
Ok, well that was easy! Now let us begin the activate page!
activate.php
<?PHP
require_once 'functions.php';
connect();
   $num = mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `username` = '$username AND `number` = '$number'")) or die (mysql_error());
 if($num == '0') 
{
 echo '<font color = "red><b><center>Error:</b>The url you used is not valid!</b>';
 exit;
}
	 $num2 = mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `username` = '$username AND `activate` = 'Yes''")) or
 if($num2 == '0')
  {
 echo '<font color = "red><b><center>Error:</b>It appears this account has already been activated!!</b>';
 exit;
}
mysql_query("UPDATE `users` SET `activate` = 'Yes' WHERE `username` = '$username' AND `number` = '$number'") or die(mysql_error());
 echo '<center><font color = "red">Successfully activated your account! You can now login!</font><br></center>';
?>
<br>
<?PHP
  require_once 'login.php';
?>
<br>
Ok, now I am going to have the members page, all it will have is the show aff link in here. I really do not know everything you want so that is why it will be simple.

userpage.php
<?PHP
  if(empty($_SESSION['affuser']) and empty($_SESSION['affpass']))
   {
 require_once 'functions.php';
connect();
config();
// Check to see if already logged in 
 return = ''Http://' . $config['site']['url'] . '/userpage.php';
require_once 'login.php';
exit;
 } 
$user = $_SESSION['affuser'];
$pass = $_SESSION['affpass'];
$r = mysql_fetch_row(mysql_query("SELECT `*` FROM `users` WHERE `username` = '$user' AND `password` = '$pass'")) or die(mysql_error());
 echo '
 <center>
  <table border = "0">
 <tr>
 <td>Your Link:</td>
 <td><input type = "text" name = "link" size = "45" readonly value = "'Http://' . $config['site']['url'] .'/?aff=' . $r[0] . '&hide=true'"></td>
 </tr>
 </table>
 </center>';
?>
Ok, well all that does is gain the info from the db and display it in the readonly textbox. Now put the following code were you want it to record the click.
<?PHP
  require_once 'functions.php';
 connect();
 config();
 if(!empty($aff))
 {
 $ip = $_SERVER['REMOTE_ADDR'];
 $date = date("l F d, Y");
 mysql_query("INSERT INTO `log` ('', '$ip', '$date', '$aff')") or die(mysql_error());
 if(!empty($hide)) 
  {
   redirect('Http://' . $config['site']['url']);
   exit;
 }
?>
Well that is it.. if you have any questions go ahead and ask, I hope you learned something from this tutorial!

Edited by liveman, 09 March 2006 - 05:37 PM.

  • AaMuckedGr likes this

#2 Paint

Paint

    Young Padawan

  • Members
  • Pip
  • 88 posts

Posted 14 March 2006 - 09:37 PM

I think that I just fell in love with you! This is AMAZING. I admire your PHP skills, I hope to be able to code like you one day. *looks FAR into the future*

#3 liveman

liveman

    Young Padawan

  • Members
  • Pip
  • 246 posts
  • Location:New Jersey

Posted 15 March 2006 - 07:53 PM

Actually this system is very basic, it has no special features, and is just an example of what you would do. Also it is mainly a members system, just with an added logging (affilliate) system.

#4 Indigo

Indigo

    Official Alien

  • Members
  • PipPipPip
  • 617 posts
  • Gender:Male
  • Location:Trondheim, Norway
  • Interests:Computing in general, especially design and programming of all kinds.

Posted 16 March 2006 - 11:18 AM

I agree with you both - It's great, but nothing unique.
I didn't read the entire code, because it's boring and quite hard for me to read not-colored syntax, but it seams quite good.
This is way better than what I would make if I sat down, so I might test it sometime later.

Thanks a lot for the tutorial!

#5 Adam050

Adam050

    Young Padawan

  • Members
  • Pip
  • 2 posts

Posted 05 June 2006 - 08:39 AM

Hey

I have tested many different Affilate Systems and most of them where rubbish or didnt even work. I'm great at PHP but i was to lazy to fix them :) Thanks for this i am going to try it tonigth and i hope it works.

Many Thanks :D

#6 FTH8er

FTH8er

    Young Padawan

  • Members
  • Pip
  • 4 posts

Posted 03 March 2007 - 01:47 AM

The only thing that i get wrong with this is an error in the login

Parse error: syntax error, unexpected $end in /home/lonew6/public_html/login.php on line 70

and idk y.

#7 nitr0x

nitr0x

    Young Padawan

  • Members
  • Pip
  • 201 posts

Posted 03 March 2007 - 06:25 PM

Yea, when doing PHP Code, it's best to use the tags sql or html or something since it will at least use some colours. Not a bad tutorial though, will be helpful for some.

Added some bits on the login.php - errors should be fixed FTH8er.

<?PHP
if($submit) {
// Please Note: the session_start() may need to be moved to your index to prevent an error!
session_start();
require_once 'functions.php';
connect();
config();
$user = $_POST['user'];
$pass = $_POST['pass'];
// Check for errors now,
// First: Missing field!
if(empty($user) || empty($pass)){
redirect('Http://' . $config['site']['url'] . '/login.php?error=1');
exit;
}
// Second: Username or Password incorrect
$en = sha1($pass);
$num = mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `username` = '$user' AND `password` = '$en'")) or die(mysql_error());
if($num == '0'){
redirect('Http://' . $config['site']['url'] . '/login.php?error=2');
exit;
}
// Third: Not activated!
$num2 = mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `username` = '$user' AND `activate` = 'No'")) or die(mysql_error());
if($num2 == '0'){
redirect('Http://' . $config['site']['url'] . '/login.php?error=3');
exit;
}
$_SESSION['affuser'] = $user;
$_SESSION['affpass'] = $en;
if(empty($return)){
$return = 'Http://' . $config['site']['url'] . '/userpage.php';
}
redirect($return);
exit;
}else{
switch($error){
case '1':
echo '<font color = "red"><b><center>Error:</b>A required field is missing!</font></center>';
break;
case '2':
echo '<font color = "red"><b><center>Error:</b>You have entered incorrect information!</font></center>';
break;
case '3':
echo '<font color = "red"><b><center>Error:</b>Your account is not activated!</font></center>';
break;
}
echo'
<form action = "login.php" method = "post">
<table border = "0">
<tr>
<Td>Username:</td>
<td><input type = "text" name = "user"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type = "password" name = "pass"></td>
</tr>
<tr>
<td><input type = "submit" value = "Login"></td>
</tr>
</table>
</form>';
}
?>


And I must actually say, after looking through some of the code, it's very bad coding, I don't mean to be offensive or anything but for me, when it comes to coding, specially PHP, actually, definitely PHP, I like it to be very organised since disorganised PHP is a nightmare going through to fix things. By organised, I mean proper indentation of your code (when using a {, indents on the code in between should be indented. And also I believe in using (" ") on functions such as echos because it makes it easier to see and such.

They are just some things to consider :D

Edited by nitr0x, 03 March 2007 - 06:44 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users