55
549
Implementing User Authentication
As you can see, the only logic in the
do_html_header()
function is to add the
appropriate title and heading to the page.The other functions used in
login.php
are
similar.The function
display_site_info()
adds some general text about the site,
display_login_form()
displays the gray form shown in Figure 26.3,and
do_html_footer()
adds a standard HTML footer to the page.
The advantages to isolating or removing HTML from your main logic stream are dis-
cussed in Chapter 24,“Using PHP and MySQL for Large Projects.”We use the function
API approach here.
Looking at Figure 26.3,you can see that this page has three options:A user can regis-
ter,log in if she has already registered,or reset her password if she has forgotten it.To
implement these modules,we move on to the next section,user authentication.
Implementing User Authentication
There are four main elements to the user authentication module:registering users,log-
ging in and logging out, changing passwords,and resetting passwords.In the following
sections,we look at each of these elements in turn.
Registering
To register a user, you need to get his details via a form and enter him in the database.
When a user clicks on the
Not a member?
link on the
login.php
page,he is taken to
a registration form produced by
register_form.php
.This script is shown in Listing 26.5.
Listing 26.5 register_form.php—This Form Gives Users the Opportunity to
Register with PHPbookmark
<?php
require_once(‘bookmark_fns.php’);
do_html_header(‘User Registration’);
display_registration_form();
do_html_footer();
?>
Again, you can see that this page is fairly simple and just calls functions from the output
library in
output_fns.php
.The output of this script is shown in Figure 26.4.
The gray form on this page is output by the function
display_registration_form()
,contained in
output_fns.php
.When the user clicks on
the Register button,he is taken to the script
register_new.php
,shown in Listing 26.6.