122 Chapter 5 Reusing Code and Writing Functions p {color:black; font-size:12pt; text-align:justify; font-family:arial,sans-serif} p.foot {color:white; font-size:9pt; text-align:center; font-family:arial,sans-serif; font-weight:bold} a:link,a:visited,a:active {color:white} </style> </head> <body> <! page header > <table width="100%" cellpadding="12" cellspacing="0" border="0"> <tr bgcolor="black"> <td align="left"><img src="logo.gif"></td> <td> <h1>TLA Consulting</h1> </td> <td align="right"><img src="logo.gif"></td> </tr> </table> <! menu > <table width="100%" bgcolor="white" cellpadding="4" cellspacing="4"> <tr > <td width="25%"> <img src="s-logo.gif"> <span class="menu">Home</span></td> <td width="25%"> <img src="s-logo.gif"> <span class="menu">Contact</span></td> <td width="25%"> <img src="s-logo.gif"> <span class="menu">Services</span></td> <td width="25%"> <img src="s-logo.gif"> <span class="menu">Site Map</span></td> </tr> </table> <! page content > <p>Welcome to the home of TLA Consulting. Please take some time to get to know us.</p> <p>We specialize in serving your business needs and hope to hear from you soon.</p> <! page footer > <table width="100%" bgcolor="black" cellpadding="12" border="0"> <tr> <td> <p class="foot">© TLA Consulting Pty Ltd.</p> Listing 5.1 Continued 07 525x ch05 1/24/03 3:36 PM Page 122 123 Using require() for Web Site Templates <p class="foot">Please see our <a href="legal.php">legal information page</a></p> </td> </tr> </table> </body> </html> You can see in Listing 5.1 that a number of distinct sections of code exist in this file.The HTML head contains Cascading Style Sheet (CSS) definitions used by the page.The section labeled “page header” displays the company name and logo,“menu bar” creates the page’s navigation bar, and “page content” is text unique to this page. Below that is the page footer.We can usefully split this file and name the parts header.inc, home.php, and footer.inc. Both header.inc and footer.inc contain code that will be reused on other pages. The file home.php is a replacement for home.html, and contains the unique page content and two require() statements as shown in Listing 5.2. Listing 5.2 home.php—The PHP That Produces TLA’s Home Page <?php require('header.inc'); ?> <! page content > <p>Welcome to the home of TLA Consulting. Please take some time to get to know us.</p> <p>We specialize in serving your business needs and hope to hear from you soon.</p> <?php require('footer.inc'); ?> The require() statements in home.php load header.inc and footer.inc. As mentioned, the name given to these files does not affect how they are processed when we call them via require().A common, but entirely optional, convention is to call the partial files that will end up included in other files something.inc (here inc stands for include). It is also common, and a good idea, to place your include files in a directory that can be seen by your scripts, but does not permit your include files to be loaded individually via the Web server.This will prevent these files from being loaded individually which will either a) probably produce some errors if the file extension is .php but contains only a partial page or script, or b) allow people to read your source code if you have used another extension. The file header.inc contains the CSS definitions that the page uses, the tables that display the company name and navigation menus as shown in Listing 5.3. Listing 5.1 Continued 07 525x ch05 1/24/03 3:36 PM Page 123 124 Chapter 5 Reusing Code and Writing Functions The file footer.inc contains the table that displays the footer at the bottom of each page.This file is shown in Listing 5.4. Listing 5.3 header.inc—The Reusable Header for All TLA Web Pages <html> <head> <title>TLA Consulting Pty Ltd</title> <style> h1 {color:white; font-size:24pt; text-align:center; font-family:arial,sans-serif} .menu {color:white; font-size:12pt; text-align:center; font-family:arial,sans-serif; font-weight:bold} td {background:black} p {color:black; font-size:12pt; text-align:justify; font-family:arial,sans-serif} p.foot {color:white; font-size:9pt; text-align:center; font-family:arial,sans-serif; font-weight:bold} a:link,a:visited,a:active {color:white} </style> </head> <body> <! page header > <table width="100%" cellpadding="12" cellspacing="0" border="0"> <tr bgcolor="black"> <td align="left"><img src="logo.gif"></td> <td> <h1>TLA Consulting</h1> </td> <td align="right"><img src="logo.gif"></td> </tr> </table> <! menu > <table width="100%" bgcolor="white" cellpadding="4" cellspacing="4"> <tr > <td width="25%"> <img src="s-logo.gif"> <span class="menu">Home</span></td> <td width="25%"> <img src="s-logo.gif"> <span class="menu">Contact</span></td> <td width="25%"> <img src="s-logo.gif"> <span class="menu">Services</span></td> <td width="25%"> <img src="s-logo.gif"> <span class="menu">Site Map</span></td> </tr> </table> 07 525x ch05 1/24/03 3:36 PM Page 124 125 Using require() for Web Site Templates Listing 5.4 footer.inc—The Reusable Footer for All TLA Web Pages <! page footer > <table width="100%" bgcolor="black" cellpadding="12" border="0"> <tr> <td> <p class="foot">© TLA Consulting Pty Ltd.</p> <p class="foot">Please see our <a href="legal.php">legal information page</a></p> </td> </tr> </table> </body> </html> This approach gives you a consistent looking Web site very easily, and you can make a new page in the same style by typing something like: <?php require('header.inc'); ?> Here is the content for this page <?php require('footer.inc'); ?> Most importantly, even after we have created many pages using this header and footer, it is easy to change the header and footer files.Whether you are making a minor text change, or completely redesigning the look of the site, you only need to make the change once.We do not need to separately alter every page in the site because each page is loading in the header and footer files. The example shown here only uses plain HTML in the body, header and footer.This need not be the case.Within these files, we could use PHP statements to dynamically generate parts of the page. Using auto_prepend_file and auto_append_file If we want to use require() to add our header and footer to every page, there is anoth- er way we can do it.Two of the configuration options in the php.ini file are auto_prepend_file and auto_append_file. By setting these to our header and footer files, we ensure that they will be loaded before and after every page. For Windows, the settings will resemble the following: auto_prepend_file = "c:/inetpub/include/header.inc" auto_append_file = "c:/inetpub/include/footer.inc" For UNIX, they will resemble the following: auto_prepend_file = "/home/username/include/header.inc" auto_append_file = "/home/username/include/footer.inc" If we use these directives, we do not need to type require() statements, but the headers and footers will no longer be optional on pages. 07 525x ch05 1/24/03 3:36 PM Page 125 126 Chapter 5 Reusing Code and Writing Functions If you are using an Apache Web server, you can change various configuration options like these for individual directories.To do this, your server must be set up to allow its main configuration file(s) to be overridden.To set up auto prepending and appending for a directory, create a file called .htaccess in the directory.The file needs to contain the following two lines: php_value auto_prepend_file "/home/username/include/header.inc" php_value auto_append_file "/home/username/include/footer.inc" Note that the syntax is slightly different from the same option in php.ini, as well as php_value at the start of the line:There is no equal sign. A number of other php.ini configuration settings can be altered in this way too. This syntax changed from PHP 3. If you are using an old version, the lines in your .htaccess file should resemble this: php3_auto_prepend_file /home/username/include/header.inc php3_auto_append_file /home/username/include/footer.inc Setting options in the .htaccess file rather than in either php.ini or your Web server’s configuration file gives you a lot of flexibility.You can alter settings on a shared machine that only affect your directories.You do not need to restart the Web server, and you do not need administrator access. A drawback to the .htaccess method is that the files are read and parsed each time a file in that directory is requested rather than just once at startup, so there is a performance penalty. Using include() The statements require() and include() are almost identical.The only difference between them is that when they fail, the require() construct will give a fatal error, while the include() construct will only give a warning. This is a change in the way these constructs work. Prior to PHP 4.0.2, they had some important differences in the way they worked. If you are still using an older ver- sion of PHP, the following notes will apply to you: An include() statement is evaluated each time the statement is executed, and not evaluated at all if the statement is not executed.A require() statement is executed the first time the statement is parsed, regardless of whether the code block containing it will be executed. Unless your server is very busy, this will make little difference but it does mean that code with require() statements inside conditional statements is inefficient. if($variable == true) { require('file1.inc'); } else { 07 525x ch05 1/24/03 3:36 PM Page 126 . Code and Writing Functions p {color:black; font-size:12pt; text-align:justify; font-family:arial,sans-serif} p.foot {color:white; font-size:9pt; text-align:center; font-family:arial,sans-serif;. {color:white; font-size:12pt; text-align:center; font-family:arial,sans-serif; font-weight:bold} td {background:black} p {color:black; font-size:12pt; text-align:justify; font-family:arial,sans-serif} p.foot. home .php, and footer.inc. Both header.inc and footer.inc contain code that will be reused on other pages. The file home .php is a replacement for home.html, and contains the unique page content and