13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Implementing User Authentication<br />

577<br />

As you can see, the only logic in the do_html_header() function is to add the<br />

appropriate title <strong>and</strong> heading to the page.The other functions used in login.php are<br />

similar.The function display_site_info() adds some general text about the site,<br />

display_login_form() displays the gray form shown in Figure 27.3, <strong>and</strong><br />

do_html_footer() adds a st<strong>and</strong>ard HTML footer to the page.<br />

The advantages to isolating or removing HTML from your main logic stream are discussed<br />

in Chapter 25,“Using <strong>PHP</strong> <strong>and</strong> <strong>MySQL</strong> for Large Projects.”We use the function<br />

API approach here.<br />

Looking at Figure 27.3, you can see that this page has three options: A user can register,<br />

log in if she has already registered, or reset her password if she has forgotten it.To<br />

implement these modules, we move on to the next section, user authentication.<br />

Implementing User Authentication<br />

There are four main elements to the user authentication module: registering users, logging<br />

in <strong>and</strong> logging out, changing passwords, <strong>and</strong> resetting passwords. In the following<br />

sections, we look at each of these elements in turn.<br />

Registering Users<br />

To register a user, you need to get his details via a form <strong>and</strong> enter him in the database.<br />

When a user clicks on the Not a member? link on the login.php page, he is taken to<br />

a registration form produced by register_form.php.This script is shown in Listing 27.5.<br />

Listing 27.5 register_form.php—This Form Gives Users the Opportunity to<br />

Register with <strong>PHP</strong>bookmark<br />

<br />

Again, you can see that this page is fairly simple <strong>and</strong> just calls functions from the output<br />

library in output_fns.php.The output of this script is shown in Figure 27.4.<br />

The gray form on this page is output by the function<br />

display_registration_form(), contained in output_fns.php.When the user clicks on<br />

the Register button, he is taken to the script register_new.php, shown in Listing 27.6.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!