While standard HTML files are fine for storing pages, it
is very useful to be able to create some content dynamically.
For example, to add a footer or header to all files, or to
insert document information such as last modified times automatically.
This can be done with CGI, but that can be complex and requires
programming or scripting skills. For simple dynamic documents
there is an alternative: server-side-includes (SSI).
SSI lets you embed a number of special 'commands' into the
HTML itself. When the server reads an SSI document, it looks
for these commands and performs the necessary action. For
example, there is an SSI command which inserts the document's
last modification time. When the server reads a file with
this command, it replaces the command with the appropriate
time.
SSI Implementation: All Webtechs Internet hosted sites
come with SSI preinstalled. To make an HTML page using
SSI name the page .shtml This signals the server to look for
embedded SSI tags described below. If the page is not named
.shtml the server will ignore the tags as comments.
SSI Commands: All SSI commands are stored within
the HTML in HTML comments. For a list of commands visit the
NCSA
tutorial site. A typical SSI command looks like this:
<!--#flastmod file="this.html" -->
In this case the command is flastmod, which means output
the last modified time of the file given. The arguments specify
the file "this.html" (which might be the name of
the file containing this command). The whole of the command
text, including the comment marker <!-- and --> will
be replaced with the result of this command. In general, all
commands take the format:
<!--#command arg1="value1 arg2="value2
... -->
where arg1, arg2, etc are the names of the arguments and
value1, value2 etc are the values of those arguments. In the
flastmod example, the argument is 'file' and it's value is
'this.html'. Often commands can take different argument names.
For example, flastmod can be given a URL with the argument
virtual, to get the last modified time from the server. For
example, use:
<!--#flastmod virtual="/" -->
to get the last modification time of the home page on the
server (this is useful if the page being accessed might have
a different file name, for instance).
Besides flastmod, there are SSI commands which get the size
of a file or URL, the contents of a variable (passed in by
the server), the contents of another file or URL, or the result
of running a local file. These are documented in the NCSA
tutorial on server side includes.
When SSI commands are executed, a number of 'environment
variables' are set. This include the CGI variables (REMOTE_HOST
etc), and some more, such as DOCUMENT_NAME and LAST_MODIFIED.
These can be output with the echo command (so a better way
of getting the last modification time of the current file
would be <!--#echo var="LAST_MODIFIED" -->).
Examples: Here are some examples of using SSI
Displaying document information: The following code puts
the document modification time on the page:
Last modified: <!--#echo var="LAST_MODIFIED"
-->
Adding a footer to many documents: Add the following text
to the bottom of each of the documents:
<!--#include file="footer.html" -->
Hide links from external users: Use the if command and the
REMOTE_ADDR CGI variable to see if the user is in the local
domain:
<!--#if expr="$REMOTE_ADDR = /^1.2.3./"
-->
<a href="internal-documents.html">Internal
Documents</a>
<!--#endif -->
(Where 1.2.3 is the IP address prefix of the local domain).