Ok for starters you will need: A server running MySQL and PHP.
OK if you have phpMyAdmin or you can import .sql files or directly use sql to do databasie stuff then use this code to create a table with all the information we need. You can also use PHP to do this but you need to be able to create tables with the username you are using. I'll show both PHP and SQL code for this.
SQL
CODE
CREATE TABLE `members` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT ,
`username` VARCHAR( 40 ) NOT NULL ,
`password` VARCHAR( 225 ) NOT NULL ,
`email` VARCHAR( 225 ) NOT NULL ,
`avatar` TINYTEXT NOT NULL ,
`level` ENUM( '1', '2' ) DEFAULT '1' NOT NULL ,
`bio` TEXT,
`joined` DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL ,
`logged` DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL ,
PRIMARY KEY ( `id` )
) TYPE = MYISAM;
`id` INT( 10 ) NOT NULL AUTO_INCREMENT ,
`username` VARCHAR( 40 ) NOT NULL ,
`password` VARCHAR( 225 ) NOT NULL ,
`email` VARCHAR( 225 ) NOT NULL ,
`avatar` TINYTEXT NOT NULL ,
`level` ENUM( '1', '2' ) DEFAULT '1' NOT NULL ,
`bio` TEXT,
`joined` DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL ,
`logged` DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL ,
PRIMARY KEY ( `id` )
) TYPE = MYISAM;
That was the SQL code to insert the table into the database. You can copy it directly into a .sql file (a text file with the extension changed) or if you are using phpMyAdmin there you can use the code on the "Query" tab on your database.
The PHP code is, in all essence, the same as the SQL code since you are running the same query.
But here it is anyway:
CODE
<?PHP
$user = "username"; //change with your username for the database
$password = "password"; //change with your password for the database
$host = "localhost"; //change with the host you have to connect to
$db = "database"; //change with the database name you have access to
//this defines the variable $conn and connects to your database host wether it is localhost or 127.0.0.1
$conn = mysql_connect("$host", "$user", "$password") or die(mysql_error());
//this function selects the database from the host since there maybe more
mysql_select_db("$db", $conn) or die(mysql_error();
/*
the main man/woman in this little sharade.
This defines the variable $sql and executes our query on the database.
If it returns false for some reason wrong password or not enough permissions it will display an error to right your wrongs.
*/
$sql = mysql_query("CREATE TABLE `members` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT ,
`username` VARCHAR( 40 ) NOT NULL ,
`password` VARCHAR( 225 ) NOT NULL ,
`email` VARCHAR( 225 ) NOT NULL ,
`avatar` TINYTEXT NOT NULL ,
`level` ENUM( '1', '2' ) DEFAULT '1' NOT NULL ,
`bio` TEXT,
`joined` DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL ,
`logged` DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL ,
PRIMARY KEY ( `id` )
) TYPE = MYISAM;") or die(mysql_error());
//checks the see if the query above returned true if it did it will display "The table etc etc"
if($sql){
echo'The table was successfully added to the database!';
}
?>
$user = "username"; //change with your username for the database
$password = "password"; //change with your password for the database
$host = "localhost"; //change with the host you have to connect to
$db = "database"; //change with the database name you have access to
//this defines the variable $conn and connects to your database host wether it is localhost or 127.0.0.1
$conn = mysql_connect("$host", "$user", "$password") or die(mysql_error());
//this function selects the database from the host since there maybe more
mysql_select_db("$db", $conn) or die(mysql_error();
/*
the main man/woman in this little sharade.
This defines the variable $sql and executes our query on the database.
If it returns false for some reason wrong password or not enough permissions it will display an error to right your wrongs.
*/
$sql = mysql_query("CREATE TABLE `members` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT ,
`username` VARCHAR( 40 ) NOT NULL ,
`password` VARCHAR( 225 ) NOT NULL ,
`email` VARCHAR( 225 ) NOT NULL ,
`avatar` TINYTEXT NOT NULL ,
`level` ENUM( '1', '2' ) DEFAULT '1' NOT NULL ,
`bio` TEXT,
`joined` DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL ,
`logged` DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL ,
PRIMARY KEY ( `id` )
) TYPE = MYISAM;") or die(mysql_error());
//checks the see if the query above returned true if it did it will display "The table etc etc"
if($sql){
echo'The table was successfully added to the database!';
}
?>
Etheir method produces the same table.
Now that we have the first steps out of the way it's time to move onto collecting people's membership details.
First though I suggest you take the following code and put it into a file called db.php it makes like easier instead of writing it out all the time.
CODE
<?PHP
$user = "username"; //change with your username for the database
$password = "password"; //change with your password for the database
$host = "localhost"; //change with the host you have to connect to
$db = "database"; //change with the database name you have access to
// Connects to the database
$conn = mysql_connect( "$host", "$user", "$password" );
// Selects the database we are going to be using
mysql_select_db( "$db", "$conn" );
?>
$user = "username"; //change with your username for the database
$password = "password"; //change with your password for the database
$host = "localhost"; //change with the host you have to connect to
$db = "database"; //change with the database name you have access to
// Connects to the database
$conn = mysql_connect( "$host", "$user", "$password" );
// Selects the database we are going to be using
mysql_select_db( "$db", "$conn" );
?>
OK now onto the registering part. First we need a HTML file to submit the information. This file is called join.htm
And it goes a little like this...
CODE
<form name="form1" method="post" action="register.php">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td> </td>
<td>* denotes a required field Note: If no password is entered you will be given a random one which you can change later. </td>
</tr>
<tr>
<td width="12%">Username:*</td>
<td width="88%"><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td><p>Confirm Password:</p>
</td>
<td><input name="cpassword" type="password" id="cpassword"></td>
</tr>
<tr>
<td>Email:*</td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td>Confirm Email:*</td>
<td><input name="cemail" type="text" id="cemail"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset"></td>
</tr>
</table>
</form>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td> </td>
<td>* denotes a required field Note: If no password is entered you will be given a random one which you can change later. </td>
</tr>
<tr>
<td width="12%">Username:*</td>
<td width="88%"><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="password" type="password" id="password"></td>
</tr>
<tr>
<td><p>Confirm Password:</p>
</td>
<td><input name="cpassword" type="password" id="cpassword"></td>
</tr>
<tr>
<td>Email:*</td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td>Confirm Email:*</td>
<td><input name="cemail" type="text" id="cemail"></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset"></td>
</tr>
</table>
</form>
You may have noticed that password is not a required field. That is because in the register form we can pick up they have not entered anything and assign them a random password.
Bring on the PHP!
The comments explain all of the bits in detail...
CODE
<?PHP
//this includes our db.php file with the connection to the database. If it can't find it for some reason it kills the script
require'db.php';
$email = $_POST['email'];
$cemail = $_POST['cemail'];
$username = $_POST['username'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
//Check to see if a password has been entered
if($password){
//if it has then check to see if confirm password and password fields match
if(!$password == $cpassword){
echo '<center><font color=red><strong>Password and Confirm Password do not match! Please enter them again below</strong></font></center>';
//shows the join form
include'join.htm';
//makes sure no other code executes
exit();
}
//if not tell them that they are going to get a random password
}else{
echo'<center><font color=blue><strong>You have not entered a password! A random password will be generated for you!</strong></font></center>';
//generates a random password using 7 of the letters and number below
$salt = "abchefghjkmnpqrstuvwxyz0123456789";
//makes the rand() function more random than normal
srand((double)microtime()*1000000);
//declares a variable
$i = 0;
//while i is less than or equal to 7 continue otherwise exit the loop
while ($i <= 7) {
//generates a random number from the random seed made from srand()
$num = rand() % 33;
//letter to add to the password
$tmp = substr($salt, $num, 1);
//adds the new letter to the rest of the password
$password = $password . $tmp;
/*adds 1 to the variable i if you remove this the loop will continue forever and you'll get an extrememly long password if the server doesnt die on you*/
$i++;
}
}
//same as above if the email field and the confirm email fields dont match then it will display the form again
if( ! $email == $cemail ){
echo'<center><font color=red><strong>Email and Confirm Email do not match! Please enter them again below!</strong></font></center>';
//shows the join form
include'join.htm';
//makes sure no other code executes
exit();
}
//this checks to see if the user has entered an email address and username
if( ( ! $email ) || ( !$username ) ){
echo '<center><font color=red><strong>You did not submit the following required information! </strong></font></center>';
//checks to see if they have entered an email address
if(!$email){
echo "<center><font color=red><strong>Email Address is a required field. Please enter it below.</strong></font></center>";
}
//checks to see if they have entered a username
if(!$username){
echo "<center><font color=red><strong>Desired Username is a required field. Please enter it below.</strong></font></center>";
}
//if not then show the form again
include'join.htm';
//and exit the script so nothing else executes
exit();
}
//this selects the email address in our table that the user submitted if it finds it
$sql_email_check = mysql_query("SELECT email FROM users WHERE email='$email_address'");
//this selects the username in our table that the user submitted if it finds it
$sql_username_check = mysql_query("SELECT username FROM users WHERE username='$username'");
//this checks to see if the above returned true meaning it found the username
if($sql_username_check){
//this double checks to see if the username was found and if so how many (which should be 1)
$username_check = mysql_num_rows($sql_username_check);
//If it found more than 0 usernames ie 1 in the table then it displays and error and gets rid of the variable
if(($username_check > 0)){
echo "Please fix the following errors: <br />";
if($username_check > 0){
echo "<center><font color=red><strong>The username you have selected has already been used by another member in our database. Please choose a different Username!</strong></font></center>";
unset($username);
}
//show the form... again :(
include 'join.htm';
//exit the script
exit();
}
}
//checks to see if the email check returned true
if($sql_email_check){
//if so how many email addresses did it find
$email_check = mysql_num_rows($sql_email_check);
//if more than 0 display and error and destroy the variable
if(($email_check > 0)){
echo "Please fix the following errors: <br />";
if($email_check > 0){
echo "<center><font color=red><strong>Your email address has already been used by another member in our database. Please submit a different Email address!</strong></font></center>";
unset($email_address);
}
//and show the form to the unlucky user
include 'join.htm';
//and kill the script
exit();
}
}
//this encrypts the password using md5 hash which cannot be decoded (as far as i know)
$db_password = md5($password);
//insert the data into our table
$sql = mysql_query("INSERT INTO members (email, username, password , joined)
VALUES('$email', '$username', '$db_password', now())");
//checks to see if the data was entered
if(!$sql){
echo 'There has been an error creating your account. Please contact the webmaster.';
exit();
} else {
//gets the user's id from the database
$userid = mysql_insert_id( $conn );
//the subject of the email
$subject = "Your Membership at My-Website.com!";
//the message remember to change My-Website.com for your url!
$message = "Dear $username,
Thank you for registering at our website, http://www.My-Website.com!
You are two steps away from accessing the exclusive members features at My Website!
To activate your membership, please click here: http://www.My-Website.com/activate.php?activate&id=$userid&code=$db_password
Once you activate your memebership, you will be able to login with the following information:
Username: $username
Password: $password
You can change your password anytime you log into your account in the Members Area
If you forget your password you can go onto our site and follow the instructions to recieve a new password.
Thanks!
coldkill
The Webmaster
This is an automated response, please do not reply!";
mail($email, $subject, $message, "From: My-Website.com<Team@My-Website.com>\nX-Mailer: PHP/" . phpversion());
echo 'Your membership information has been mailed to your email address! Please check it and follow the directions!';
}
//closed all mysql connections to save on bandwidth and stuff.
mysql_close();
?>
//this includes our db.php file with the connection to the database. If it can't find it for some reason it kills the script
require'db.php';
$email = $_POST['email'];
$cemail = $_POST['cemail'];
$username = $_POST['username'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
//Check to see if a password has been entered
if($password){
//if it has then check to see if confirm password and password fields match
if(!$password == $cpassword){
echo '<center><font color=red><strong>Password and Confirm Password do not match! Please enter them again below</strong></font></center>';
//shows the join form
include'join.htm';
//makes sure no other code executes
exit();
}
//if not tell them that they are going to get a random password
}else{
echo'<center><font color=blue><strong>You have not entered a password! A random password will be generated for you!</strong></font></center>';
//generates a random password using 7 of the letters and number below
$salt = "abchefghjkmnpqrstuvwxyz0123456789";
//makes the rand() function more random than normal
srand((double)microtime()*1000000);
//declares a variable
$i = 0;
//while i is less than or equal to 7 continue otherwise exit the loop
while ($i <= 7) {
//generates a random number from the random seed made from srand()
$num = rand() % 33;
//letter to add to the password
$tmp = substr($salt, $num, 1);
//adds the new letter to the rest of the password
$password = $password . $tmp;
/*adds 1 to the variable i if you remove this the loop will continue forever and you'll get an extrememly long password if the server doesnt die on you*/
$i++;
}
}
//same as above if the email field and the confirm email fields dont match then it will display the form again
if( ! $email == $cemail ){
echo'<center><font color=red><strong>Email and Confirm Email do not match! Please enter them again below!</strong></font></center>';
//shows the join form
include'join.htm';
//makes sure no other code executes
exit();
}
//this checks to see if the user has entered an email address and username
if( ( ! $email ) || ( !$username ) ){
echo '<center><font color=red><strong>You did not submit the following required information! </strong></font></center>';
//checks to see if they have entered an email address
if(!$email){
echo "<center><font color=red><strong>Email Address is a required field. Please enter it below.</strong></font></center>";
}
//checks to see if they have entered a username
if(!$username){
echo "<center><font color=red><strong>Desired Username is a required field. Please enter it below.</strong></font></center>";
}
//if not then show the form again
include'join.htm';
//and exit the script so nothing else executes
exit();
}
//this selects the email address in our table that the user submitted if it finds it
$sql_email_check = mysql_query("SELECT email FROM users WHERE email='$email_address'");
//this selects the username in our table that the user submitted if it finds it
$sql_username_check = mysql_query("SELECT username FROM users WHERE username='$username'");
//this checks to see if the above returned true meaning it found the username
if($sql_username_check){
//this double checks to see if the username was found and if so how many (which should be 1)
$username_check = mysql_num_rows($sql_username_check);
//If it found more than 0 usernames ie 1 in the table then it displays and error and gets rid of the variable
if(($username_check > 0)){
echo "Please fix the following errors: <br />";
if($username_check > 0){
echo "<center><font color=red><strong>The username you have selected has already been used by another member in our database. Please choose a different Username!</strong></font></center>";
unset($username);
}
//show the form... again :(
include 'join.htm';
//exit the script
exit();
}
}
//checks to see if the email check returned true
if($sql_email_check){
//if so how many email addresses did it find
$email_check = mysql_num_rows($sql_email_check);
//if more than 0 display and error and destroy the variable
if(($email_check > 0)){
echo "Please fix the following errors: <br />";
if($email_check > 0){
echo "<center><font color=red><strong>Your email address has already been used by another member in our database. Please submit a different Email address!</strong></font></center>";
unset($email_address);
}
//and show the form to the unlucky user
include 'join.htm';
//and kill the script
exit();
}
}
//this encrypts the password using md5 hash which cannot be decoded (as far as i know)
$db_password = md5($password);
//insert the data into our table
$sql = mysql_query("INSERT INTO members (email, username, password , joined)
VALUES('$email', '$username', '$db_password', now())");
//checks to see if the data was entered
if(!$sql){
echo 'There has been an error creating your account. Please contact the webmaster.';
exit();
} else {
//gets the user's id from the database
$userid = mysql_insert_id( $conn );
//the subject of the email
$subject = "Your Membership at My-Website.com!";
//the message remember to change My-Website.com for your url!
$message = "Dear $username,
Thank you for registering at our website, http://www.My-Website.com!
You are two steps away from accessing the exclusive members features at My Website!
To activate your membership, please click here: http://www.My-Website.com/activate.php?activate&id=$userid&code=$db_password
Once you activate your memebership, you will be able to login with the following information:
Username: $username
Password: $password
You can change your password anytime you log into your account in the Members Area
If you forget your password you can go onto our site and follow the instructions to recieve a new password.
Thanks!
coldkill
The Webmaster
This is an automated response, please do not reply!";
mail($email, $subject, $message, "From: My-Website.com<Team@My-Website.com>\nX-Mailer: PHP/" . phpversion());
echo 'Your membership information has been mailed to your email address! Please check it and follow the directions!';
}
//closed all mysql connections to save on bandwidth and stuff.
mysql_close();
?>
OK thats the first part of this tutorial. I shall update it soon with the rest of the system!
/coldkill