Server Side Includes

We have already discussed the problem of viewers using different kinds of browsers.  In part this has been resolved with the introduction of XHTML.  However Server side includes allow us to provide the kind of interaction only previously possible by CGI scripts using a server-supported language such as PERL.

SSI allows the actual document served up to be changed depending on the viewer's browser; but also on the time of day, IP number, time etc.  Also database functionality can be added.  Here are some examples

Browser Output How it's done in SHTML
Today's date and time is Today's date and time is
<!--#echo var="DATE_LOCAL" -->
The link you used to get here is The link you used to get here is
<!--#echo var="HTTP_REFERER" -->
This is the Greenwich Mean Time This is the Greenwich Mean Time
<!--#echo var="DATE_GMT" -->
Your IP number is Your IP number is
<!--#echo var="REMOTE_ADDR" -->
This web page file name is This web page file name is
<!--#echo var="DOCUMENT_NAME" -->
Your browser & PC's operating system is Your browser & PC's operating system is
<!--#echo var="HTTP_USER_AGENT" -->
This web page file size is This web page file size is
<!--#fsize file="webSSI.shtml" -->
This page was last modified on This page was last modified on
<!--#flastmod file="webSSI.shtml" -->

 

Using SSI To Include A File

'Include' is another SSI command and it uses the 'file' attribute. With the 'include file' command we can include a text or html file into your shtml file (SSI enabled web page). The included file may for example be a common header or footer.

The syntax for this comment tag is

<!--#include file="myfile.txt" --> or <!--#include file="myfile.html" -->

Suppose we have a header file called "header.txt" and in this text file is

fleece was white

Our SSI enabled web page called "marylamb.shtml" contains

<html>
<head><title>Mary's lamb</title></head>
<body>
Mary had a little lamb, it's <!--#include file="header.txt" --> as snow.
</body>
</html>

The SSI enabled web page (marylamb.shtml) would look like this in a browser


            Mary had a little lamb, it's fleece was white as snow.

This small example can be extended to include any sized header or footer file. We simply create the header or footer and name it header.txt and call it with the include command <!--#include file="header.txt" -->. While the header and footer can have the extension .txt or .html, the SSI enabled web page must have the .shtml extension in order for the server to parse the page looking for SSI commands. It is a good idea to make the header/footer file a .txt file rather than an .html file. HTML tags can be placed in the header.txt file and the .shtml page will insert the commands into it's page just as if they were part of the original .shtml page. However the file can't then be viewed as a separate web page or by search engines.

<!--#include file="footer.txt" -->