Affordable Web Hosting with Excellent Customer Support internet connection free month hosting

Server Side Includes (SSI)

Server Side Includes (SSI) are tags that are put into an HTML document that require the webserver to parse (scan for commands) the document, perform some function (such as executing a CGI script) and then send the document to the client.

When implemented, SSI can be a real time saver. For example, every document in this support section uses the SSI command include to insert the header images and links (header_support.html). By doing this, any changes that need to be made to the header will be made to one file instead of all of the files in the support area. The value of SSI is apparent in situations such as this.

The first rule of using SSI is that any page containing commands should be named .shtml, .shtm or .sht. By changing the extension, you're letting the webserver know that those files must be parsed before sending them to the client.

The next thing to remember is that all SSI commands must use the following format:

<!--#include virtual="/dir/file.ext"-->

The tag used for SSI is similar to the HTML comment tag which looks like this:

<!-- Comment Tag -->

Common SSI Commands

Config - Echo - Exec - Flastmod - Fsize - Include

Config

The SSI command config allows to you to set output options for other commands.
  • errmsg - Using the errmsg variable of config allows you to change the content of an error message if there is a problem parsing the page. The default error message looks like this:
    [an error occurred while processing this directive]

    You can modify this to display whatever you'd like, for instance <!--#config errmsg="SSI Error, time for a nap!" --> will return:

          SSI Error, time for a nap!
         
  • sizefmt - The sizefmt variable is for use with the SSI fsize command exclusively. By default, This page is <!--#fsize file="SSI_Server_Side_Includes.shtml"--> will display:
    This page is 
           16K
         

    However, by using the bytes variable like this: This page is <!--#config sizefmt="bytes"--><!--#fsize file="SSI_Server_Side_Includes.shtml"--> ,you can change the display to:

    This page is 
          16,840
         

    There is one more variable for sizefmt, abbrev. This page is <!--#config sizefmt="abbrev"--><!--#fsize file="SSI_Server_Side_Includes.shtml"--> will provide an output like this:

    This page is 
           16K
         

    Depending on how your webserver displays file sizes by default, you may or may not need to use abbrev.
     

  • timefmt - Perhaps the best example of the config command is timefmt. Using this variable, you can set how you want a date and/or time to be displayed. By default the webserver will display the current date and time in this manner:
          Tuesday, 24-Oct-2017 07:12:08 EDT
         

    To change the above to this:

          October 24, 2017
         

    You use this tag <!--#config timefmt="%B %d, %Y" --> in conjunction with the tag that displays the actual date, <!--#echo var="DATE_LOCAL" -->.

    For more information on time format commands, check out the SSI Time Format Chart.

Echo

