Server Side Includes


Server Side Includes is a simple interpreted server-side scripting language used almost exclusively for the World Wide Web. It is most useful for including the contents of one or more files into a web page on a web server, using its #include directive. This could commonly be a common piece of code throughout a site, such as a page header, a page footer and a navigation menu. SSI also contains control directives for conditional features and directives for calling external programs. It is supported by Apache, LiteSpeed, nginx, IIS as well as W3C's Jigsaw. It has its roots in NCSA HTTPd.
In order for a web server to recognize an SSI-enabled HTML file and therefore carry out these instructions, either the filename should end with a special extension, by default .shtml, .stm, .shtm, or, if the server is configured to allow this, set the execution bit of the file.

Design

As a simple programming language, SSI supports only one type: text. Its control flow is rather simple, choice is supported, but loops are not natively supported and can only be done by recursion using include or using HTTP redirect. The simple design of the language makes it easier to learn and use than most server-side scripting languages, while complicated server-side processing is often done with one of the more feature-rich programming languages. SSI is Turing complete.
SSI has a simple syntax: <!--#directive parameter=value parameter=value -->. Directives are placed in HTML comments so that if SSI is not enabled, users will not see the SSI directives on the page, unless they look at its source. Note that the syntax does not allow spaces between the leading "<" and the directive. Apache tutorial on SSI stipulates the format requires a space character before the "-->" that closes the element.

Examples

A web page containing a daily quotation could include the quotation by placing the following code into the file of the web page:


With one change of the quote.txt file, all pages that include the file will display the latest daily quotation. The inclusion is not limited to files, and may also be the text output from a program, or the value of a system variable such as the current time.

Directives

Common

The following are SSI directives from the times of NCSA HTTPd. They are supported by all implementations.
DirectiveParametersDescriptionExample
includefile or virtualThis is probably the most used SSI directive, allowing the content of one document to be transcluded in another. The file or virtual parameters specify the file to be included. Includes the contents of another file or the result of running a CGI script. If the process does not have access to read the file or execute the script, the include will fail. "virtual" specifies the target relative to the domain root, while "file" specifies the path relative to the directory of the current file. When using "file" it is forbidden to reference to absolute paths. Higher directories are usually forbidden, unless explicitly configured. The Apache documentation recommends using "virtual" in preference to "file".

execcgi or cmdThis 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, as a result "exec cgi" should be used instead of "include virtual".

echovarThis directive displays the contents of a specified HTTP environment variable. Variables include HTTP_USER_AGENT, LAST_MODIFIED, and HTTP_ACCEPT.
Your IP address is:
configtimefmt, sizefmt, or errmsgThis directive configures the display formats for the date, time, filesize, and error message.

flastmod and fsizefile or virtualThese 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.

Control directives

Control directives are later added to SSI. They include the ubiquitous if-elif-else-endif flow control and variable writing as well as more exotic features like loops only found in some implementations.
DirectiveParametersDescriptionExampleFound in
exprThe if statement. Used for condition tests that may determine and generate multiple logical pages from one single physical page. elif is a shorthand for nested else-if. else and endif do not accept parameters.
Expression syntax vary among implementations. Variable existence and equality/regex checks are commonly supported. Jigsaw uses expressions split over multiple attributes instead.

Ubiquitous.
setvar, valueSets the value of a SSI variable. Apache provides additional parameters for encodings.
Apache, Nginx
printenv This directive outputs a list of all SSI variables and their values, including environmental and user-defined variables. It has no attributes.
Apache