Server Side Includes

[return to previous page]

What are Server Side Includes?
SSI [Server Side Includes] are directives that are placed in HTML pages, and evaluated on the server while the pages are being served. They let you add dynamically generated content to an existing HTML page, without having to serve the entire page via a CGI program, or other dynamic technology.

The decision of when to use SSI, and when to have your page entirely generated by some program, is usually a matter of how much of the page is static, and how much needs to be recalculated every time the page is served. SSI is a great way to add small pieces of information, such as the current time. But if a majority of your page is being generated at the time that it is served, you need to look for some other solution.

Configuring your account to permit SSI
Using your favorite editor create a file called .htaccess in the web directory where you want to permit SSI. In the .htaccess file, you’ll need the following directives:

Options +Includes

This tells Apache that you want to permit files to be parsed for SSI directives. Not just any file is parsed for SSI directives. You have to tell Apache which files should be parsed. There are two ways to do this. You can tell Apache to parse any file with a particular file extension, such as .shtml, with the following directives:

AddType text/html .shtml
AddHandler server-parsed .shtml

Basic SSI directives
SSI directives have the following syntax:

<!–#element attribute=value attribute=value … –>

It is formatted like an HTML comment, so if you don’t have SSI correctly enabled, the browser will ignore it, but it will still be visible in the HTML source. If you have SSI correctly configured, the directive will be replaced with its results. The element can be one of a number of things.

Ex: Today’s date
<!–#echo var=”DATE_LOCAL” –>

The echo element just spits out the value of a variable. There are a number of standard variables, which include the whole set of environment variables that are available to CGI programs. Also, you can define your own variables with the set element.

If you don’t like the format in which the date gets printed, you can use the config element, with a timefmt attribute, to modify that formatting.

<!–#config timefmt=”%A %B %d, %Y” –>
Today is <!–#echo var=”DATE_LOCAL” –>

Modification date of the file
This document last modified <!–#flastmod file=”index.html” –>

This element is also subject to timefmt format configurations.

Including the results of a CGI program
This is one of the more common uses of SSI – to output the results of a CGI program, such as everybody’s favorite, a “hit counter.”

<!–#include virtual=”/cgi-bin/” –>

Information on this page was obtained from Apache’s website.