Let's revisit the story I began when we were discussing Dreamweaver library items on the previous page of this tutorial…
So, the story goes… a web designer maintains a 200 page site for a good client. The client is granted a trademark and wants to change the legal line (in the footer) on every page. What's the fastest way to do it? You could use find-and-replace. But what if you type an incorrect character? Or forget a <? Or the legal line is used elsewhere on the page, or the site?—and for now, you only want to change the text in the footer? What now?
Ahhh… the wonderfully fast and efficient workflow tool that is the… PHP include. PHP includes help me to greatly increase my web workflow efficiency. I currently manage several web sites that total well over 300 pages. Simply stated, I couldn't do this without includes. Unless I quadrupled my time spent—which I don't have. With a little planning and preparation, includes are a real time-saver.
Writing the code
First, I write my code, save it, and name it with the .php file extension. Just open a blank page in a text editor or the Code view in Dreamweaver and start typing.
Here is what my php include code for the right aside categories(on this page) looks like:
<a href="web.intro.html"> Intro to Web Design<br>
</a><a href="web.web-fundamentals.html"> Web Basics<br>
</a><a href="web.web-v-mobile.html"> Web v Mobile<br>
</a><a href="web.library-items.html"> Library Items<br>
</a><a href="web.web-analytics.html"> Web Analytics<br>
</a><a href="web.rollovers.html"> Rollovers<br>
</a><a href="web.favicon.html"> Favicon<br>
</a><a href="web.apple-touch-icon.html"> iPad Icon<br>
</a><a href="web.pause-play-btn.html"> Play/Pause Btn<br>
</a><a href="web.semantic-web.html"> Semantic Web</a></p>
<a href="web.css-nav-styling.html"> Navigation<br>
</a><a href="web.css-list-styling.html"> Lists (ol ul li)<br>
</a><a href="web.font-face.html"> font-face<br>
</a><a href="web.media-queries.html"> Media Queries<br>
</a><a href="web.rounded_corner.html"> Rnded Corners<br>
</a><a href="web.css-dropshadow.html"> Drop Shadow<br>
The above code is saved as include.web-nav.php and uploaded to my hosting server in the top level of my site's root directory.
Place the include on your page
The next step is to call for the include (which contains the list I am displaying on this site's aside) in your page's html. Place this inside a div:
<?php include ("include.web-nav.php"); ?>
Pretty fancy, huh?! We're almost done, but we still have one last trick to get the include to load when our web page is viewed.
Apache handlers and .htaccess files
Our next step is to tell my hosting server what to do with your .php file when the browser calls for it (tries to load it). Here is a good description of how an Apache handler works, quoted from my web hosting company bluehost.com, "Apache handlers control how the Apache web server software manages certain file types and extensions for your site." The primary thought behind this is that we want to tell the server to "handle" our .php file like it is an .html file. My web hosting company has a cool area on their web site that helps clients like me write the Apache handler code.
Again, like before, I opened a blank page in a text editor or Code view in Dreamweaver and wrote:
# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php .html
Then, I saved the file and named it .htaccess. I put this in the top level of my site's root directory and uploaded to my hosting server.
And to my amazement, it worked!
(A side note: I used to host with godaddy.com and this was the code that worked in the .htaccess file:
AddHandler x-httpd-php .html .htm .cgi .php. Be sure to check with your hosting company to see what code you need to use for your Apache handler.
The beauty of includes
I found php includes demonstrated several advantages over Dreamweaver library items including:
Once you design web sites in WYSIWYG web design software (like Dreamweaver) for a year or so (maybe shorter, or longer, depending on how much work you do) you will begin to realize that working with raw html code is cleaner and faster for some tasks. Once you get to this point, you may want to dump Library items and switch to Includes.
When DW inserts a library item into a page, it writes all of the html code for the item into your page's html code. When you change a library item, in order to update your site, DW basically performs a site-wide find-and-replace based on the newer library item's chunk of code. While this doesn't take hours, it can take several minutes—a life time in today's measure, right?! And it doesn't always work seamlessly.
- Simple and fast
When you update the PHP include file, all you have to do is upload that single file and you're done. (the PHP script loads the html include file only when the page is loaded). There is no site-wide find-and-replace.
- Works in html text editors
I like to update my web sites on the go from my iPad, using an html text editor. This means no Dreamweaver, only raw html text. So I definitely had to switch to PHP includes because, currently, there is no Dreamweaver for iPad. And, currently, there is no site-wide find-and-replace in my html text editor. Maybe
I like learning and growing, so what could be cooler than casually telling your web designer friends, "Oh, it was no big deal, I just wrote a little html code to create my include, wrote a script to load that include into my page's html, wrote an .htaccess file to instruct the server's Apache software to read the .html files as PHP, and I was done before lunch! (at least I think that's what I did).
The one disadvantage might be that each time a web page loads is has to call out and load the include, and this can slow the page load. From my experiences, I have not been able to tell the difference. I think the decreased time is close to a fraction of a second, because when I load my web pages, they pop up instantly.