When creating a basic website most people create a standard design, usually with a navigation menu to the left, top or right. This is the best way to do it as shown by many successful websites. The only problem with using a navigation on every page that needs to have new links added to it often is when you have many pages, it can become a very time consuming operation.
In this article I am going to show you how to create a PHP Include. This will allow you to update your navigation pane from one single file that will update all the other pages at the same time. PHP includes are very easy to use and are useful for anyone with a static website.
How it works
The best way to think of a PHP include is as a window, imagine all your pages have this window and on the other side of the window or a frame. It’s like an iframe except it does all of the content injection on the server side.
I’m not sure that description makes sense, I’ll probably change that at some point.
Anyway, on each page where the navigation is meant to be, you put a small PHP code snippet. This snippet points at a single file somewhere on your server. Imagine the single file was called “hello.php” and on your page “index.php” you have the PHP code to inject hello.php the content of “index.php”. If “hello.php” has the word “hi” in it, “hi” will appear in “index.php” as if it was normal text entered in “index.php”. Now, imagine what you can do when you put the php code on all of the pages of your site. You can put it on as many pages as you like and you can put anything inside “hello.php”.
For this excersise we’ll be putting some links in “hello.php” and including it in “index.php”.
Code
First, make sure you have created “hello.php”
You should already have your own site design, so I’ll assume you do. Now what you need to do is copy all of the links in your navigation menu to “hello.php” and save it. Make sure “hello.php” has NOTHING else apart from your links, no styles headers or anything. Not even HTML or BODY tags! Now save that file and put it on your server.
You’re almost done, all you have to do now, if go round and replace the navigation links with the following code:
<?php include('hello.php') ?>
If you want to put your includes in a sub directory (example.com/includes maybe?) then use this: (remember to replace includes with the name you gave your own sub directory)
<?php include('includes/hello.php') ?>
That is all you should need to do to get your navigation included. If you have any problems I will be more than happy to help you. Just leave a comment if you get stuck.
« Go back to the tutorials index.

July 21st, 2007at 12:55 pm
you do know that PHP is inferior compared to the coding for windows flight simulator
July 21st, 2007at 1:00 pm
Would you please explain?
July 21st, 2007at 1:04 pm
well vista has this running capability that makes it look so realistic that when my mom saw it she got scared and said your moving with your auntie and uncle in bel-air!
I whistled for a cab and when it came near
The license plate said fresh and it had dice in the mirror
If anything I can say this cab is rare
But I thought ‘Now forget it’ - ‘Yo homes to Bel Air’
I pulled up to the house about 7 or 8
And I yelled to the cabbie ‘Yo homes smell ya later’
I looked at my kingdom
I was finally there
To sit on my throne as the Prince of Bel Air!
October 17th, 2007at 12:57 pm
strange…. I kept scrolling down to actually get to the useful bit and it never came.
What the hell is that? that has povided no server side way of navigation whatsoever.
October 17th, 2007at 5:24 pm
Sorry, I don’t understand your problem. Please digress..
February 24th, 2008at 5:55 pm
I’m searching for a tutorial how to make a navigation for my site since to days. I read through three forums and many websites containing information about php and its possibilities. I now know something about ’switches’, ‘arrays’, the ‘get’ function and so on but all I found so far is not working or maybe I just don’t understand php.
But to be frank; your explanation about how to build a navigation with php is really poor. It left most things behind what a newbie in php is looking for. And so far your scribbles will work only if all files are in the same directory.
Thanks for nothing dude, but I will look further, maybe there IS someone out there with a more detailed description.
February 25th, 2008at 9:04 am
Hello there,
Sorry that you find this article confusing. I know, it does suck but was written a long time ago when my PHP skills just didn’t exist.
As a result of your comment I will be rewriting this article, sorry to waste your time.
One thing that pissed me off is the fact you said “thanks for nothing” - at the end of the day you didn’t have to part with any money to see this, so just pipe down.
Thanks for being rude.
March 9th, 2008at 10:24 am
Hi Adcuz,
I’m sorry for my behaviour. I didn’t mean to being rude, normally I’m a nice guy. I think that day I was really tired and frustrated and pissed that there is no easy example that really explains how to put together a naviagtion in php with for example using arrays and how to implement it. So far I have a xx.php with all my links/sites using the switch/case function and including it in my index.php. But I have 12 index.php sites, for each categorie one and therefor 12 xx.php to include. I just thought to re-design my site and instead of 12 index.php to use only one and instead of a xx.php with all the containing links/site of my website to use arrays. But somehow I don’t know how to do it.
So, I am sorry for being rude and will continue with my search.
Greetings,
Gabster
March 10th, 2008at 7:34 pm
Not a problem, we were probably both having a bad day at the time anyway.