University of Southern California

ITS Information Technology Services

Using Server-Side Includes


Server-side includes (SSIs) allow authors to include some dynamic information in static HTML pages. For instance, a standard header or footer can be included in all documents in a web site without the need to reproduce the HTML code on each page. Or, information about the document, such as the last time it was updated, can be displayed automatically.

SSI commands are embedded in HTML files, enclosed in HTML comment tags so that they will not be displayed. When a browser requests a file containing SSI commands, the server parses (reads) and executes the commands before sending the page contents to the browser. In this way, the contents of an external file can be included on a page, or real-time information, such as the system date, can be displayed.

Enabling Server-Side Includes

By default, the web server does not parse a file before serving it to the browser. In order to get the server to search for SSIs before displaying a page:

  1. place a file called .htaccess in the directory which includes the directive:
    AddHandler server-parsed .html
    Since server parsing takes extra time, it's not wise to use this method unless a majority of the .html files in the directory require parsing.

  2. If the files you want parsed have a different file extension, first define that extension as text/html:
    AddType text/html .ssi
    AddHandler server-parsed .ssi

Embedding Commands

SSI commands are embedded in HTML code using HTML comments, e.g.,

<!--#flastmod virtual="thisfile.html" -->

in the location where the command results should be displayed.

Basic Commands

fsize

Prints the size of a specified file.
Example: The size of this file is <!--#fsize virtual="ssi.html" -->.
Displays: The size of this file is .

flastmod

Prints the last modification date of the specified file.
Example: Last modified on <!--#flastmod virtual="ssi.html" -->.
Displays: Last modified on .

include

Inserts the text of another document or file into the parsed file.
Example: <!--#include virtual="includefile.html" -->
Displays:

Specifying File Paths

Use the "virtual" attribute to specify a file path in a command.

virtual="filepath"

The virtual path is interpreted in the same way as a partial URL would be: If it begins with a slash (/), the path is taken to begin at the server root. If it does not begin with a slash (/) then it is taken to be relative to the current document.

Include Variables

Include variables provide information on the current document, server, browser and date. The most commonly-used variables are:

HTTP_USER_AGENT
The browser client in use
REMOTE_ADDR
The browser's IP address
DATE_LOCAL
The local date and time
LAST_MODIFIED
When the document was last updated
SCRIPT_URI
The document's URL

You can display the value of a variable using the echo command:

echo
Prints the value of an include variable.
Example: Last modified on <!--#echo var="LAST_MODIFIED" -->
Displays: Last modified on
(a better way to display the last modification time of the current file)

You can use the SSI printenv command to see a list of all variables and their current values:

printenv
Prints a list of all variables and their attributes
Example: <!--#printenv -->
Displays:

Last updated:
October 22, 2009

Web Publishing Documentation

The use of all USC computing resources is governed by the USC Computing Policies.