| only for RuBoard - do not distribute or recompile |
When a user loads the page index.php, he will see the output shown in Figure 27.2.
This is the default behavior for the application. With no $action chosen yet, and no login details supplied, we will execute the following parts of the code.
In the preprocessing stage, we execute the following code:
include ('include_fns.php');
session_start();
These lines start the session that will be used to keep track of the $auth_user and $selected_account session variables, which we'll come to later on.
To save work when customizing the user interface, the buttons that appear on the toolbar are controlled by an array. We declare an empty array,
$buttons = array();
and set the buttons that we want on the page:
$buttons[0] = 'view-mailbox'; $buttons[1] = 'new-message'; $buttons[2] = 'account-setup';
For the header stage, we print a plain vanilla header:
do_html_header($auth_user, "Warm Mail", $selected_account); … display_toolbar($buttons);
This code prints the title and header bar, and then the toolbar of buttons you can see in Figure 27.2. These functions can be found in the output_fns.php function library, but as you can easily see their effect in the figure, we won't go through them here.
Now we come to the body of the code:
if(!check_auth_user())
{
echo "<P>You need to log in";
if($action&&$action!='log-out')
echo " to go to ".format_action($action);
echo ".<br><br>";
display_login_form($action);
}
The check_auth_user() function is from the user_auth_fns.php library. We have used very similar code in some of the previous projects—it checks if the user is logged in. If he is not, which is the case here, we will show him a login form, which you can see in Figure 27.2. We draw this form in the display_login_form() function from output_fns.php.
If the user fills in the form correctly and presses the Log In button, he will see the output shown in Figure 27.3.
On this execution of the script, we will activate different sections of code. The login form has two fields, $username and $password. If these have been filled in, the following segment of preprocessing code will be activated:
if($username||$password)
{
if(login($username, $passwd))
{
$status .= "<p>Logged in successfully.<br><br><br><br><br><br>";
$auth_user = $username;
session_register("auth_user");
}
else
{
$status .= "<p>Sorry, we could not log you in with that
username and password.<br><br><br><br><br><br>";
}
}
As you can see, the code calls the login() function, which is similar to the one used in Chapters 24 and 25. If all goes well, we register the username in the session variable $auth_user.
In addition to setting up the buttons we saw while not logged in, we add another button to allow the user to log out again, as follows:
if(check_auth_user())
{
$buttons[4] = 'log-out';
}
You can see this Log Out button in Figure 27.3.
In the header stage, we again display the header and the buttons. In the body, we display the status message we set up earlier:
echo $status;
After that, it's just a case of printing the footer and waiting to see what the user will do next.
| only for RuBoard - do not distribute or recompile |