How to Add Server Side Includes to a Website Using PHP

By Virginia DeBolt

Rate: (6 Ratings)

A Server Side Include (SSI) is a small bit of HTML that can be inserted onto any page of your website by the server when the page loads. A single Server Side Include file can be used again and again on many pages of a site, thereby saving a great deal of maintenance work when changes need to be made.

Instructions

Difficulty: Moderate

Things You’ll Need:

  • A website
  • Some knowledge of HTML and PHP
  • Nothing new is needed in this section

How to Create the HTML Include

Step1
A snippet of HTML that will become an include First create the HTML that you want to appear in place of the PHP Include. This is not a complete web page; it is just the small bit of HTML that will be included on a larger web page where you indicate. The accompanying image shows an example of a menu that could be saved as an include.
Step2
Create a new directory on your website to hold your include files. A directory name such as "inc" or "includes" is appropriate. Save the HTML snippet in that directory. The example in Step 1 could be saved as menu.html.
Step3
In the case of a menu or any include with links, write the links in such a way that they will work properly no matter where they are on your website. You may have to use absolute links to accomplish this.
Step4
Includes can insert any HTML: an image, an email address, a privacy statement, a table, a logo.

How to Write the PHP Include Command

Step1
On your complete web pages, write the PHP Include command on the page in the exact position where the included snippet of HTML is to appear.
Step2
A PHP include command For example, suppose you are working on your home page: index.php, using the menu.html example from Section 1, you would add the PHP shown in the image to place an include on your home page.

When the page is loaded in the browser, what is shown in that position on the page is the HTML from the included file.
Step3
The image in Step 2 shows a path to the included snippet (menu.html) that is found in a directory called "includes" at the root level of the web server. Since the menu.html file is written so that the links for the menu will work no matter where they are placed on the site, the same include can be added to many other pages besides your index.php page. In fact, you could add that include to EVERY page on your site.
Step4
If you want to change the menu (or anything you've added to a page using a PHP include) you merely find the original menu.html file in the includes directory, change it and upload the changes. Every single page on your site that uses that include file will reflect the change immediately.

You can see that Server Side Includes can save you a lot of time and effort when you need to make changes to items that appear over and over again on your website.

Tips & Warnings

  • The image in Step 1 shows a menu that uses the site root as a starting point for a link. The / before the directory path indicates that the path to the link begins at the site root. If you use an apache server, you can write links this way so that they will work on any page on the site.
  • SSIs are perfect for menus, headers, footers, contact information, privacy statements, or other page elements that appear on most or all of the pages of your website.

Comments

| View All Comments

Veesites said

Flag This Comment

on 1/29/2008 jonxblaze, yes, you can include php files with an include. And, some includes are txt files.

Flag This Comment

on 1/18/2008 Thank you for this, can you also include a php file as opposed to an html file?

Veesites said

Flag This Comment

on 1/5/2008 Thanks, grouch, I appreciate the comment.

grouch said

Flag This Comment

on 1/5/2008 Thanks for the great step by step. It was very well written with clear directions.

View All

Post a Comment

POST A COMMENT

Request a New How-To Article

Looking for more How To information? Chances are there’s an eHow member who knows how to do what you’re looking to do. Submit an article request now!

eHow Article:  How to Add Server Side Includes to a Website Using PHP

eHow Expert: Virginia DeBolt

Virginia DeBolt

Expert: Internet

Profession: http://www.vdebolt.com

Location: USA

Related Ads

Internet

Veesites
Meet Virginia DeBolt eHow’s Internet Expert.