Using PHP Includes

When creating websites that are likely to have a lot of pages, updating the links in the navigation may become a long process. To make life easier, if you use server side includes, you will only have to edit the navigation once and the changes will update across the whole site.

Quick tip though – if you are working in a local development environment such as MAMP on OSX or MAMP on Windows, you will need to set up what is known as Virtual Hosts. There is a good explanation here that helped me: http://forums.macrumors.com/showthread.php?t=749072.

To get started with server side includes, I build a complete template page with the navigation and graphics which I style up first. Now, lets say your navigation is going into a file called header.php. Open a new blank page and copy and paste the divs from your template that contain the navigation.

Make sure all your links and paths are site root relative (such as <img src=”/img/graphic.png” />). Save the file as header.php either in the site root or in a directory called includes.

In your new pages, you simply need to add <?php require_once (“includes/header.php”); ?> in the place where your header.php will sit, perhaps a #header div. Remember, if you want to include these files in any pages in sub-directories, change the path to the included file as necessary to something such as <?php require_once (“../includes/header.php”); ?>.

other page


3 Comments

It's good to talk!

Your email address will not be published.