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
- Register Page
- Login Page
- Members Page
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
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.