Server-Side (Includes) Magic

Server-Side Includes (SSI) are commands that allow you to control aspects of the server from within your web page.  SSI is useful to add small pieces of information or files, such as the time or a header or footer, to many web pages when they are rendered.  If you ever have to edit these files, you only need to edit the one file and the changes appear throughout your web page.

Basic SSI

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.

Examples

Today's date

<!--#echo var="DATE_LOCAL" -->
Friday, 18-May-2012 19:07:33 EDT

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. Note that the format codes here are for a Linux server; codes for a Windows server may be different.
<!--#config timefmt="%A %B %d, %Y" -->;

Today is Friday May 18, 2012

Modification date of the file

You can show the last modified date of a file using
<!--#flastmod file="ssi.shtml" -->
This document last modified Wednesday November 05, 2008
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. The file you are calling here is actually a program that produces an output. Note the program must be in the cgi-bin folder. Some hosting services turn this off due to security issues. <!--#include virtual="/cgi-bin/randhtml.cgi" -->
[an error occurred while processing this directive]

The Major Magic

To me, the most major magic of SSI is the ability to grab other files and insert them into my HTML page. I use this for parts of the pages that are the same across all or most pages, such as the header, footer and navigation area. i can also insert files which I have given others permission to edit. Since these are simple text files with a bit of code, they are easy to edit.

<!--#include virtual="footer.htm" -->

Copyright PC Club of Charlotte - 2008 Article by Dewey Williams

The files included using SSI should not be full HTML files with a DOC TYPE, header, html and body tags sicne they are being inserted into a file that already has those sections. To differentiate from regular HTML file, I place most of my include files in a special folder. i also give them an extension different from my default HTML file extension.

For SSI files to be processed by the server, the server most know which files to look for SSI directives in. The standard is to use .shtml as the extension for SSI-enhanced files.

Links to More Information

http://httpd.apache.org/docs/2.2/howto/ssi.html
http://en.wikipedia.org/wiki/Server_Side_Includes
http://pang.ncsa.uiuc.edu/docs/tutorials/includes.html
Google Search for "server-side includes"