ITG Logo










Internetworking 1.3 Header

contents prev: Providing Free Web Services next: Opinions-Spawining a new window
WORKSHOP

Server-Side Includes
Pawan Vora, pvora@uswest.com

I prefer to include at least 2 pieces of information on a Web page: Last modified date and the Web page's URL. But because I create pages from templates, often I forget to change the last modified date and URL. This results in several pages having the same last modified date and the URL. Also, sometimes I prefer to have a left navigation bar that repeats on all the pages of the site. Although having a template helps me, it becomes a nightmare when I have to add an item to or remove an item from the navigation bar, or even reorder the items in the navigation bar. I found a solution to these problems in Server-Side Includes (SSI), which helped me solve my problems without any complicated programing or scripting--with HTML like tags.

To understand SSI better, let's break the term "Server-Side Include" into "Server-Side" and "Include." Server-side means that the processing takes place on the Web server. This also means that you won't see the results if you are not requesting the Web page from a Web server. And, Include means that you include these special tags in your HTML code of the Web page.

This is what an SSI tag looks like:

<!--#echo var="LAST_MODIFIED"-->

As you may notice, it looks like an HTML comment tag. What makes this comment-like tag an SSI tag is the presence of the # sign, which indicates to the Web server that this is a special tag and needs its attention. The server then processes this special tag -- and doesn't ignore it like a COMMENT tag -- and includes the results of the processing in the HTML document. In the above example, the server will include the last modified date of the HTML file being served.

