Server Side Includes; More Tags
A previous articles about SSI, The Basics of SSI, outlined some very useful techniques used by many a webmaster.
This article will explore some less common but equally valuable SSI tags.
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, December 22, 2024 at 02:42 AM
The formatting is done by specifying codes that begin with a % character.
Here's a list of formatting codes and what they're replaced with.
This code... | ...is replaced with |
---|---|
%A | Full weekday name |
%a | Abbreviated weekday name |
%B | Full month name |
%b | Abbreviated month name |
%C | Server date and time in default format |
%D | Date with %m/%d/%y format |
%d | Month day as 01-31 |
%e | Month day as 1-31 |
%H | Hour as 00-23 |
%I | Hour as 01-12 |
%j | Year day as 001-366 |
%M | Minute as 00-59 |
%m | Month as 01-12 |
%n | A linebreak |
%p | Period as AM or PM |
%R | Time with %H:%M format |
%r | Time with %I:%M:%S %p format |
%S | Seconds as 00-59 |
%T | Time with %H:%M:%S format |
%t | A tab character |
%U | Year week as 00-51 (week begins Sunday) |
%W | Year week as 00-51 (week begins Monday) |
%w | Week day as 0-6 (week begins Sunday) |
%X | Server time in locale format |
%x | Server date in locale format |
%Y | Four-digit year |
%y | Two-digit year |
%Z | Time zone abbreviation |
%% | A "%" character |
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.
Here's an example 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 the <!--#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. <!--#elif expr="$HTTP_USER_AGENT = /Chrome/"--> Chrome. <!--#else --> a browser different than most people. <!--#endif --> Welcome to the club! </p>
It will display like this:
I see you use a browser different than most people. Welcome to the club!
Will Bontrager