home  THE WWW  : SSI Server Side Includes 

Server Side Includes (SSI)

Server Side Includes (SSI) are tags that are put into an HTML document that require the webserver to parse (scan for commands) the document, perform some function (such as executing a CGI script) and then send the document to the client.

When implemented, SSI can be a real time saver. For example, every document in this support section uses the SSI command include to insert the header images and links (header_support.html). By doing this, any changes that need to be made to the header will be made to one file instead of all of the files in the support area. The value of SSI is apparent in situations such as this.

The first rule of using SSI is that any page containing commands should be named .shtml, .shtm or .sht. By changing the extension, you're letting the webserver know that those files must be parsed before sending them to the client.

The next thing to remember is that all SSI commands must use the following format:

<!--#include virtual="/dir/file.ext"-->

The tag used for SSI is similar to the HTML comment tag which looks like this:

<!-- Comment Tag -->

Common SSI Commands

Config - Echo - Exec - Flastmod - Fsize - Include

Config

The SSI command config allows to you to set output options for other commands.

Echo

This SSI command allows you to print information determined by the variable you use in conjunction with it. Below is a small list of useful variables. A complete list can be found in the NCSA's SSI Tutorial and on their CGI Environment Variables page.

*The output of these commands can be altered with the timefmt variable of the SSI command config.

Exec

The SSI command exec allows to you execute a CGI script from within a webpage. For example, the following script checks if the browser accessing this page is Microsoft® Internet Explorer® 5.0. If you're using IE 5.0, it will display text informing you so. If you aren't, it will insetad generate text informing you of that:

#!/bin/perl
my $agent = $ENV{HTTP_USER_AGENT};

if ($agent =~ /MSIE/) {
    print "Content-Type: text/html\n\n";
    print "\<span\ class\=\"code\"\>You\'re\ using\ Microsoft\&reg\;\ Internet\ Explorer\&reg\;\ 5\.0\<\/span\>\n";
    } else {
        print "Content-Type: text/html\n\n";
        print "\<span\ class\=\"code\"\>You\'re\ not\ using\ Microsoft\&reg\;\ Internet\ Explorer\&reg\;\ 5\.0\<\/span\>\n";
    }
exit 0;

This script is called with this tag <!--#exec cgi="exec-test.cgi" --> and the actual text output appears as such:

You're not using Microsoft® Internet Explorer® 5.0

Flastmod

The SSI command flastmod works exactly like the echo command when it's used with the variable LAST_MODIFIED, that is, it displays the date and/or time the file was last modified*. For example, This page last updated <!--#flastmod file="SSI_Server_Side_Includes.shtml" --> displays:

This page last updated Saturday, 7-Jan-2006 18:07:20 GMT

However, flastmod and echo differ in that flastmod allows you to specify the following two attributes. These attributes may be used to display the "last modified" date of other files on the server. This would be valuable in a number of situations, for example if you were creating a site map and wanted to list the date that each file had last been updated.

The output of this command can be altered with the timefmt variable of the SSI command config.

Fsize

The SSI command fsize enables you to display the size of a file in a parsed file. For example, This page is <!--#fsize file="SSI_Server_Side_Includes.shtml"--> will appear as:

This page is 16K

The output of this command can be altered with the sizefmt variable of the SSI command config.

Include

Perhaps the most useful SSI command is include. This command enables you to insert the text of another file into the parsed document. For example, to include a file called monkey.txt, you'd use <!--#include text="monkey.txt"-->. The output from this command would look like this:

Monkey's are cute. Monkey's have opposable thumbs.

Like the flastmod command, include has two variables that can be used for added flexibility:

Related Items

Email and Web Hosting by Internet Connection 2004-2007 ©
Contact IC Support via AIM!  Our SN is ICSupportDesk