13.09.2016 Views

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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

700 Chapter 30 Building a Mailing List Manager<br />

You can see the four segments of the code clearly marked in this listing. In the preprocessing<br />

stage, you set up the session <strong>and</strong> process any actions that need to be done before<br />

headers can be sent. In this case, they include logging in <strong>and</strong> out.<br />

In the header stage, you set up the menu buttons that the user will see <strong>and</strong> display the<br />

appropriate headers using the do_html_header() function from output_fns.php.This<br />

function just displays the header bar <strong>and</strong> menus, so we don’t discuss it in detail here.<br />

In the main section of the script, you respond to the action the user has chosen.<br />

These actions are divided into three subsets: actions that can be taken if not logged in,<br />

actions that can be taken by normal users, <strong>and</strong> actions that can be taken by administrative<br />

users.You check to see whether access to the latter two sets of actions is allowed by<br />

using the check_logged_in() <strong>and</strong> check_admin_user() functions.These functions are<br />

located in the user_auth_fns.php function library.The code for these functions <strong>and</strong> the<br />

check_normal_user() function are shown in Listing 30.3.<br />

Listing 30.3 Functions from user_auth_fns.php—These Functions Check Whether<br />

a User Is Logged In <strong>and</strong> at What Level<br />

function check_normal_user() {<br />

// see if somebody is logged in <strong>and</strong> notify them if not<br />

if (isset($_SESSION['normal_user'])) {<br />

return true;<br />

} else {<br />

return false;<br />

}<br />

}<br />

function check_admin_user() {<br />

// see if somebody is logged in <strong>and</strong> notify them if not<br />

if (isset($_SESSION['admin_user'])) {<br />

return true;<br />

} else {<br />

return false;<br />

}<br />

}<br />

function check_logged_in() {<br />

return ( check_normal_user() || check_admin_user() );<br />

As you can see, these functions use the session variables normal_user <strong>and</strong> admin_user<br />

to check whether a user has logged in.We explain how to set up these session variables<br />

shortly.

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

Saved successfully!

Ooh no, something went wrong!