Before you can provide login capability, you need to already have a table in the database with usernames and passwords so that the login page can match them. Instead of having an administrator enter usernames and passwords for users (e.g., visitors), you can the user to create their own username and password. This can be accomplished by creating a registration page that will allow a user to "register" his or her own username and password into the database.
Below is a list of resources for creating login/registration pages:
The code that will be created will perform the following tasks:
<div data-role="content">
<h2 class="ui-body ui-body-a ui-corner-all">EMPLOYEE REGISTRATION</h2>
<div class="ui-body ui-body-a ui-corner-all">
<form action="registration_page.php" method="post">
<label for="FirstName">First Name:</label>
<input type="text" name="FirstName" size="20" data-mini="true" value=""/>
<label for="LastName">Last Name:</label>
<input type="text" name="LastName" size="20" data-mini="true" value=""/>
<label for="Email">Email Address:</label>
<input type="text" name="Email" size="20" data-mini="true" value="" />
<label for="Password1">Password:</label>
<input type="password" name="Password1" size="20"data-mini="true"/>
<label for="Password2">Re-enter Password:</label>
<input type="password" name="Password2" size="20"data-mini="true"/>
<p><input type="submit" name="Submit" value="REGISTER" data-inline="true" data-mini="true"/></p>
</form>
</div>
</div>
A common method is to check to see if a form has been submitted by the user clicking a submit button (REGISTER). It is important to remember that EACH form element (sometimes called a form variable) is treated as an element of an array--the _$POST array to be exact. To see these elements we will "trace" its results.
<div data-role="content">
<h2 class="ui-body ui-body-a ui-corner-all">EMPLOYEE REGISTRATION</h2>
<div class="ui-body ui-body-a ui-corner-all">
<?php if ( empty( $_POST ) ) { ?>
<form action="registration_page.php" method="post">
<label for="FirstName">First Name:</label>
<input type="text" name="FirstName" size="20" data-mini="true" value=""/>
<label for="LastName">Last Name:</label>
<input type="text" name="LastName" size="20" data-mini="true" value=""/>
<label for="Email">Email Address:</label>
<input type="text" name="Email" size="20" data-mini="true" value="" />
<label for="Password1">Password:</label>
<input type="password" name="Password1" size="20"data-mini="true"/>
<label for="Password2">Re-enter Password:</label>
<input type="password" name="Password2" size="20"data-mini="true"/>
<p><input type="submit" name="Submit" value="REGISTER" data-inline="true" data-mini="true"/></p>
</form>
<?php
}
else
{
echo ("Hello World");
}
?>
</div>
</div>
CODE EXPLANATION:
- The empty() method is used to check to see if the $_POST array is, well, "empty." When the page first loads, the $_POST array is indeed empty.
Once a user clicks the REGISTER button and the page reloads on top of itself (self posting) because it has the same name as the current page,
the array is populated with one element (Submit) if not input fields have data entered or more elements (the form elements) if any or all of
the input fields have data entered.
- When the page first loads, the empty() method returns true, and the "if" portion is executed and the form is displayed.
- When the REGISTER button is pressed, the empty() method returns false and the "else" portion of the "if/else" is executed and the
output of the echo statement is displayed (e.g., Hello World).
<?php
}
else
{
print_r( $_POST );
}
?>
<?php }
else {
print_r( $_POST );
}
?> // or even a single line of code:
<?php } else { print_r( $_POST ); } ?>
CODE EXPLANATION:
- Notice the method is print_r and not print or echo. They are all used to output data to the screen.
- print_r() is used for printing the array in human readable format.
- echo has no return value so it will returns void; whereas, print will return 1 so it can be used in expressions.
- echo is considered slightly faster than print.
- echo can take multiple arguments (however, this is rare); whereas, print can only take one argument.
- The $_POST super-global variable is passed as an argument to the print_r method.
- Several other predefined variables (e.g., _$GET, $_SERVER, $_FILES, _SESSION, etc.) in PHP are "super-globals",
which means that they are always accessible, regardless of scope - and you can access them from any function,
class or file without having to do anything special.
<?php
}
else
{
echo ("<p>Thank you. You have been registered.</p>
<a href='index.php' class='ui-btn ui-icon-user ui-corner-all ui-btn-icon-left ui-btn-inline ui-mini'>Log In</a>");
}
?>
What we have implemented so far was done with PHP not the database. Now, we need to actual submit the form variables data to the database table. Remember, the associative array keys match the column names in the MySQL table (employees).
<?php
require ('db.php');
?>
<!doctype html>
<?php
}
else
{ $FirstName = $_POST[ 'FirstName' ]; $LastName = $_POST[ 'LastName' ]; $Email = $_POST[ 'Email' ]; $Password = $_POST[ 'Password1' ]; echo ("<p>Thank you. You have been registered.</p>
<a href='index.php' class='ui-btn ui-icon-user ui-corner-all ui-btn-icon-left ui-btn-inline ui-mini'>Log In</a>");
}
?>
CODE EXPLANATION:
- It is important to remember that form variables don't get DEFINED UNTIL AFTER the form is processed by clicking the submit button
(REGISTER). As a result, the variables are placed inside of the "else" portion of the "if/else" conditional statement where the form is
processed.
- Notice that only one password field is set a variable because only one is needed (Password1).
<?php
}
else
{ $FirstName = $_POST[ 'FirstName' ]; $LastName = $_POST[ 'LastName' ]; $Email = $_POST[ 'Email' ]; $Password = $_POST[ 'Password1' ];
$sql = "INSERT INTO passwords ( FirstName, LastName, Email, Password )
VALUES ( :FirstName, :LastName, :Email, :Password )";
echo ("<p>Thank you. You have been registered.</p>
<a href='index.php' class='ui-btn ui-icon-user ui-corner-all ui-btn-icon-left ui-btn-inline ui-mini'>Log In</a>");
}
?>
<?php
}
else
{ $FirstName = $_POST[ 'FirstName' ]; $LastName = $_POST[ 'LastName' ]; $Email = $_POST[ 'Email' ]; $Password = $_POST[ 'Password1' ];
$sql = "INSERT INTO passwords ( FirstName, LastName, Email, Password )
VALUES ( :FirstName, :LastName, :Email, :Password )"; $query = $pdo_conn->prepare($sql);
$query->execute( array( ':FirstName'=>$FirstName, ':LastName'=>$LastName, ':Email'=>$Email, ':Password'=>$Password ) );
echo ("<p>Thank you. You have been registered.</p>
<a href='index.php' class='ui-btn ui-icon-user ui-corner-all ui-btn-icon-left ui-btn-inline ui-mini'>Log In</a>");
}
?>
$query = $pdo_conn->prepare( $sql );
$result = $query->execute( array( ':FirstName'=>$FirstName, ':LastName'=>$LastName, ':Email'=>$Email, ':Password'=>$Password ) );
if($result) { echo ("<p>Thank you. You have been registered.</p> <a href='index.php' class='ui-btn ui-icon-user ui-corner-all ui-btn-icon-left ui-btn-inline ui-mini'>Log In</a>"); } else { echo "<p>Sorry, there has been a problem registering. Please contact admin.</p>"; } } ?>
For both usability and security reasons, it is best practice to validate form elements.
It is best practice to implement a sticky form fields on a registration form so that if a user submit the form, the data is not lost if the form is not submitted. See the Enhancement tab for details.
It is best practice to let the user know that his or her registration was successful. One way of doing this is to send a confirmation e-mail to the user. See the Enhancement tab for details.
It is important to check to ensure that two users do not have the same username. See the Enhancement tab for details.