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 />

585<br />

Listing 27.11<br />

display_user_menu();<br />

do_html_footer();<br />

?><br />

Continued<br />

You might recognize the logic in the member.php script: It reuses some of the ideas from<br />

Chapter 23.<br />

First, you check whether the user has come from the front page—that is, whether he<br />

has just filled in the login form—<strong>and</strong> try to log him in as follows:<br />

if ($username && $passwd) {<br />

// they have just tried logging in<br />

try {<br />

login($username, $passwd);<br />

// if they are in the database register the user id<br />

$_SESSION['valid_user'] = $username;<br />

}<br />

You try to log the user in by using a function called login(). It is defined in the<br />

user_auth_fns.php library, <strong>and</strong> we look at the code for it shortly.<br />

If the user is logged in successfully, you register his session as you did before, storing<br />

the username in the session variable valid_user.<br />

If all goes well, you then show the user the members page:<br />

do_html_header('Home');<br />

check_valid_user();<br />

// get the bookmarks this user has saved<br />

if ($url_array = get_user_urls($_SESSION['valid_user'])) {<br />

display_user_urls($url_array);<br />

}<br />

// give menu of options<br />

display_user_menu();<br />

do_html_footer();<br />

This page is again formed using the output functions. Notice that the page uses several<br />

other new functions: check_valid_user() from user_auth_fns.php, get_user_urls()<br />

from url_fns.php, <strong>and</strong> display_user_urls() from output_fns.php.The<br />

check_valid_user() function checks that the current user has a registered session.<br />

This is aimed at users who have not just logged in, but are mid-session.The<br />

get_user_urls() function gets a user’s bookmarks from the database, <strong>and</strong><br />

display_user_urls() outputs the bookmarks to the browser in a table.We look at<br />

check_valid_user() in a moment <strong>and</strong> at the other two in the section on bookmark<br />

storage <strong>and</strong> retrieval.

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

Saved successfully!

Ooh no, something went wrong!