Server Side Includes (SSI)

(This material last modified

Some Web servers allow the inclusion of some simple items such as the current time and when a file was last modified -- without the need for JavaScript, Java, Perl, ASP, JSP, or PHP

Server Side Includes (SSI) can be used for such simple items as well as to make a simple visitor counter

Many Web servers do NOT allow Server Side Includes because of security and/or time concerns. Make sure that your Web server does allow them before spending a lot of time making Web pages with SSIs. PUCC Web servers (icdweb, mentor, expert) DO allow SSIs.

Most servers that do allow SSIs look for them by default in files with a .shtml suffix and send those parsed files to the browser indicating that they are the same as .html files. In this arrangement, no time is wasted looking for SSIs in regular .html files -- only in .shtml files.

If you want to use any other suffix for SSI files, create in your www folder (or some sub-folder) an .htaccess file that includes a line like...

AddType text/x-server-parsed-html .incl Then, any file in that folder with a .incl extension will be parsed for Server Side Includes before sending to the browser. So, the default .shtml is just like you had... AddType text/x-server-parsed-html .shtml in an .htaccess file.

SSI Commands

All SSI directives look like HTML comments. (Servers that do not handle SSIs or any SSIs that are included accidentally in files without an .shtml extension will simply be sent along to the browser as a comment.) Each SSI directive has the following format:

<!--#command attribute="value" -->

Here are some of the most useful commands and attributes:

<!--#echo var="variable" -->

Echo prints the value of one of the variables below:

REMOTE_HOST
The remote machine that is making the request.
REMOTE_ADDR
The IP address of the remote machine making the request.
DATE_LOCAL
The current day, date, and time at the server.
HTTP_USER_AGENT
The browser the user is using.
<!--#flastmod file="location" -->

flastmod prints the last modification date of the specified file.

<!--#include file="location" -->

Include will insert the text of a document into the parsed document. "Location" is the same as a relative URL.

<!--#exec cgi="scriptname" -->

exec cgi executes a given shell CGI script. (Sometimes echo, flastmod, and include are all enabled, BUT exec is not!)

An SSI Example

Let us examine a sample Web page that uses SSI.

First, notice that this Web page is named servexamp.shtml. The .shtml extension is important or none of the SSIs will work. The file looks like this:

<HTML> <HEAD> <TITLE>Server Side Include Examples</TITLE> </HEAD> <BODY BGCOLOR=WHITE> <H1>Server Side Include Examples</H1> REMOTE_HOST is <!--#echo var="REMOTE_HOST"--><P> REMOTE_ADDR is <!--#echo var="REMOTE_ADDR"--><P> DATE_LOCAL is <!--#echo var="DATE_LOCAL"--><P> HTTP_USER_AGENT is <!--#echo var="HTTP_USER_AGENT"--><P> This file was last modified <!--#flastmod file="servexamp.shtml"--><P> You are visitor number <!--#exec cgi="count.cgi"--> to this Website.<P> <!--#include file="info.txt"--><P> </BODY> </HTML> Notice the first four "echo var" directives to report the REMOTE_HOST, REMOTE_ADDR, DATE_LOCAL, and HTTP_USER_AGENT.

Next, notice the "flastmod" directive to report when this file (it could be any file in this folder) was last modified.

The "exec cgi" directive runs a Perl script named count.cgi in the same folder which looks like this:

#!/usr/local/bin/perl open (COUNT, "<count.txt"); read (COUNT, $iCounter,10); close (COUNT); $iCounter = $iCounter + 1; open (COUNT, ">count.txt"); print COUNT "$iCounter \n"; close (COUNT); print "Content-type: text/html \n\n"; print "$iCounter"; The file count.txt is world-readable and world-writeable and contains a single integer number.

Finally, notice the "include file" directive to grab another file info.txt and to include its contents as if they appeared in servexamp.shtml at that point.