1. Technology

How To Include HTML in Many Documents Using PHP

By

If you want the same content copied over multiple pages of your site, with HTML you'll need to manually copy and paste that content. But if you have PHP on your server you can write one file and then include it on your Web pages where you need it.

PHP requires that you have it installed on your Web server. Contact your system administrator if you're not sure.

Difficulty: Average
Time Required: 15 minutes

Here's How:

  1. Write the HTML you want repeated and save it to a separate file.
    I like to save my include files into a separate directory, usually "includes". I would save my copyright information in an include file like this:
    includes/copyright.php
  2. Open the Web page where you want the include file to display.
  3. Find the location in the HTML where the include file should display, and place the following code there:
    <?php
    require($DOCUMENT_ROOT . "includes/copyright.php");
    ?>
  4. Change the path and file name to reflect your include file location.
  5. Add that same code to every page you want your copyright information on.
  6. When the copyright information changes, edit the copyright.php file. Once you've uploaded it to the includes/ directory, it will change on every page of your site.

Tips:

  1. You can include HTML or text in a PHP include file. Anything that can go in an standard HTML file can go in a PHP include.
  2. Your entire page should be saved as a PHP file, eg. index.php rather than HTML. Some servers don't require this, so test first.

What You Need

  • PHP on the server
  1. About.com
  2. Technology
  3. Web Design / HTML
  4. Web Programming
  5. PHP
  6. How to Include HTML in Many Documents Using PHP

©2014 About.com. All rights reserved.