This SSI command allows you to print information determined by the variable you use in conjunction with it. Below is a small list of useful variables. A complete list can be found in the NCSA's SSI Tutorial and on their CGI Environment Variables page.
  • DATE_GMT - Displays the current time on the webserver in Greenwich Mean Time*. For example, It's now <!--#echo var="DATE_GMT" --> displays:
    It's now 
          Tuesday, 24-Oct-2017 11:12:08 GMT
         
  • DATE_LOCAL - Displays the current time on the webserver*. For example, It's now <!--#echo var="DATE_LOCAL" --> displays:
    It's now 
          Tuesday, 24-Oct-2017 07:12:08 EDT
         
  • HTTP_USER_AGENT - Displays what browser client is being used to send the request. For example, You're using <!--#echo var="HTTP_USER_AGENT" --> displays:

    You're using

    CCBot/2.0 (http://commoncrawl.org/faq/)

  • LAST_MODIFIED - Displays the date the page was last updated*. For example, This page last updated <!--#echo var="LAST_MODIFIED" --> displays:
    This page last updated 
          Tuesday, 12-Apr-2011 15:25:45 EDT
         

    This tag works much like flastmod, which is discussed below.

  • QUERY_STRING - Displays the contents of a query string. A query string is information that is attached to a Uniform Resource Locator (URL). Usually a query string is separated from the rest of the URL by a "?". Here's an example of example of a URL with a query string:

    http://www.internetconnection.net/signup.shtml?Domain-Name

    Perhaps one of the best uses for <!--#echo var="QUERY_STRING" --> is in conjuction with a form.

    For example, using Internet Connection's SSL server, you can specify certain form fields to be required. When you do this it is recommended that you specify a "form failure" page to inform your visitors that their form was missing some required information. This "form failure" page could utilize <!--#echo var="QUERY_STRING" --> so that the name of the required form field could be displayed. For more on this, please check out Internet Connection's Secure Sockets Layer (SSL) support document.

*The output of these commands can be altered with the timefmt variable of the SSI command config.

Exec

The SSI command exec allows to you execute a CGI script from within a webpage. For example, the following script checks if the browser accessing this page is Microsoft® Internet Explorer® 5.0. If you're using IE 5.0, it will display text informing you so. If you aren't, it will insetad generate text informing you of that:
 #!/bin/perl
    my $agent = $ENV{HTTP_USER_AGENT};
 if ($agent =~ /MSIE/) {
        print "Content-Type: text/html\n\n";
        print "\<span\ class\=\"code\"\>You\'re\ using\ 
    Microsoft\&reg\;\ Internet\ Explorer\&reg\;\ 5\.0\<\/span\>\n";
        } else {
            print "Content-Type: text/html\n\n";
            print "\<span\ class\=\"code\"\>You\'re\ 
    not\ using\ Microsoft\&reg\;\ Internet\ Explorer\&reg\;\ 5\.0\<\/span\>\n";
        }
    exit 0;

This script is called with this tag <!--#exec cgi="exec-test.cgi" --> and the actual text output appears as such:

Flastmod

The SSI command flastmod works exactly like the echo command when it's used with the variable LAST_MODIFIED, that is, it displays the date and/or time the file was last modified*. For example, This page last updated <!--#flastmod file="SSI_Server_Side_Includes.shtml" --> displays:
	This page last updated
	Tuesday, 12-Apr-2011 15:25:45 EDT

However, flastmod and echo differ in that flastmod allows you to specify the following two attributes. These attributes may be used to display the "last modified" date of other files on the server. This would be valuable in a number of situations, for example if you were creating a site map and wanted to list the date that each file had last been updated.

  • file - allows you to provide a path relative to the directory the parsed document resides in. For example, The support section's main page was last modified <!--#flastmod file="index.shtml" --> would display:
    The support section's main page was last modified 
          Thursday, 27-Jan-2011 10:12:43 EST
         

    You cannot use /, ../ etc. with this attribute.  

  • virtual - allows you to provide a virtual path to the directory the parsed document resides in. For example, The Internet Connection home page was last modified
    <!--#flastmod virtual="../index.shtml" -->
    would display: Internet Connection's home page was last modified Tuesday, 11-Jun-2013 12:28:54 EDT

The output of this command can be altered with the timefmt variable of the SSI command config.

Fsize

The SSI command fsize enables you to display the size of a file in a parsed file. For example, This page is <!--#fsize file="SSI_Server_Side_Includes.shtml"--> will appear as:
This page is  16K
   

The output of this command can be altered with the sizefmt variable of the SSI command config.

Include

Perhaps the most useful SSI command is include. This command enables you to insert the text of another file into the parsed document. For example, to include a file called monkey.txt, you'd use <!--#include text="monkey.txt"-->. The output from this command would look like this: Monkey's are cute. Monkey's have opposable thumbs.

Like the flastmod command, include has two variables that can be used for added flexibility: file - allows you to provide a path relative to the directory the parsed document resides in. Since monkey.txt is in the same directory as this file, you would use <!--#include file="monkey.txt"--> to include it's text.
You cannot use /, ../ etc. with this attribute.
  virtual - allows you to provide a virtual path to the directory the parsed document resides in. The file llama.txt resides in the root directory of the Internet Connection site. To insert its content you would use <!--#include virtual="../../llama.txt"--> which would include this text:

      SSI Error, time for a nap!
     

Related Items

Free On-line Dictionary of Computing's definition of Server Side Includes
NCSA's SSI Tutorial
Another SSI Tutorial
Plenty of SSI Documentation courtesy of The CGI Resource
Yahoo!'s Server Side Scripting Category