RadianT tutorials, tips and tricks
<%PostCommentPage%> <%PhotoAlbumPage%> <%ProfilePage%>

SSI - Server Side Includes

Posted on 2007-Aug-26 at 12:19 in Web programming
Server side includes are directives that you can place in your HTML or other Web documents to execute scripts or generate output, such as environment variables or file statistics. SSIs are an Apache feature implemented by mod_include Apache module.

It allows placement of special codes in documents that will carry out certain actions as specified. SSI lets you add dynamically generated content to an existing HTML page, without having to serve the entire page via a CGI program or other dynamic programming.

When a page is sent from the server to the requesting client the page is scanned by the server for a SSI code and if found, carries out the required actions. SSI is a great way to add small pieces of information, such as the current time or server rotating banner ads. Typically files with the extension .shtml are processed by the server for SSI. This can be easily modified though in the Apache httpd.conf file or even in any given directory using .htaccess files.

The most used application of SSI is to include content which is common to multiple web pages. It can be placed in a single document and then included on all the pages where it is desired. To make any changes you only need to modify the source document. But SSI can also be used for displaying document information such as document name, URI, last modified date or file size. Using the echo command you can echo environmental variables, user information such as IP address or browser information and more.

You can also redirect visitors to a page depending on their browser version, or check what country they are from and display a custom message in their language or redirect them to a specific page. You can also query databases, send emails. You can do all this even without using any CGI script.

Configuring your server to allow SSI

To allow SSI on your server, you can use several methods. One method is to set this directive in your httpd.conf file:

AllowOverride All

and set the Options directive:

Options Indexes FollowSymLinks Includes

or place this directive in an .htaccess file:

Options +Includes

This tells Apache that you want to permit files to be parsed for SSI directives. Most configurations contain multiple Options directives that can override each other. You will probably need to apply the Options directive to the specific directory where you want SSI enabled in order to assure that it gets evaluated last.

Setting files to be parsed

You have to tell Apache which files may be parsed. You can do this by modifying the Apache httpd.conf file or you can add specific commands to an .htaccess file in a given directory. Use the following directives to provide multiple file types SSI parsing capability:

AddType text/html .shtml .html .htm
AddOutputFilter INCLUDES .shtml .html .htm

You can also use the XBitHack directive:

XBitHack On

This tells Apache to parse files for SSI directives if they have the eXecute bit set.

Or you can use

XBitHack Full

This tells Apache to determine the last modified date by looking at the date of the originally requested file, ignoring the modification date of any included files.

To add SSI directives to an existing page, you then need to make the file executable using Chmod.

chmod +x pagexxx.html (or use your FTP client to change the file permissions)

The XBitHack option provides a faster solution in many cases. Apache does not have to read through every file that it sends to clients, including those that don't have any SSI directives to process.

SSI directives

SSI directives are formatted like HTML comments and have the following syntax:

 

Some examples:

Show today's date:

You can use the config element, with a timefmt attribute, to modify the formatting:

 Include CGI results in a page:

Last modified date:

Use an include file for a header or a footer:

Execute a command:

 - this will show the directory listing

Show the size of a specified file in bytes:

The size of this image is:

Output a list of all variables and their values:

Advanced SSI applications

You can set variables using the Set directive, or use environment variables to pass values into variables. You can use conditional statements with structures such as if, elif, else, and endif. For a full list of comparison operators study the mod_include documentation.

 


Last Page | Page 22 of 23 | Next Page
RadianT's blog offers informational items, such as tips and tricks on many computer related subjects.

Links

- Home
- Archives
- RSS Feed