KB / Web Programming / CGI, PHP, and Databases / SSI: Server-Side Includes

From DreamHost

Jump to: navigation, search

On our servers, you can use SSI (server-side includes) in any file with a .shtml extension.

You can over-ride this requirement and use SSI in .html files by following these instructions, however this isn't recommended since SSI files are slightly slower to serve than pure static HTML pages, and if you turn on SSI in all your .html files you'll probably be doing a lot of unnecessary parsing of files that don't even have any SSI in them!

For information on how to use SSI, check out the article below!

Contents

Parent Article

Using server side includes.

I've heard that server side includes (SSI) allow you to dynamically insert content in the middle of a page. This sounds great - how do I do it?

It's easy! Here's how:

1. First, create your HTML files as usual. [how do I do that?] Server side includes are embedded into the content of a normal html page. [what's html?]

2. Rename the HTML file that will contain the included content (ie: whatever.html) to end in ".shtml" (ie: whatever.shtml).

Note that you may have to alter links to that page, as changing the name will break any links made to it. If the parent web page does not have the .shtml extension, server side includes will not work.

3. Now, add some special commands to the page into which you want to dynamically add content. This page is parsed as usual - as a normal HTML document; the special commands are embedded as SGML comments. A command has the syntax:

<!--#element attribute=value attribute=value ... -->

The value will often be enclosed in double quotes, and many commands only allow a single attribute-value pair. Note that the comment terminator (-->) should be preceded by whitespace to ensure that it isn't considered part of an SSI token. A single space should be sufficient.

Here are some basic elements you may run across:

  • Config - Controls various parts of the parsing of your document.
  • Exec - Executes a shell command or cgi script.
  • Fsize - Prints the size of the specified file.
  • Flastmod - Prints the last modification date of the specified file.
  • Include - Inserts the text of another document or file into the parsed file.
  • Printenv - Prints out a listing of all exisiting variables and their values.
  • Set - Sets the value of a variable.


Other elements:

Basic elements

config

config

This command controls various aspects of the parsing. The valid attributes are:

errmsg
The value is a message that is sent back to the client if an error occurs whilst parsing the document.
sizefmt
The value sets the format to be used which displaying the size of a file. Valid values are bytes for a count in bytes, or abbrev for a count in Kb or Mb as appropriate.
timefmt
The value is a string to be used by the strftime(3) library routine when printing dates.

echo

This command prints one of the include variables, defined below. If the variable is unset, it is printed as (none). Any dates printed are subject to the currently configured 'timefmt' element. The only valid attribute is:

var
The value is the name of the variable to print.

exec

exec

The exec command executes a given shell command or CGI script. The IncludesNOEXEC Option disables this command completely. Its valid attributes are:

cgi
The value specifies a (%-encoded) URL relative path to the CGI script. If the path does not begin with a (/), then it is taken to be relative to the current document. The document referenced by this path is invoked as a CGI script, even if the server would not normally recognize it as such. However, the directory containing the script must be enabled for CGI scripts (with ScriptAlias or the ExecCGI Option).
The CGI script is given the PATH_INFO and query string (QUERY_STRING) of the original request from the client; these cannot be specified in the URL path. The include variables will be available to the script in addition to the standard CGI environment.
If the script returns a Location: header instead of output, then this will be translated into an HTML anchor.
The include virtual element should be used in preference to exec cgi.
cmd
The server will execute the given string using /bin/sh. The include variables are available to the command.

fsize

fsize

This command prints the size of the specified file, subject to the sizefmt format specification. Attributes:

file
The value is a path relative to the directory containing the current document being parsed.
virtual
The value is a quoted and %-encoded URL-path relative to the current document being parsed. If it does not begin with a slash (/) then it is taken to be relative to the current document.

flastmod

flastmod

This command prints the last modification date of the specified file, subject to the timefmt format specification. The attributes are the same as for the fsize command.

include

include

This command inserts the text of another document or file into the parsed file. Any included file is subject to the usual access control. If the directory containing the parsed file has the Option IncludesNOEXEC set, and the including the document would cause a program to be executed, then it will not be included; this prevents the execution of CGI scripts. Otherwise CGI scripts are invoked as normal using the complete URL given in the command, including any query string.

An attribute defines the location of the document; the inclusion is done for each attribute given to the include command. The valid attributes are:

file
The value is a path relative to the directory containing the current document being parsed. It cannot contain ../, nor can it be an absolute path. The virtual attribute should always be used in preference to this one.
virtual
The value is a quoted and %-encoded URL relative to the current document being parsed. The URL cannot contain a scheme or hostname, only a path and an optional query string. If it does not begin with a slash (/) then it is taken to be relative to the current document. A URL is constructed from the attribute, and the output the server would return if the URL were accessed by the client is included in the parsed output. Thus included files can be nested.

     For example: <!--#include virtual="/includes/navbar.html" -->

printenv

printenv

This prints out a listing of all existing variables and their values. No attributes.

     For example: <!--#printenv -->

set

set

This sets the value of a variable. Attributes:

var
The name of the variable to set.
value
The value to give a variable.

     For example: <!--#set var="category" value="help" -->

Other Elements

Include Variables

In addition to the variables in the standard CGI environment, these are available for the echo command, for if and elif, and to any program invoked by the document.

DATE_GMT

The current date in Greenwich Mean Time.

DATE_LOCAL

The current date in the local time zone.

DOCUMENT_NAME

The filename (excluding directories) of the document requested by the user.

DOCUMENT_URI

The (%-decoded) URL path of the document requested by the user. Note that in the case of nested include files, this is not then URL for the current document.

LAST_MODIFIED

The last modification date of the document requested by the user.

Variable Substition

Variable substitution is done within quoted strings in most cases where they may reasonably occur as an argument to an SSI directive. This includes the config, exec, flastmod, fsize, include, and set directives, as well as the arguments to conditional operators. You can insert a literal dollar sign into the string using backslash quoting:

     <!--#if expr="$a = $test" -->

If a variable reference needs to be substituted in the middle of a character sequence that might otherwise be considered a valid identifier in its own right, it can be disambiguated by enclosing the reference in braces, ࠬa shell substitution:

     <!--#set var="Zed" value"${REMOTE_HOST}_${REQUEST_METHOD}" -->

This will result in the Zed variable being set to "X_Y" if REMOTE_HOST is "X" and REQUEST_METHOD is "Y".

EXAMPLE: the below example will print "in foo" if the DOCUMENT_URI is /foo/file.html, "in bar" if it is /bar file.html and "in neither" otherwise:

     <!--#if expr="\"$DOCUMENT_URI\" = \"/foo/file.html\"" -->      in foo      <!--#elif expr="\"$DOCUMENT_URI\" = \"/bar/file.html\"" -->      in bar      <!--#else -->      in neither      <!--#endif -->

Flow Control Elements

These are available in Apache 1.2 and above (which our servers run). The basic flow control elements are:

     <!--#if expr="test_condition" -->      <!--#elif expr="test_condition" -->      <!--#else -->      <!--#endif -->

The if element works like an if statement in a programming language. The test condition is evaluated and if the result is true, then the text until the next elif, else, or endif element is included in the output stream.

The elif or else statements are be used the put text into the output stream if the original test_condition was false. These elements are optional.

The endif element ends the if element and is required.

test_condition is one of the following:

string

true if string is not empty

string1 = string2 string1 != string2

Compare string1 with string 2. If string2 has the form /string/ than it is compared as a regular expression. Regular expressions have the same syntax as those found in the Unix egrep command.

( test_condition )

true if test_condition is true

! test_condition

true if test_condition is false

test_condition1 && test_condition2

true if both test_condition1 and test_condition2 are true

test_condition1 || test_condition2

true if either test_condition1 or test_condition2 is true

"=" and "!=" bind more tightly than "&&" and "||". "!" binds most tightly. Thus, the following are equivalent:

     <!--#if expr="$a = test1 && $b = test2" -->      <!--#if expr="($a = test1) && ($b = test2)" -->

Anything that's not recognized as a variable or an operator is treated as a string. Strings can also be quoted: 'string'. Unquoted strings can't contain whitespace (blanks and tabs) because it is used to separate tokens such as variables. If multiple strings are found in a row, they are concatenated using blanks. So,

     string1 string2 results in string1 string2      'string1 string2' results in string1 string2 </ol>

Personal tools