SSI -- Server Side Include
1.Introduction -- What is SSI
Server Side Includes or SSI is an easy server-side scripting language used almost exclusively for the web. As its name implies, its primary use is including the contents of a file into another, via a Web Server.
SSI is primarily used to "paste" the contents of one or more files into another. For example, a file (of any type, .htm, .txt, etc.) containing a daily quote, could be included into multiple SSI Enabled pages throughout a website, by placing the following code
<!--#include virtual="../quote.txt" -->
into the desired pages. With one change of the quote.txt file, pages including the snippet will display the latest daily quote. Server Side Includes are useful for including a common piece of code throughout a site, such as a navigation menu.
In order for a web server in a default configuration to recognise a SSI-enabled HTML file and therefore carry out these instructions, the file must end with the .shtml extension. SSI files can also end with .shtm but this depends on the servers ability to recognise the extension. It is possible to configure a web server to recognise any file with the .html file extension for server side include processing.
2. Basic Syntax and Directives
These are the most common SSI directives:
include
file or virtual
This is probably the most used SSI directive, allowing the content of one document to be included in another. The file or virtual parameters specify the file (HTML page, text file, script, etc) to be included. The file parameter defines the included file as relative to the document path; the virtual parameter defines the included file as relative to the document root.
<!--#include virtual="header.html"-->
exec
cgi or cmd
This directive executes a program, script, or shell command on the server. the cmd parameter specifies a server-side command; the cgi parameter specifies the path to a CGI script. The PATH_INFO and QUERY_STRING of the current SSI script will be passed to the CGI script. "include virtual" should be used instead of "exec cgi".
<!--#exec cgi="/cgi-bin/foo.cgi"-->
or
<!--#exec cmd="ls -l"-->
echo
var
This directive displays the contents of a specified HTTP environment variable. Variables include HTTP_USER_AGENT, LAST_MODIFIED, and HTTP_ACCEPT.
<!--#echo var="REMOTE_ADDR" -->
config
timefmt, sizefmt, or errmsg
This directive configures the display formats for the date, time, filesize, and error message (returned when an SSI command fails).
<!--#config timefmt="%y %m %d" -->
or
<!--#config sizefmt="bytes" -->
or
<!--#config errmsg="SSI command failed!" -->
flastmod or fsize
file or virtual
These directives display the date when the specified document was last modified, or the specified document's size. The file or virtual parameters specify the document to use. The file parameter defines the document as relative to the document path; the virtual parameter defines the document as relative to the document root.
<!--#flastmod virtual="index.html"-->
or
<!--#fsize file="script.pl"-->
printenv
This directive outputs a list of all variables and their values, including environmental and user-defined variables. It has no attributes.
<!--#printenv -->
3. Practical Usage
Today's date
<!--#echo var="DATE_LOCAL" -->
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.
<!--#config timefmt="%A %B %d, %Y" -->
Today is <!--#echo
var="DATE_LOCAL" -->
Modification date of the file
This document last modified <!--#flastmod file="index.html" -->
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.''
<!--#include virtual="/cgi-bin/counter.pl" -->
Notice: You need to turn on the SSI support on the server side in advance, otherwise you'll get a bunch of blank pages!
Referrence:



