Tasen Software Home
Using PHP Includes to Manage Common Components

Q: How can I keep certain content static from page-to-page in my website?
     How can I alter one or two values in the content simply and easily?

A: Say you want to keep the header for your website consistant throughout each page, but you want to have the page title display differently right below the header, on each page (not in the title bar). For an example see this website, where the page title is shown just below the navigation bar on each page.

What you might do is this:
  1. Firstly you will create your index.php page, for which the title will be "Index Page".

  2. Next you will create your faq.php page, for which the title will be "FAQ Page".

  3. Next you will create an "includes" folder in the same directory as your index.php and faq.php files.

  4. In the includes folder you will create a header.php file.

  5. Within the header.php you will include only the code that is used to create your header. It might consist of a logo or an image, a navigation bar, whatever you choose. At the very bottom of this file you want to include something like:

    <?php echo '<h1>'.$pageTitle.'</h1>'?>

    This will be the variable that stores the page title.

  6. At the top of your index.php file, you will want to include the following code:

    <?php $pageTitle='Index Page'?>

    This will be the way we store our page title. For the FAQ page you would just say:

    <?php $pageTitle='FAQ Page'?>

  7. Finally, you will want to include the following code in index.php and faq.php, probably between DIV tags:

    <?php include_once('includes/header.php'); ?>

    This code should be placed at the top of the body section, as this is where a header is normally rendered.
Now when you view your index.php and faq.php files, you should be able to print out identical headers on each page, with dynamically configured page titles. Some people would have you name the header file header.inc, but Dreamweaver does not recognize these as PHP file be default, so you will lose the syntax highlighting when you edit them.

That's all for now, but please don't hesitate to contact us with any further questions.


©2006 TasenSoftware.com - All Rights Reserved
Website Design & Software Development in New England - New Hampshire (NH), Massachusetts (MA), Vermont (VT), and Maine (ME)