home fanart gallery downloads tutorials www

php includes with headers


This tutorial will teach you in detail about headers and footers how they work with php extensions. To understand this you need to have a basic knowledge of HMTL code.

So lets say you have your first site online, and it started out as a few pages and is now starting to grow. You enjoy making and changing the layout but are finding that with more pages recoding each page with the new layout is taking longer and longer… then you reading the right tutorial!

Headers and footers means that you will in future only have to update 1 page each time you change the layout and the rest of the pages will take everything from that. It’s like the stylesheet but it will hold your design and images as well. (if you don’t currently use stylesheets, I have a tutorial on them here)

Firstly you need to make sure you have php enabled on your server. Most host have this as default now, but it’s worth checking before you start. If you can’t see it stated in your control panel options, then email your host to ask. The php function includes the file specified in the ()'s. (When a file is included, the code it contains inherits the variable scope of the line on which the include occurs.) Therefore on your pages you only need to put ‘include’ and it will take the code from the file you’ve coded completed, the header.

Anyway, enough behind how it works, on to how to code it.

Here is roughly what your pages now look like in terms of design, the coding you are currently changing on each page when the layout/design changes.



The first thing you need to do is to rename every page on your site to end with .php instead of .htm so your index page becomes index.php from index.htm

Then we will have to create the header and footer files for your site. Look at your current index file (similar to the code above) and copy everything that is above the content. Paste it into an empty notepad file, and name it header.php. Your header.php file should look something like this.



You will see we’ve literally copied everything right up until the content starts. This is because we want the content in the same place on every page, but again we want to only change it once when we change the layout. So we have the div layer for content opened on the header page.

We then save this file as header.php

We now need to create the footer.php, this is the simple one as it literally needs to close that content div layer, and end the html page. So our footer will contain only this;



We then save as footer.php

Now for the part we need to put at the top of all our pages to tell them to read the header and footer and include everything from those files into this page.

So in each page make sure there is no layout/design coding and then insert this code:

<? include('header.php'); ?>

At the top of the page and

<? include(‘footer.php'); ?>

At the bottom.


So your content pages should literally look like:
 

<? include('header.php'); ?>
Today I added the following pieces of art…..

.. that’s all for now
<? include(‘footer.php'); ?>



So now your files will all look exactly the same, and you will not have to make any changes to them, only the header.php will contain the layout details, and once that’s changed and the new images uploaded the whole site will change.

I hope that was helpful and easy to understand.