Server Side, Making Your Life Easier: Part 2 – PHP Headers and Footers

This is an older tutorial and contains deprecated PHP code. I’ll revisit this in the near future to elaborate.

PHP is great because not only will it allow you to insert files like SSIs, it allows you to easily pass variables on to them such as a page title (your server must support PHP for this to work). You don’t need to know anything about PHP to use this. This example will show how to do this and pass on individual page titles, descriptions and keywords.

1. Create a page with headers and footers like you would with server side includes. (see pt. 1 of this post)

I suggest you name the pages header.php, index.php, and footer.php

2. In your header file, replace your title and meta tags with the following:

  1. <title><?=$title?></title>
  2. <meta name="description" content="<?=$description?>">
  3. <meta name="keywords" content="<?=$keywords?>">

3. In the index page, put the following at the very top

  1. <?php
  2. $title = "Page Title Goes Here";
  3. $description = "Page Description Goes Here ";
  4. $keywords = "Page Keywords Go Here ";
  5. include(‘header.php’);
  6. ?>

and put this at the bottom

  1. <?php
  2. include(‘footer.php’);
  3. ?>

4. Upload to the server and open index.php.

5. Create all of your other pages and use these same tags at the beginning and end, then if you need to change the site in a major way, just edit the header and footer files. This can all be done in a similar fashion in ASP, if anyone wants to now how, let me know and will post instructions.

Thats it, this will make your life easier. There are some drawbacks though. It is not always necessary to change these on every page (It is a good idea to change the title and meta description for every page. It makes your listings in the search engines much more attractive). In Pt. 3 I will show you how set default values so you don’t have to worry about managing keywords and descriptions for every page.

Leave a Comment

Please note: Comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.