How To Include HTML in Many Documents Using PHP

website plan
Image courtesy Stockbyte / Getty Images

If you look at a typical website, you will likely notice that there are certain pieces of that site that are repeated on every single page. These repeated elements or sections are likely to include the site's header area, including the navigation and logo, as well as the footer area of the site. There may also be other pieces that are present sitewide on certain sites, but the header and footer areas being persistent across every page is a pretty safe bet for most websites.

On normal HTML pages, these persistent areas would need to be added individually to each page.This poses a problem when you want to make a change, like updating a copyright date inside the footer or changing the word on one of the links in your site's navigation menu. To make this seemingly simple edit, you would need to change every single page on the website. Not a big deal if the site has one 3 or 4 pages, but what if the site in question has a hundred pages or more?  Making that simple edit suddenly becomes a very big job. This is where included files can really make a big difference.

If you have PHP on your server, you can write one file and then include it on any web pages where you need it. This could mean that it is included on every page, like the aforementioned header and footer example, or it could be something that you selectively add to pages as needed. For example, say you have a "contact us" form widget that allows site visitors to connect with your company.

If you want this added to certain pages, like all the "services" pages for your company's offerings, but not to others, then using a PHP include is a great solutions.

PHP requires that you have it installed on your web server. Contact your system administrator if you're not sure whether or not you have this installed.

Difficulty: Average

Time Required: 15 minutes

Steps:

  1. Write the HTML you want repeated and save it to a separate file.In this example, I want to include the aforementioned example of a "contact" form that I will selectively add to certain pages. 

    From a file structure standpoint, I like to save my include files into a separate directory, usually called "includes". I would save my contact form in an include file like this:
    includes/contact-form.php
  2. Open one of the web pages where you want the included file to display.
     
  3. Find the location in the HTML where this included file should displayed, and place the following code in that spot
    <?php
    require($DOCUMENT_ROOT . "includes/contact-form.php");
    ?>
  4. Note that in the abive code example, you would change the path and file name to reflect your include file location and the name of the specific file you want to include. In my example, I have the 'contact-form.php' file inside of the 'includes' folder, so this would be the proper code for my page.
     
  5. Add this same code to every page you want the contact form to appear on. All you really need to do is copy and paste this code onto those pages, or if you are in the process of developing a new site, build each page with the proper include files referenced.
     
  1. If you wanted to change something on the contact form, like adding a new field, you would edit the contact-form.php file. Once you've uploaded it to the includes/ directory on the web server, it will change on every page of your site that uses this code. This is much better than having to change those pages individually!

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 your configuration first.

Original article by Jennifer Krynin. Edited by Jeremy Girard on 12/09/16