Using SSI to Boost Efficiency - Simple SSI Directives
(Page 3 of 7 )
As the name implies, SSI allows a web designer to insert the contents of one file into another when the file is served up. Other information can also be inserted, including file modification dates, file sizes and values of server environment variables. The general form of all directives is:
<!--#element attribute=value attribute=value ... -->
Because it's wrapped in a comment, if your server isn't configured to support SSI, the content will be exported normally and your browser will treat it just like any other HTML comment. If you're using a syntax checking editor, this will prevent it from choking on your directive.
Including Files
To include one file in the output of another, use the #include directive:
<!--#include virtual="/virtual/url/to/file" -->
If you've used SSI before or if your working on IIS, the "virtual" attribute may look a little funny to you. It isn't the file system path to a file but is a URL on the local server to the file. This makes your SSI files easier to move around on the file system and from server to server. The "file" attribute is still supported but "virtual" is greatly preferred. If you persist in using "file", you may run into trouble when you move your site from the development server to production.
File Modification Time
File modification times are useful when you're offering files for download. When the new file is uploaded to the server, the web page will automatically reflect the new modification time.
<!--#flastmod file="download.zip" -->
This directive can also be used to mark a page with its own last modification date by including the file name as the "file" directive. While there are other ways to handle this, such as a version control system, it's a pretty good work around for providing the information.
Executing Commands
The output of programs run on the server can also be included. The classic example is to produce a file listing. I've also used it for web page counters and for generating dynamic menus on my web site.
<!--#exec cmd="menu.pl" -->
The command is executed in the system's default shell and inherits all of the environment variables available to a CGI program. This feature is potentially very dangerous and should be disabled site wide if users can contribute content to the web site, such as via a guest book. For Apache, adding IncludeNOEXEC to the Options directive where you enabled SSI. For IIS, the feature is disabled by default and must be turned on.
Displaying Environment Variables
You can display any environment variable using the following directive:
<!--#echo var="variable" -->
It's useful for displaying any of the variables which are available to CGI programs.
Setting Environment Variables
It's useful to be able to set environment variables. You can use it to set certain options in CGI programs called by the #exec directive. You can also set variables in one file that are displayed in another included file using the #echo directive.
<!--#set var="name" value="value" -->
Unfortunately you can't use this feature on IIS. That means one of the examples coming up won't be useful for IIS users.
Next: How to Use SSI >>
More HTML Articles
More By Clay Dowling