LIGHTENING YOUR WORKLOAD WITH INCLUDES
71
Introducing the PHP include commands
PHP has four commands that can be used to include code from an external file, namely:
include()
include_once()
require()
require_once()
They all do basically the same thing, so why have four?
The fundamental difference is that include() attempts to continue processing a script, even if the
external file is missing, whereas require() is used in the sense of mandatory: if the file is missing, the
PHP engine stops processing and throws a fatal error. In practical terms, this means you should use
include() if your page would remain usable even without the contents of the external file. Use
require() if the page depends on the external file.
The other two commands, include_once() and require_once(), work the same way, but they prevent
the same file from being included more than once in a page. This is particularly important when including
files that define functions or classes. Attempting to define a function or class more than once in a script
triggers a fatal error. So, using include_once() or require_once() ensures that functions and classes
are defined only once, even if the script tries to include the external file more than once, as might happen
if the commands are in conditional statements.
So, which should you use? I recommend using include() for external files that aren't mission critical, and
require_once() for files that define functions and classes.
Where PHP looks for include files
To include an external file, you pass the file path to one of the four include commands as a string—in other
words, the file path must be in quotes (single or double, it doesnt matter). The file path can be either
absolute or relative to the current document. For example, any of the following will work (as long as the
target file exists):
include('includes/menu.inc.php');
include('C:/xampp/htdocs/phpsols/includes/menu.inc.php');
include('/Applications/MAMP/htdocs/phpsols/includes/menu.inc.php');
PHP accepts forward slashes in Windows file paths.
Using parentheses with the include commands is optional, so the following would also work:
include 'includes/menu.inc.php';
include 'C:/xampp/htdocs/phpsols/includes/menu.inc.php';
include '/Applications/MAMP/htdocs/phpsols/includes/menu.inc.php';
When using a relative file path, its recommended to use ./ to indicate that the path begins in the current
folder. So, its more efficient to rewrite the first example like this:
include('./includes/menu.inc.php'); // path begins in current folder
CHAPTER 4
72
What doesnt work is using a file path relative to the site root like this:
include('/includes/menu.inc.php'); // THIS WILL NOT WORK
If PHP cant find the file, it also looks in the include_path, as defined in your PHP configuration. Ill return
to this subject later in this chapter. Before that, lets put PHP includes to practical use. For the time being,
I recommend you use file paths relative to the current document.
PHP Solution 4-1: Moving the menu and footer to include files
Lets convert the page shown in Figure 4-1 to use include files. Because the menu and footer appear on
every page of the Japan Journey site, theyre prime candidates for include files. Heres the code for the
body of the page with the menu and footer highlighted in bold.
Listing 4-1. The static version of index.php
<body>
<div id="header">
<h1>Japan Journey</h1>
</div>
<div id="wrapper">
<ul id="nav">
<li><a href="index.php" id="here">Home</a></li>
<li><a href="blog.php">Journal</a></li>
<li><a href="gallery.php">Gallery</a></li>
<li><a href="contact.php">Contact</a></li>
</ul>
<div id="maincontent">
<h2>A journey through Japan with PHP</h2>
<p>One of the benefits of using PHP . . .</p>
<div id="pictureWrapper">
<img src="images/water_basin.jpg" alt="Water basin at Ryoanji temple"
width="350" height="237" class="picBorder">
</div>
<p>Ut enim ad minim veniam, quis nostrud . . .</p>
<p>Eu fugiat nulla pariatur. Ut labore et dolore . . .</p>
<p>Sed do eiusmod tempor incididunt ullamco . . .</p>
<p>Quis nostrud exercitation eu fugiat nulla . . .</p>
</div>
<div id="footer">
<p>&copy; 2006&8211;2010 David Powers</p>
</div>
</div>
</body>
1.Copy index_01.php from the ch04 folder to the phpsols site root, and rename it index.php.
If you are using a program like Dreamweaver that offers to update the page links, dont update
them. The relative links in the download file are correct. Check that the CSS and images are
displaying properly by loading index.php into a browser. It should look the same as Figure 4-
1.
s
Download from Wow! eBook <www.wowebook.com>
LIGHTENING YOUR WORKLOAD WITH INCLUDES
73
2. Copy blog.php, gallery.php, and contact.php from the ch04 folder to your site root folder.
These pages wont display correctly in a browser yet because the necessary include files still
havent been created. Thatll soon change.
3. In index.php, highlight the nav unordered list as shown in bold in Listing 4-1, and cut
(Ctrl+X/Cmd+X) it to your computer clipboard.
4. Create a new file called menu.inc.php in the includes folder. Remove any code inserted by
your editing program; the file must be completely blank.
5. Paste (Ctrl+V/Cmd+V) the code from your clipboard into menu.inc.php and save the file. The
contents of menu.inc.php should look like this:
<ul id="nav">
<li><a href="index.php" id="here">Home</a></li>
<li><a href="blog.php">Journal</a></li>
<li><a href="gallery.php">Gallery</a></li>
<li><a href="contact.php">Contact</a></li>
</ul>
6. Dont worry that your new file doesnt have a DOCTYPE declaration or any <html>, <head>, or
<body> tags. The other pages that include the contents of this file will supply those elements.
7. Open index.php, and insert the following in the space left by the nav unordered list:
<?php include('./includes/menu.inc.php'); ?>
This uses a document-relative path to menu.inc.php. The ./ at the beginning of the path
indicates explicitly that the path starts in the current folder and is more efficient.
8. Save index.php, and load the page into a browser. It should look exactly the same as before.
Although the menu and the rest of the page are coming from different files, PHP merges them
before sending any output to the browser.
Dont forget that PHP code needs to be processed by a web server. If you have stored your files in a
subfolder of your servers document root called
phpsols
, you should access
index.php
using the
URL
http://localhost/phpsols/index.php
. See “Where to locate your PHP files” in Chapter 2 if
you need help finding the servers document root.
9. Do the same with the footer <div>. Cut the lines highlighted in bold in Listing 4-1, and paste
them into a blank file called footer.inc.php in the includes folder. Then insert the command
to include the new file in the gap left by the footer <div>:
<?php include('./includes/footer.inc.php'); ?>
10. Save all pages, and reload index.php in your browser. Again, it should look identical to the
original page. If you navigate to other pages in the site, the menu and footer should appear on
every page. The code in the include files is now serving all pages.
CHAPTER 4
74
11. To prove that the menu is being drawn from a single file, change the text in the Journal link in
menu.inc.php like this:
<li><a href="blog.php">Blog</a></li>
12. Save menu.inc.php and reload the site. The change is reflected on all pages. You can check
your code against index_02.php, menu.inc_01.php, and footer.inc_01.php in the ch04
folder.
As Figure 4-2 shows, theres a problem with the code at the moment. Even when you navigate away from
the home page, the style that indicates which page youre on doesnt change (its controlled by the here
ID in the <a> tag).
Figure 4-2. The current page indicator still points to the Home page.
Fortunately, thats easily fixed with a little PHP conditional logic. Before doing so, lets take a look at how
the web server and the PHP engine handle include files.
Choosing the right filename extension for includes
As you have just seen, an include file can contain raw HTML. When the PHP engine encounters an include
command, it stops processing PHP at the beginning of the external file and resumes again at the end. If
you want the external file to use PHP code, the code must be enclosed in PHP tags. As a consequence of
this behavior, an include file can have any filename extension.
A common convention is to use .inc as the filename extension to make it clear that the file is intended to
be included in another file. However, most servers treat .inc files as plain text. This poses a security risk
if the file contains sensitive information, such as the username and password to your database. If the file
is stored within your websites root folder, anyone who discovers the name of the file can simply type the
URL in a browser address bar, and the browser will obligingly display all your secret details!
On the other hand, any file with a .php extension is automatically sent to the PHP engine for parsing
before its sent to the browser. So, as long as your secret information is inside a PHP code block and in a
file with a .php extension, it wont be exposed. Thats why many developers use .inc.php as a double
extension for PHP includes. The .inc part reminds you that its an include file, but servers are only
interested in the .php on the end, which ensures that all PHP code is correctly parsed.
Since its common practice to store include files in a separate folder—often called includesyou could
argue that .inc.php is superfluous. Which naming convention you choose is up to you, but using .inc on
its own is the least secure.
LIGHTENING YOUR WORKLOAD WITH INCLUDES
75
PHP Solution 4-2: Testing the security of includes
This solution demonstrates the difference between using .inc and .inc.php as the filename extension
for an include file. Use index.php and menu.inc.php from the previous section. Alternatively, use
index_02.php and menu.inc_01.php from the ch04 folder. If you use the download files, remove the _02
and _01 from the filenames before using them.
1. Rename menu.inc.php to menu.inc, and edit index.php accordingly to include it:
<?php include('./includes/menu.inc'); ?>
2. Load index.php into a browser. You should see no difference.
3. Amend the code inside menu.inc to store a password inside a PHP variable like this:
<ul id="nav">
<li><a href="index.php" id="here">Home</a></li>
<?php $password = 'topSecret'; ?>
<li><a href="blog.php">Blog</a></li>
<li><a href="gallery.php">Gallery</a></li>
<li><a href="contact.php">Contact</a></li>
</ul>
4. Reload the page. As Figure 4-3 shows, the password remains hidden in the source code.
Although the include file doesnt have a .php filename extension, its contents have been
merged with index.php, so the PHP code is processed.
Figure 4-3. Theres no output from the PHP code, so only the HTML is sent to the browser.
5. Now load menu.inc directly in the browser. Figure 4-4 shows what happens.