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.

664 Chapter 29 Building a <strong>Web</strong>-Based Email Service<br />

Showing the login screen is the default behavior for the application.With no $action<br />

chosen yet, <strong>and</strong> no login details supplied, <strong>PHP</strong> will then execute the following parts of<br />

the code.<br />

In the preprocessing stage, <strong>PHP</strong> first executes the following code:<br />

include ('include_fns.php');<br />

session_start();<br />

These lines start the session that will be used to keep track of the $auth_user <strong>and</strong><br />

$selected_account session variables, which we come to later.<br />

As in the other applications, you create short variable names.You have done this in<br />

every form-related script since Chapter 1,“<strong>PHP</strong> Crash Course,” so it barely needs mention<br />

except for the variable action. Depending on where in the application this variable<br />

comes from, it might be either a GET or POST variable.You therefore extract it from the<br />

$_REQUEST array.You have to do the same thing with the account variable because it is<br />

usually accessed via GET but is accessed via POST when deleting an account.<br />

To save work when customizing the user interface, you use an array to control the<br />

buttons that appear on the toolbar.You declare an empty array as follows:<br />

$buttons = array();<br />

Then you set the buttons that you want on the page:<br />

$buttons[0] = 'view-mailbox';<br />

$buttons[1] = 'new-message';<br />

$buttons[2] = 'account-setup';<br />

If the user later logs in as an administrator, you will add more buttons to this array.<br />

For the header stage, you print a plain vanilla header:<br />

do_html_header($_SESSION['auth_user'], "Warm Mail",<br />

$_SESSION['selected_account']);<br />

...<br />

display_toolbar($buttons);<br />

This code prints the title <strong>and</strong> header bar <strong>and</strong> then the toolbar of buttons you can see in<br />

Figure 29.2.These functions are located in the output_fns.php function library, but<br />

because you can easily see their effect in the figure, we don’t go through them here.<br />

Next comes the body of the code:<br />

if(!check_auth_user()) {<br />

echo "You need to log in";<br />

if(($action) && ($action!='log-out')) {<br />

echo " to go to ".format_action($action);<br />

}<br />

echo ".";<br />

display_login_form($action);<br />

}

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

Saved successfully!

Ooh no, something went wrong!