Server Side Includes (SSI) is a simple server-side scripting language used almost exclusively for the web. As its name implies, its primary use is including the contents of one file into another one dynamically when the latter is served by 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, .html, .txt, etc.) containing a daily quote could be included into multiple SSI-enabled pages throughout a website by placing the following code into the desired pages:
<!--#include virtual="../quote.txt" -->
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 recognize an SSI-enabled HTML file and therefore carry out these instructions, the file must end with the .shtml, .stm or .shtmextension. (It is also possible to configure a web server to process files with extension .html.)
SSI is most suitable for simple automatization tasks; more complex server-side processing is often done with one of the more complex programming languages Perl, PHP, ASP, JSP, CFML, Python and Ruby.
SSI has a simple syntax: <!--#directive parameter=value parameter=value -->. Directives are placed in HTML comments so that if SSI isn't enabled, users won't see the SSI directives on the page, unless they look at its source.
Directives
Most common directives
Directive
Parameters
Description
Example
include
file, direct 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 HTTPenvironment 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 -->
Advanced directives
Directive
Parameters
Description
Example
if
expr
Used for condition tests that may determine and generate multiple logical pages from one single physical page.
Client Side Includes are HTML includes achieved on the client-side through use of frames, IFrames, JavaScript, or JavaScript with Ajax requests. These methods suffer from shortcomings not present in server-side includes: they rely on the client's support of their respective technologies and, in the case of frames and iframes, are less accessible.