Things You Didn't Know About SSI
Server Side Includes (SSI) are tags that can be put
into web pages that tell the server to include certain
content. The content is included before the page is
sent to the browser.
You probably already knew that.
Content commonly included in web pages are static files
and content generated by CGI programs.
You probably already knew that, too. However, if you're
unclear about the common SSI tags and what they can do,
or feel the need for a quick refresher, see "Server Side
Includes" linked from /archives
Following are some less common uses of SSI tags; some may
may be new to you.
Publishing the Current Date and Time
This can be done with JavaScript, too, so long as the
browser being used is JavaScript enabled. The SSI method,
however, works with all browsers.
This is the SSI tag to use:
<!--#echo var="DATE_LOCAL" -->
Where that tag is put, the current date and time will be
inserted.
Probably you'll want to format that date and/or time to
something more pleasing. The way to do that is to put a
date/time format tag somewhere before the data/time
publication tag.
The date/time format tag is something like this:
<!--#config timefmt="%A, %B %d, %Y at %I:%M:%S %p"-->
Now, when a date/time publication SSI tag is encountered,
it will be formatted as, for example:
Sunday, March 27, 2005 at 04:09:35 PM
The formatting is done by specifying codes that begin with
a % character.
The %A means print the day of the week, the %B the month
name, and so forth. A complete list of formatting codes
is at http://unixhelp.ed.ac.uk/CGI/man-cgi?strftime
under the "DESCRIPTION" section. For this article, only
the list of % codes and descriptions are pertinent.
Publishing the Date and Time a File Was Last Modified
To publish the date/time the current web page was last
modified, use this SSI tag:
<!--#echo var="LAST_MODIFIED"-->
A date/time format SSI tag may be specified before the
above last modified tag.
If more than one date/time format SSI tag is in the
web page, the one immediately prior to the date/time
publishing tag will be used. Therefore, you can have
several dates on a web page, the current time and also
the date the current web page was last modified, for
example, each date/time formatted independently.
To publish the date/time other files on your server were
last modified, use this SSI tag:
<!--#flastmod file="index.html"-->
Replace file name "index.html" with the file name of the
one you want the date of. To specify a file name in a
different directory, specify the server path to the
current web page. Example:
<!--#flastmod virtual="books/specials.html"-->
Notice that the key word for the file name is "file" when
the file is in the current directory and is "virtual"
when the file contains the server path.
Publishing a File Size
To publish the size of a file on the server, use one of
the following SSI tags:
<!--#fsize file="index.html"-->
<!--#fsize virtual="books/specials.html"-->
The first example is for files in the current directory.
The second is for files in other directories.
Publishing a Whole Lot of Information About the Visitor
This could be alarming for folks who are more privacy
conscious than the norm, and who aren't familiar with the
variety of information their browser provides to servers
and the variety of information the server provides to
their browser.
But here it is, without recourse to JavaScript or CGI
program. Simply put this into the source code of a web
page that will be parsed for SSI tags:
<pre>
<!--#printenv -->
</pre>
What it prints is the environment variables, information
server and browser may need to know about each other in
order to respond as the other expects during any of many
possible browser/server interactions.
The SSI tag is between PRE tags because the information
comes without BR tags. The PRE tags keep it from printing
as a one-paragraph glob.
Conditional Content Publication
When it's appropriate to publish certain content only when
certain conditions are met, SSI can make decisions for
you.
These are conditional expressions #if, #elif (else if),
#else, and #endif. Those expressions can be used to test
various information and publish or not publish depending
on the results of the test.
For example, the DOCUMENT_URI, an environment variable
available to both browser and server, contains the URI of
the current web page. (The URI would be the URL without
the http and domain name part. The URI of URL
http://example.com/index.html would be /index.html)
The DOCUMENT_URI can then be tested to publish something,
or not, depending on which page is being viewed.
The "Server Side Includes; Navigation Links" article
linked from /archives contains code
that allows a file containing the navigation menu to be
included on every web page. The conditional expressions
use the DOCUMENT_URI to determine which menu item (the
one for the current page) should not be a link.
The article demonstrates how content can be published
depending on an environment variable.
Another example is to publish certain content depending
on which browser is used. Here is a fun way to try to make
all site visitors feel special.
<p>I see you use
<!--#if expr="$HTTP_USER_AGENT = /Opera/"-->
the Opera browser.
<!--#elif expr="$HTTP_USER_AGENT = /Firefox/"-->
Firefox.
<!--#elif expr="$HTTP_USER_AGENT = /MSIE/"-->
the IE browser.
<!--#else -->
a browser different than most people.
<!--#endif -->
Welcome to the club!</p>
HTTP_USER_AGENT is an environment variable available to
both browser and server, like DOCUMENT_URI is.
A Google search for "SSI Browser Detection" (no quotes)
will yield a number of pages explaining how to do more
elaborate browser detection with SSI.
The detection can be used to determine which style sheet
to use, for example, or whether or not to include certain
JavaScript.
Will Bontrager
©2005 Bontrager Connection, LLC
Please note:
Articles on this website are presented "as is". However -
If you have a question about a CGI script, HTML, CSS, PHP, or JavaScript
Ask one of our Experts and you'll have your answer!
Click here for details.