| only for RuBoard - do not distribute or recompile |
The first page we'll build will be called login.php because it provides users with the opportunity to log in to the system. The code for this first page is shown in Listing 24.2.
<?
require_once("bookmark_fns.php");
do_html_header("");
display_site_info();
display_login_form();
do_html_footer();
?>
This code looks very simple, as it is mostly calling functions from the function API that we will construct for this application. We'll look at the details of these functions in a minute. Just looking at this file, we can see that we are including a file (containing the functions) and then calling some functions to render an HTML header, display some content, and render an HTML footer.
The output from this script is shown in Figure 24.3.

The functions for the system are all included in the file bookmark_fns.php, shown in Listing 24.3.
<?
// We can include this file in all our files
// this way, every file will contain all our functions
require_once("data_valid_fns.php");
require_once("db_fns.php");
require_once("user_auth_fns.php");
require_once("output_fns.php");
require_once("url_fns.php");
?>
As you can see, this file is just a container for the five other include files we will use in this application. We have structured it like this because the functions fall into logical groups. Some of these groups might be useful for other projects, so we put each function group into a different file where we will know where to find them when we want them again. We constructed the bookmark_fns.php file because we will use most of the five function files in most of our scripts. It is easier to include this one file in each script rather than having five include statements.
Note that the require_once() construct only exists in PHP from version 4.0.1pl2. If you are using a prior version, you will need to use require() or include() and ensure that the files do not get loaded multiple times.
In this particular case, we are using functions from the file output_fns.php. These are all straightforward functions that output fairly plain HTML. This file includes the four functions we have used in login.php, that is, do_html_header(), display_site_info(), display_login_form(), and do_html_footer(), among others.
We will not go through all these functions in detail, but we will look at one as an example. The code for do_html_header() is shown in Listing 24.4.
function do_html_header($title)
{
// print an HTML header
?>
<html>
<head>
<title><?=$title?></title>
<style>
body { font-family: Arial, Helvetica, sans-serif; font-size: 13px }
li, td { font-family: Arial, Helvetica, sans-serif; font-size: 13px }
hr { color: #3333cc; width=300; text-align=left}
a { color: #000000 }
</style>
</head>
<body>
<img src="bookmark.gif" alt="PHPbookmark logo" border=0
align=left valign=bottom height = 55 width = 57>
<h1> PHPbookmark</h1>
<hr>
<?
if($title)
do_html_heading($title);
}
As you can see, the only logic in this function is to add the appropriate title and heading to the page. The other functions we have used in login.php are similar. The function display_site_info() adds some general text about the site; display_login_form() displays the grey form shown in Figure 24.3; and do_html_footer() adds a standard HTML footer to the page.
(The advantages to isolating or removing HTML from your main logic stream are discussed in Chapter 22, "Using PHP and MySQL for Large Projects." We will use the function API approach here, and a template-based approach in the next chapter for contrast.)
Looking at Figure 24.3, you can see that there are three options on this page—the user can register, log in if they have already registered, or reset their password if they have forgotten it. To implement these modules we will move on to the next section, user authentication.
| only for RuBoard - do not distribute or recompile |