How To Include HTML in Many Documents Using PHP

website plan
Image courtesy Stockbyte / Getty Images

If you look at a any website, you will 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, like social media widgets or buttons or other piece of content, but the header and footer areas being persistent across every page is a pretty safe bet for most websites.

This use of persistent area is actually a web design best practice. It allows people to more easily understand how a site works and once they understand one page, they have a good idea of the other pages as well since there are pieces that are consistent.

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 adding a new link to  your site's navigation menu. To make this seemingly simple edit, you would need to change every single page on the website. This is 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 solution. This is because if you ever need to edit that form in the future, you would do so in one spot and every page that includes it would get the update. 

First off, you must understand that using 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. If you do not have it installed, ask them what it would take to do so, otherwise you will need to find another solution for includes.

Difficulty: Average

Time Required: 15 minutes


  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.
    1. 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:
    2. 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
    1. <?php
  4. require($DOCUMENT_ROOT . "includes/contact-form.php");
  5. ?>
    1. 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.
    2. 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 right from the get-go.
    1. If you wanted to change something on the contact form, like adding a new field, you would edit the
      file. Once you've uploaded it to the
      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!


    • 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.
    • 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, but an easy way to ensure you are all set is to just use.