Quick Tips

Ever work with 10... 15... 20+ application windows open at once?  Ever wanted to get straight to your desktop?  Just use your Windows key+D - yup, that funny Windows key between Ctrl and Alt.
 
Home arrow Code & Tutorials arrow Tutorials arrow PHP Includes Tutorial
PHP Includes Tutorial PDF Print
PHP Includes - Phenomenal Cosmic Power... Itty Bitty Living Space...

Here’s an example to think about...
Say you have a site with 500 pages, all with the same header and the same navigation somewhere on the page. You want to make a change to your header. You have a few good options:
1) Do an extended search and replace with the hopes that it will ONLY replace what you want.

2) Edit each page manually after consuming mass quantities of your favorite caffeine based stimulant.

3) Find a good designer that will do it for you because you’re independently wealthy.

4) Get out of the web design business because it’s too hard.
And by the way, there was a typo in your code after doing the update, so you have to do it all again with the right code...  When you're done with updating this site, don't forget the site with 5,000 pages that needs the same type of update...

Is there an easier way to do this? You’d better believe it.

The ability to include the contents of one document in another document is something that can make site maintenance much easier. The concept is simple - have one set of code to maintain rather than the same code all over the place. Then, ‘include’ that one piece of code everywhere you need it rather than having the code duplicated all over the place. Includes have been available for a while, even with basic HTML on Apache servers, but PHP (and other languages) give them more power.

Consider what you have on the pages of your site(s). You likely have some items that are common to just about every page. Some kind of header graphic and basic navigation somewhere as well as a footer with links at the bottom along with a copyright notice and what have you. It makes no sense at all to maintain the same information on every single page on your site.

What does make sense is to have each of those pieces of a page in their own files. This way, when you update that one file and upload it to your server, your entire site changes all at once. It’s almost like what CSS does for your site with fonts, colors, etc... but with content.

It might be easier for some people to understand with a simple example. This example has five files - kind of a pain for a one page site, but expand the concept to a site with 10... 50... 100... pages or more and I hope you can see the advantage of it.

First look at the example page and view source to see the code. Looks pretty normal, right? Nothing fancy here once the content is delivered to your browser. Now, reload the page a few times - this one happens to have some dynamic content. Notice that when you view source, it still looks like normal static content.

Now, here is the actual code used for that one displayed page - from four different files.

sample1.php - main page that includes everything else

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
    <title>PHP Include Sample Page</title>
    <link rel="STYLESHEET" type="text/css" href="/css/qtm.css">
</head>

<body style="width:750px; padding-left:25px; padding-top:10px; ">
<div align="center"><? include("./header.html"); ?></div>

<table border="2" width="100%">
<tr>
    <td width="20%" valign="top" class="font_8pt">
        <? include("./nav_links.html"); ?>
    </td>
    <td valign="top"><br><br><p><strong>Joke of the Moment</strong></p>
        <p><? include("http://www.adultjokeratingmachine.com/webjoke.html?wmconnID=1385"); ?></p><br><br>
    </td>
</tr>
<tr>
    <td colspan="2" align="center" class="footer"><? include("./footer.html"); ?></td>
</tr>
</table>

</body>
</html>
header.html - Currently a one line file, but you can have anything you need here.

<h1>PHP Includes Tutorial Sample</h1>

footer.html - happens to include some PHP code to make copyrights easier...

Site Map | links | Other Stuff

<?php
$today = getdate();
$year = $today['year'];
print("<div class='footer_copyright'>© www.".$_SERVER['SERVER_NAME']." - 2003 - ".$year."</div>");
?>

nav_links.html - Ok, so there is only one link here, but you get the idea...

Home<br>
<a href="<?= $_SERVER['PHP_SELF']; ?>" target="_self">Reload Page</a><br>
Gallery<br>
Stuff<br>
More Stuff<br>

Other than the PHP include() syntax, it should all look pretty normal to just about anybody familiar with HTML. The PHP include is where things start to get fun. The included file can have anything you want/need in it. The one point that you MUST remember is that the included file will have the scope of the page it’s getting pulled into. This means that if you have variables else where on that page, the included code will know about them and be able to use them; regardless of what directory (or site) you pull the include from, the included code will be part of the main document so things like file paths will be relative to the main document. It also means that any CSS in effect on the main page is also in effect for the content that is included. Some of this may take a little to wrap your head around, but when you do, you’ll see the power of this.

One of the most powerful PHP include features is that you are not limited to including a document from your own domain. Look at the code for sample1.php and you’ll see this line:

<? include("http://www.adultjokeratingmachine.com/webjoke.html?wmconnID=1385"); ?>
This include is pulling content from an entirely different domain. Just be sure that you know what you are connecting to since PHP has the power to read file systems and alter them.

The final feature I’ll point out is the ability to use an include in an included document. With this, you can cascade includes if you need to.

There are basic page elements that lend themselves to being an include - most obviously your header, footer and common page navigation. For the rest of any page, my approach is to build the page and look for what might be cluttering up working with the page later. Something like a huge block of PHP programming is a good possibility for moving over to an include - just to make the rest of the page easier to look at.

You can also use includes for security by moving the include to a directory that is outside your sites web context, but that is for another tutorial later.

Just as a quick note for includes, it's good to have a naming convention for them that still ends in .php.  This way, if somebody gets into your file system and tries to load a file, they will only get the end result of the .php file rather than a potential print out of the actual code in the file.  Try adding .inc.php to all your include files, or something similar.  That way, not only are they a little more secure, they are also easy to spot in your directories.

As with anything, be sure to keep it simple until you fully understand it. Play with it and have some fun. It can help you manage your sites more easily.
 
Next >
© 2010 Quiet Thunder Marketing
Joomla! is Free Software released under the GNU/GPL License.