Because the Web server does the processing, you want to make sure that your Web server supports Server-Side Includes before including SSI tags on your Web page. Also, you need to make sure that it is enabled on your Web server. By default, Web servers come with the processing of Server-Side Includes disabled. Not all Web servers refer to this functionality as Server-Side Includes. Some Web servers refer to them as the Parse HTML feature. This is because when SSI is enabled the server must parse (i.e., read) the HTML file before sending it to the client (for example, the user's Web browser).

Furthermore, files using SSIs may need to use a special file extension, usually .shtml, that is set up in the server configuration file. But you can always set up your server to implement SSIs even in files ending in .htm or .html. You may want to check with your server administrator whether it is necessary to change the file extension in your files that utilize SSIs.

Be careful with SSI syntax
As you saw before, an SSI command looks like:

<!--#command parameter="Argument"-->

Notice that the # sign immediately follows <!-- without any intervening spaces. Also note that the argument--the right side of the SSI tag--is enclosed in double quotes. Furthermore, the SSI commands are case-sensitive, meaning that it is stringent about upper and lowercase. If SSI requires that you type LAST_MODIFIED, that's what you type, not last_modified or Last_Modified.

Applications of SSI
(see Appendix A on this page for a list of SSI commands).

  1. Including files
    Probably the most common use of SSI is for including the contents of a document (i.e., file) in another HTML document. For example, if you included the following tag in your content.shtml file,

    <!--#include file="navigation.html"-->

    you would see the contents of navigation.shtml file included in the content.shtml file.

  2. Including Environmental Variables
    Simply stated, environmental variables store the information about the computer system and the files and directories on it. Some of the commonly used environmental variables are:

    Environmental Variable What it represents
    SERVER_NAME Domain name of the Web server
    REMOTE_HOST User (client) computer's domain name
    HTTP_USER_AGENT The user's Web browser
    DATE_GMT Current date and time in Greenwich Mean Time (GMT)
    DATE_LOCAL Current date and time in your local time zone (e.g., MST)
    DOCUMENT_NAME File name of the Web page being displayed in the browser
    DOCUMENT_URL Virtual path to the file relative to the Web server's document root
    LAST_MODIFIED Date and time the current file was modified

    Above SSI commands are used with the syntax <!--#echo var="command"-->. So, for example, to get the last modified date, you will write the SSI tag as follows:

    <!--#echo var="LAST_MODIFIED"-->

  3. Get Information About Files
    There are two SSI commands to help you get information about files:

    1. #fsize, which returns the size of the file
    2. #flastmod, which returns the last time and date the file was modified

    #fsize and #flastmode commands are particularly useful when you are allowing people to download files from your Web sites. If these files are large, it's a good practice to indicate the size of the file and when it was last modified. This way, if the file you are allowing people to download changes in some respect, you don't need to update the Web page to change the file size or last modified date information. #fsize and #flastmode can display this information for you.

    Example:
    Download xxx.exe, size: <!--#fsize file="xxx.exe"-->, last modified on <!--#flastmod file="xxx.exe"-->

  4. Change the format of the information displayed by the Web server
    When you use the SSI tag #flastmod, the default format is: Day, dd-Mon-yy hh:mm:ss (e.g., Sunday, 13-Dec-98 20:21:22). Now, if you want it displayed in mm/dd/yy format, you can use the config timefmt command as follows:

    <!--#config timefmt="%x"--><!--#flastmod file="super_duper.html"-->

    See Appendix B (on this page) for more on timefmt

    You can also use config sizefmt command to change the format in which the file size is displayed to the user. So, the default format to display the file size using #fsize is in nK (e.g., 32K, 100K, etc.). You can configure it to show the size in bytes as follows:

    <!--#config sizefmt="bytes"--><!--#fsize file="super_duper.html"-->

  5. Running programs from within a page itself
    To run a CGI program from the page itself, you can use the SSI tag exec. A common application is to include a CGI program that shows number of hits on a page. Of course, you need to have the CGI program avilable to do so. For example, if you have a counter.cgi program in your cgi-bin directory, you can use it on a page as follows:

    <!--exec cgi="/cgi-bin/counter.cgi"-->

    You can also run Shell commands using the exec tag. For example, to display the calendar on a UNIX-based Web server, you can use:

    <pre><!--exec cmd="cal"--></pre>

    In the above example, I have used <pre> to make sure that the calendar displays properly in a mono-spaced font.

    Your Webmaster may not like using exec SSIs because they can compromise the security of the Web server. Allowing use of exec makes it possible for a hacker to execute potentially destructive commands within the HTML page itself.

Common mistakes with the use of SSI

  • Forgetting the pound (#) sign. If you forget the # sign, the Web server interprets it as a regular HTML comment and ignores it.
  • Using "smart" or "curly" quotes
  • Upper and lowercase problems. Make sure that you type the commands as they appear in the list below. Otherwise, you will either get an error or the server may treat the SSI command as the comment (especially when the first character after <-- is not a #.
  • Typos and spaces. The most obvious and the most common mistake is because of the typos and missing or extra spaces. Often, extra spaces show up before and after the '='.
  • Missing double-quotes. Make sure that the right side of the SSI command (after =) is enclosed in double-quotes.
  • HTML parsing not enabled. Make sure that your Web server supports Server-Side Includes (or HTML parsing) and that it is enabled. Otherwise, the Web server will treat it as an HTML comment and ignore it. If your SSI command is correct, and you don't get an error message, the very likely reason is that the SSI is not enabled on the Web server.
  • Not making a server request when requesting a page with SSI commands. If you simply open the page with SSI commands in a browser without making the server request, you will not see any errors since the browser will treat the SSI commands as comments. Remember, SSI means Server-Side Include, so the page must be served by a Web server.
  • One mistake many people make in using SSI is when embedding the SSI tags such as LAST_MODIFIED and DOCUMENT_URI in the "included" file. It's better to explain this with an example. Suppose you had an HTML file called document_info.shtml, in which you wanted to include some copyright information, last modified date, and the URL. So, you created a file, footer.shtml, as follows:

    © XYZ Corporation
    <!--#echo var="LAST_MODIFIED"-->
    <!--#echo var="DOCUMENT_URI"-->

    Although it seems to be okay and you don't get any errors, what you will see included in the document_info.shtml file is the last modified date of the file footer.shtml and the path to the file footer.shtml insted of that of document_info.shtml. So, note that these tags are file specific and should be included in the main file, in this case in document_info.shtml file.

List of commonly used SSI Commands

  • <!--#include file="footer.html"-->
    To include a file with a relative path. Can not refer to a file above the current directory.

  • <!--#include virtual="/ssi/footer.html"-->
    To include a file with an absolute path from the document root. The paths must begin with a "/".

  • <!--#echo var="LAST_MODIFIED"-->
    Displays the date and time the file was last modified.

  • <!--#echo var="DOCUMENT_NAME"-->
    Displays the file name.

  • <!--#echo var="REMOTE_HOST"-->
    Displays the user (client) computer's domain name (e.g., www.sandia.gov).

  • <!--#echo var="REMOTE_ADDR"-->
    Displays the IP address (e.g., 209.181.87.94).

  • <!--#echo var="HTTP_USER_AGENT"-->
    Displays the user (client) browser information (e.g., Mozilla/4.03 [en]C-USWC0810 (WinNT; U).

  • <!--#echo var="HTTP_HOST"-->
    Displays the Web server's domain name (www.uswest.com).

  • <!--#echo var="DATE_GMT"-->
    Displays the current date and time in Greenwich Mean Time (GMT).

  • <!--#echo var="DATE_LOCAL"-->
    Displays the current date and time in the local time zone.

  • <!--#echo var="DOCUMENT_URI"-->
    Displays the path of the file from the Web server's document root (e.g., /itg/internetworking/dec98/ssi_workshop.html).

  • <!--#echo var="SERVER_NAME"-->
    Displays the domain name of the Web server (e.g., www.sandia.gov).

  • <!--#echo var="SERVER_PORT"-->
    Displays the server port number (e.g., 80).

  • <!--#echo var="SERVER_SOFTWARE"-->
    Displays the Web server software (e.g., Netscape-Enterprise/3.5.1).

  • <!--#echo var="SERVER_URL"-->
    Displays the base URL of the Web server including the protocol (e.g., http://www.sandia.gov).

  • <!--#flastmod file="contents.shtml"-->
    Displays the last modified date of the file. The file path is shown as a relative path.

  • <!--#flastmod virtual="/itg/newsletter/contents.shtml"-->
    Displays the last modified date of the file. The file path is specified as an absolute path from the Web server's document root.

  • <!--#fsize file="contents.shtml"-->
    Displays the size of the specified file. The file path is specified as a relative path. (e.g., 3K)

  • <!--#fsize virtual="/itg/newsletter/contents.shtml"-->
    Displays the size of the specified file. The file path is specified as an absolute path from the Web server's document root. (e.g., 3K)

  • <!--#config timefmt="%x"--><!--#flastmod file="contents.shtml"-->
    Displays the time in mm/dd/yy format.

  • <!--#config timefmt="%A, %B %e, %Y"--><!--#flastmod file="contents.shtml"-->
    Displays the time format in Day, month date, year format.

  • <!--#config sizefmt="bytes"--><!--#fsize file="contents.shtml"-->
    Displays the size of the file in bytes (instead of Kilobytes).

  • <!--#config sizefmt="abbrev"--><!--#fsize file="contents.shtml"-->
    Displays the size of the file either in kilobytes (K) or megabytes (MB).

  • <pre><!--#exec cmd="cal"--></pre>
    Displays the results of the UNIX command cal. PRE is used to format the displays in mono-spaced font.

Appendix B: List of formating modifiers for Displaying Date
(to be used with config timefmt)

Modifier What does it display? Example
%A Day of the week Wednesday
%a Day of the week (abbreviated) Wed
%B Month of the year January
%b Abbreviated month of year Jan
%c Date & time,
formatted as per local preferences
Sun Dec 13 01:10:16 1998
%d Day of the month 07
%H Hour of the day (24 hour clock) 22
%I Hour of the day (12-hour clock) 10
%j Day of year 350
%M Minutes past the hour 10
%m Month of year (01-12) 12
%p AM or PM PM
%S Seconds past the minute 43
%U Week of the year (00-53)
(with Sunday as the first day of the week)
50
%W Week of the year (00-53)
(With Monday as the first day of the week)
49
%w Weekday as a decimal number (0-6)
(with Sunday as the first day of the week)
06
%X Time, as formatted per local preferences 01:10:16
%x Date, as formatted per local preferences 12/07/98
%Y Year, 4 digits 1998
%y Year, 2 digits 98
%Z Time zone (abbreviated) EST

References

contents prev: Providing Free Web Services next: Opinions-Spawining a new window

© Internet Technical Group
Last update: December 5, 1998
URL: http://www.sandia.gov/itg/newsletter/dec98/ssi.html
hosted by Sandia National Labs

Disclaimer: Neither Sandia Corporation, the United States Government, nor any agency thereof, nor any of their employees makes any warranty, express or implied, or assumes any legal liability or responsibility for the accuracy, completeness, or usefulness of any information, apparatus, product, or process disclosed, or represents that its use would not infringe privately-owned rights. Reference herein to any specific commercial product, process, or service by trade name, trademark, manufacturer, or otherwise does not necessarily constitute or imply its endorsement, recommendation, or favoring by Sandia Corporation, the United States Government, or any agency thereof. The views and opinions expressed herein do not necessarily state or reflect those of Sandia Corporation, the United States Government or any agency thereof.