Enhance Your Site With PHP Includes
An external style sheet allows all of a site’s styles to be controlled from one point—which makes for very efficient page layout and design.
“PHP includes” can be used in a similar way, in that elements appearing on multiple pages can be controlled from one location. For example, it is common to have a menu on every page of a website. If you want to update that menu—such as adding a new menu item—it will be a nightmare to do this if you have to open every page on your site to alter each instance of that menu.
Luckily, you do not need to know much about PHP to use PHP includes. There are various ways to use them, but what follows is a very simple method that works very well.
Step one
Let’s assume that you are creating an include file for your site’s main menu.
Firstly, create a new directory (or folder) on your website, and call it something like “includes
”. (For this example, it is assumed that this directory is located in your root folder—in other words, that the directory is located at yoursite.com/includes/
.)
Inside this directory, create a new file called menu.php
. (It is not essential to use the .php
extension here—you could use .html
, for example—but it’s worth doing so, as I’ll explain later.)
Now, copy the menu code from one of your pages and paste it into the menu.php
file. Here, for example, is the (somewhat simplified) code for this site’s menu (which I keep in a single include file):
<ul class="menu" role="navigation">
<li><a href="/services">Services</a></li>
<li><a class="current" href="/notebook">Notebook</a></li>
<li><a href="/code">Code</a></li>
<li><a href="/learning">Learning</a></li>
</ul>
Upload menu.php
and its directory to the web.
Step two
Ideally, you would have set up your system of includes while building your site. If your site has already been created, you will have to do a bit of manual labor; but once you’re done, you will never have to do this again.
For PHP to work on a web page, the page needs to have a .php
extension, rather than .html
. That’s the only real bad news if your site is already set up with .html
pages. (However, if this is your situation, don’t despair, as there is a simple way to allow PHP to run on .html
pages by means of a .htaccess
file.) When I build sites now, I never use .html
any more, but rather .php
—regardless of whether or not there is any PHP on the page. (It will make life easier in future if I need to add some PHP.) When a page on the internet is requested by a browser, the server that hosts the page will check to see if there is a .php
extension, and if there is, it quickly runs the page through a PHP parser (which carries out any PHP instructions in the page) before serving the page to the browser. For our menu example, we want the PHP parser to include the menu.php
code on the requested page before it is sent to the browser.
So, back to the setup. On every page where the menu is to appear, remove the menu code and replace it with this:
<?php include $_SERVER["DOCUMENT_ROOT"] . "/includes/menu.php"; ?>
It’s as simple as that! The beauty of $_SERVER["DOCUMENT_ROOT"]
is that you can use the same code on every page of the site without having to change the path to the menu.php
file.
Final notes
In the example above, I just placed the actual menu code in menu.php
, but you can actually place as much code into an include file as you like—such as the containing div
etc. I tend to place any code that will appear throughout the site into a PHP include, so that it will be easy to edit from one location in future. DOCTYPE, CSS links, header sections, footer div… You name it, it’s in an include.
You can, of course, place PHP includes within PHP includes, which is why I use the .php
extension for my include files—just in case.
As I said, you don’t need to know much about PHP at all to use PHP includes. The code that tells the PHP parser to do something is the <?php ... ?>
bit. The parser will execute whatever instructions are found in between those opening and closing tags. The only other important thing is to give the page a .php
extension. When you rename your pages with the .php
extension, make sure to update any links that reference the .html
version. (It is also good to delete the .html
versions of each page from the server if you are uploading pages from an FTP program, as both the .php
and .html
versions will be sitting on the server. Make sure your .php
pages are working nicely, and links all updated, before deleting the .html
versions.)
Acknowledgements & Links
- You can learn more from the Apache website: Introduction to Server Side Includes.
Legacy Comments
Hi!
I really liked your guide on “Enhance Your Site With PHP Includes”. I have now moved all my
.php
to separate folders so I getdomain.com/contact/
instead ofdomain.com/contact.php
, but I’m wondering how I can do with the “root index.php”. My first landing page atdomain.com/index.php
. I can’t put that in a/home/
folder for example, or can I?Do you have any tips on how to solve this?
Kind regards
Kevin
Hi Kevin!
Actually, it’s much the same as what you were doing with the contact page, except that this time you just point to your domain without
index.php
on the end. That is, if you just point todomain.com
, you will see theindex.php
page without having to type it as part of the URL. So make sure all your links to the hole page are justdomain.com
rather thandomain.com/index.php
.An alternative is to add some code to a
.htaccess
file to ensure that no one can see theindex.php
extension even if they type it into the browser, although that’s a different topic.Placing the home page inside a
/home/
folder wouldn’t work, because there must be an index file in your root folder as your home page—again, unless you want to do a.htaccess
redirect, but that’s not common and pretty undesirable, I think.Hi Ralph
I tried using the php and a separate folder includes in root directory and navigation code copied there but as i copied your code changing the menu to nav as i named the file nav.php, my navigation disappeared from the pages.
Please help me fix the problem.
Thanks
Sandy
Hi Sandy. Should be easy to sort out. A bit hard here, though. Perhaps email me with a more detailed account of your setup.
Thanks for this, this has saved me a lot of time. However, after I changed all my .html files to .php to cater for the includes, I found I couldnt create a sitemap using my usual xml sitemap generator? (link here) http://www.sitemapdoc.com/Default.aspx I like this generator because it also spots any unbroken links etc.
Is this because the files are now .php? if so, how can I generate an xml sitemap to upload to Google?
Thanks
Hi! I wouldn’t think the .php files would make any difference. I tried this on a site with .php pages and it worked fine. I wonder how the map is generated. If all the pages are pulled from a Google search, it may be that Google hasn’t re-indexed the site yet. Just a thought.