Server Side Includes

From Seo Wiki - Search Engine Optimization and Programming Languages

Jump to: navigation, search

Server Side Includes (SSI) is a simple interpreted server-side scripting language used almost exclusively for the web.

The most frequent use of SSI is to include the contents of one or more files into a web page on a web server. For example, a web page containing a daily quote could include the quote by placing the following code into the file of the web page:

<!--#include virtual="../quote.txt" -->

With one change of the quote.txt file, all pages including the file will display the latest daily quote. The inclusion is not limited to files. In this very example, on a server that can run fortune, the output of it, a randomly selected quote, can also be included.

Server Side Includes are useful for including a common piece of code throughout a site, such as a page header, a page footer and a navigation menu. Conditional navigation menus can be conditionally included using control directives.

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 server is configured to allow this, set the execution bit of the file.

As a simple programming language, SSI support 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.

Apache, lighttpd and IIS are the major three web servers that support this language.


Basic syntax

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. Note that the syntax does does not allow spaces between the leading "<" and the directive. [1]


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"-->
<!--#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" -->
<!--#config sizefmt="bytes" -->
<!--#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"-->
<!--#fsize file=""-->
printenv   This directive outputs a list of all variables and their values, including environmental and user-defined variables. It has no attributes. <!--#printenv -->

Control directives

Directive Parameters Description Example
if expr Used for condition tests that may determine and generate multiple logical pages from one single physical page. <!--#if expr="${Sec_Nav}" -->
<!--#include virtual="" -->
<!--#endif -->
elif expr Serves the same purpose as further conditioning in programming languages. <!--#if expr="${Sec_Nav}" -->
<!--#include virtual="secondary_nav.txt" -->
<!--#elif expr="${Pri_Nav}"-->
<!--#include virtual="primary_nav.txt" -->
<!--#endif -->
else If none of the if and elif directive catches the present condition, things in here should happen. <!--#if expr="${Sec_Nav}" -->
<!--#include virtual="secondary_nav.txt" -->
<!--#else -->
<!--#include virtual="article.txt" -->
<!--#endif -->
endif Termination of a conditional construct. See above for example.
set var, value Sets the value of a SSI variable. (Not supported by all implementations) <!--#set var="foo" value="bar" -->

The <!--#set --> command is supported in both Apache httpd and lighttpd.

Client Side Includes

Client Side Includes are HTML includes achieved on the client-side through use of frames, object inclusion, IFrames, JavaScript, or JavaScript with Ajax requests. Compared to server-side includes, they have the advantage that they can include content from external domains, and can also improve page loading performance by some measures. However, client-side includes rely on the client's support of their respective technologies and, in the case of frames and iframes, are less accessible.

See also


  1. "Basic SSI Directives and Syntax". 2010-01-20. Retrieved 2010-01-20. 

External links

Personal tools

Served in 0.173 secs.