Tutorial: How to create a simple PHP Registration System
NOTE: This tutorial assumes some knowledge of PHP. First, you must create a users table in your mySQL database that contains the following columns:
- user_id [int, primary]
- username [text]
- first_name [text]
- surname [text]
- email [text]
- code [text]
- verified [boolean]
<form action="check_registration.php" method="post">
First Name:
<input type="text" name="fname" id="textbox" />
Surname:
<input type="text" name="sname" id="textbox" />
Username:
<input type="text" name="uname" id="textbox" />
Password:
<input type="text" name="pword" id="textbox" />
Email:
<input type="text" name="email" id="textbox" />
<input type="submit" id="click" value="Register User" />
</form>
<input type="text" name="fname" id="textbox" />
Surname:
<input type="text" name="sname" id="textbox" />
Username:
<input type="text" name="uname" id="textbox" />
Password:
<input type="text" name="pword" id="textbox" />
Email:
<input type="text" name="email" id="textbox" />
<input type="submit" id="click" value="Register User" />
The above creates a form which allows the user to input their unique details. You may add tables or css styling as you please to achieve your desired look.
Next, create a file called check_registration.php, which collects the information passed by the form using the POST method. To begin, connect to your database, and perform a SELECT * FROM users query.
if($r = mysql_query ($query))
{
The above compares the username and email given from the POST data against each entry in the database, and returns a flag "exists" as true if either has previously been stored.
If the username or email already exists, tell the user:
{
while($row = mysql_fetch_array($r))
{
{
$user = $row['uname'];
$mail = $row['email'];
$new_user = $_POST['uname'];
$new_mail = $_POST['email'];
if((strcmp($user, $new_user) == 0) || strcmp($mail, $new_mail))
{
}
$mail = $row['email'];
$new_user = $_POST['uname'];
$new_mail = $_POST['email'];
if((strcmp($user, $new_user) == 0) || strcmp($mail, $new_mail))
{
$exists = 1;
}
if($exists)
{
Otherwise, do the following:
Encrypt the password using the 'md5' function.
Also create a unique code, so the user can verify their email address (see later):
{
echo "User already exists. <br \>";
echo "<a href=\"register\">Try Again</a>";
}echo "<a href=\"register\">Try Again</a>";
$length = 15;
$characters = ‘0123456789abcdefghijklmnopqrstuvwxyz’;
$random = ”;
for ($p = 0; $p < $length; $p++)
{
$c_id = md5($random);
Use an INSERT statement to store the relevant POST data into the database.
If the mySQL query is successful, then tell the user they have registered successfully, and use the mail function as follows to send a confirmation email:
$characters = ‘0123456789abcdefghijklmnopqrstuvwxyz’;
$random = ”;
for ($p = 0; $p < $length; $p++)
{
$random .= $characters[mt_rand(0, strlen($characters))];
}$c_id = md5($random);
$message = "Thank you for registering.\n\nYour details are:\nUsername: $uname\nPassword: $pword\n\nConfirm your registration by clicking here: http://binaryblot.com/verify?id=$c_id";
$subject = "Registration confirmation.";
$header = "From: YourWebsite\r\n";
mail($email, $subject, $message, $header);
$subject = "Registration confirmation.";
$header = "From: YourWebsite
mail($email, $subject, $message, $header);