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 user enter usernames and passwords for users (e.g., visitors), you can allow them 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.
The code that will be created will perform the following tasks:
<?php require("includes/header.inc.php")?>
<div data-role="content">
<h2 class="ui-body ui-body-a ui-corner-all">EMPLOYEE REGISTRATION</h2>
<!-- The form is created OUTSIDE of the conditional statement so that it will ALWAYS be displayed. -->
<div class="ui-body ui-body-a ui-corner-all">
<form action="employee_registration.php" method="post">
<p>First Name: <input type="text" name="first_name" size="20" data-mini="true"/></p>
<p>Last Name: <input type="text" name="last_name" size="20" data-mini="true"/></p>
<p>Email Address: <input type="text" name="email" size="20" data-mini="true"/></p>
<p>Password: <input type="password" name="password1" size="20"data-mini="true"/></p>
<p>Re-enter Password: <input type="password" name="password2" size="20" data-mini="true"/></p>
<p><input type="submit" name="submit" value="REGISTER" data-inline="true" data-mini="true"/></p>
<input type="hidden" name="submitted" value="true" />
</form>
</div>
</div>
<?php require("includes/footer.inc.php")?>
A Sticky Form is a form that remembers the values that were entered into it if the form is not submitted correctly so that a user does not have to re-enter those values. The syntax for a form variable to to sticky is:
<input type="text" name="first_name" size="20" value="<?php if (isset($_POST["first_name"])) {print htmlspecialchars($_POST["first_name"]); } ?>"
The value attribute of a form variable is set to a PHP script that check to see if the variable is set and if so it will display the result in the form element after parsing it with the htmlspecialchars method. The htmlspecialchars method converts certain HTML tags into entities (e.g., <h1> entity is <h1>) to avoid problems if a user enters HTML tags into a form field.
<?php require("includes/header.inc.php")?>
<div data-role="content">
<h2 class="ui-body ui-body-a ui-corner-all">EMPLOYEE REGISTRATION</h2>
<!-- The form is created OUTSIDE of the conditional statement so that it will ALWAYS be displayed. -->
<div class="ui-body ui-body-a ui-corner-all">
<form action="employee_registration.php" method="post">
<p>First Name: <input type="text" name="first_name" size="20" data-mini="true"
value="<?php if (isset($_POST["first_name"])) {print htmlspecialchars($_POST["first_name"]);} ?>"/></p> <p>Last Name: <input type="text" name="last_name" size="20" data-mini="true"
value="<?php if (isset($_POST["last_name"])) {print htmlspecialchars($_POST["last_name"]);} ?>"/></p>
<p>Email Address: <input type="text" name="email" size="20" data-mini="true"
value="<?php if (isset($_POST["email"])) {print htmlspecialchars($_POST["email"]);} ?>"/></p>
<p>Password: <input type="password" name="password1" size="20" data-mini="true"/></p>
<p>Re-enter Password: <input type="password" name="password2" size="20" data-mini="true"/></p>
<p><input type="submit" name="submit" value="REGISTER" data-inline="true" data-mini="true"/></p>
<input type="hidden" name="submitted" value="true" />
</form>
</div>
</div>
<?php require("includes/footer.inc.php")?>
Anytime you create a form, you should validate the form elements using client-side JavaScript, server-side script or both. When creating a registration page, you want to ensure that all critical form elements get a value or that they are the right data type. This is especially true of text input fields.
<h2 class="ui-body ui-body-a ui-corner-all">EMPLOYEE REGISTRATION</h2> <?php print "<p>Register for benefits</p>"; // Check to see if the form has been submitted: if (isset($_POST["submitted"]) ) { $missingField = FALSE; // Set $missingField FLAG to TRUE if "if" statement is TRUE. // Check each form variable to see if any one is empty if (empty($_POST["first_name"]) && isset($_POST["first_name"])) { $missingField = TRUE; print '<p class="error">Please enter your first name!</p>'; } } ?> <!-- The form is created OUTSIDE of the conditional statement so that it will ALWAYS be displayed. -->
<h2 class="ui-body ui-body-a ui-corner-all">EMPLOYEE REGISTRATION</h2> <?php print "<p>Register for benefits</p>"; // Check to see if the form has been submitted: if (isset($_POST["submitted"]) ) { $missingField = FALSE; // Set $missingField FLAG to TRUE if "if" statement is TRUE. // Check each form variable to see if any one is empty if (empty($_POST["first_name"]) && isset($_POST["first_name"])) { $missingField = TRUE; print '<p class="error">Please enter your first name!</p>'; } if (empty($_POST["last_name"])) { $missingField = TRUE; print '<p class="error">Please enter your last name!</p>'; } if (empty($_POST["email"])) { $missingField = TRUE; print '<p class="error">Please enter your email!</p>'; } } ?> <!-- The form is created OUTSIDE of the conditional statement so that it will ALWAYS be displayed. -->
if (empty($_POST["email"]) && isset($_POST["email"]))
{ $missingField = TRUE; print '<p class="error">Please enter your email address!</p>'; }
if (empty($_POST["password1"]))
{ $missingField = TRUE; print '<p class="error">Please enter a password!</p>'; }
if ($_POST["password1"] != $_POST["password2"]) { $missingField = TRUE; print '<p class="error">Your password did not match your confirmed password!</p>'; }
if (!$missingField) { print "<p>You are now registered!<br />Okay, you are not really registered but...</p>"; // Clear posted values by emptying the $_POST array $_POST = array(); }
} ?>
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.
As we have already seen, normally, any HTML or PHP print() statement outside the PHP script is immediately sent to the browser. However, there are several methods (e.g., header(), setcookies()) that can only be called if nothing (including HTML or a blank space) has been sent to the browser; otherwise, a header already sent error message will be displayed. To avoid this error, you can implement what is called output buffering.
With output buffering any HTML or PHP print() statement will be placed in a memory buffer and then it can be sent to the browser or cleared without being sent to the browser if needed.
To begin output buffering, add a ob_start() method to the top of the script and an ob_end_flush() method at the end of the script to send the content of the buffer to the browser. You can also use ob_end_clean() method to delete the content of the buffer without sending it to the browser.
We will add the first two methods in the header.inc.php and footer.inc.php pages, respectively, so that is can be used by all pages within the site or app.
{ // Correct email and password! print "<p>You are logged in!</p>"; header("Location: index.php"); }- The highlighted code is used to redirect a user to another page. (In this case, index.php).
<?php ob_start();?> <!doctype html> <html>
</html> <?php ob_end_flush();?>- PHP automatically executes the ob_end_flush method at the end of a script if it does not exist; however, it is still good idea to use it.