In addition to including files into your page, Server Side Includes can also be used to get information from the server and write it to the page before it is sent to the reader. Though they aren’t meant as replacements for more sophisticated methods of generating dynamic content such as CGI or PHP, SSI are a nice way of adding small details to your page. Here we will discuss the file, date and time options.
This page was last updated on 2012-08-21
Config and Echo
If you have read through the introductory tutorial on file Includes, you should now be familiar with the format of an SSI directive. They generally look like this:
<!--#command argument="value" -->
We’re about to use two new ones to print the date to the page. These are
config sets the format that one of the SSI variables will be returned in (we’re gonna print the date so we’ll be using the local date variable,
echo writes one of the variables into the file.
To write the current date and/or time to a
.shtml file, add this line wherever you want it to show up:
<!--#config timefmt="%c" --><!--#echo var="DATE_LOCAL" -->
This will return something like
Sun Jan 25 12:33:47 2004
Timestamp Format Codes
Above we used the
%c value to return a full date and time, but there are 19 other choices for how you want the timestamp to look. The values you can give
timefmt are all single characters, though you’ll need a chart to remember them all.
||Abbreviated weekday name||Sun|
||Full weekday name||Sunday|
||Abbreviated month name||Feb|
||Full month name||February|
||Standard date & time||Mon Oct 22
||Day of month as digit||12|
||Hour number (24-hour clock)||22|
||Hour number (12-hour clock)||10|
||Day of the year as digit||047|
||Month as digit||06|
||AM or PM||PM|
||Day of the week number (with Sunday as first day)||1|
||Date in standard format||01/12/05|
||Time in standard format||12:04:45|
||Two-digit year number||04|
When the output is referred to as “standard” above, it means that the server will return it’s normal format for that value. This may be different from server to server.
All these options are nice, but what you’ll probably end up doing is combining some of them to create your preferred format. For instance, to create a date like Tuesday 11 November, 2004, you would write
<!--#config timefmt="%A %d %B, %Y" -->
<!--#echo var="DATE_LOCAL" -->
Just whack them all in there together, along with whatever punctuation you need.
Printing File Data
To print out the date and time when a file was last modified, use the
flastmod command. This will return a date and time, the format of which is specified in the last
timefmt directive you wrote. An example, and its output:
<!--#config timefmt="%d/%m/%Y" -->
Last modified on <!--#flastmod virtual="/index.shtml" -->
Last modified on 25/01/2004
You can also print out the filesize of a file with
fsize. The format of the returned value depends on the directive
sizefmt, which can be set to
bytes for a formatted byte-count (eg: 23,720), or the default,
abbrev, for an abbreviated count rounded up into kilobytes or megabytes.
<!--#config sizefmt="bytes" -->
Filesize: <!--#fsize virtual="/index.shtml" -->
SSI Environment Variables
In addition to the CGI environment variables, SSIs also have access to a number of built-in values which can be echoed to the page. These are:
- The current filename.
- The virtual path to this document (such as
- The unescaped version of any search query the client sent, with all characters special to the shell escaped with a backslash.
- The current date in the server’s local time zone, as seen above.
- Same as
DATE_LOCALbut in Greenwich mean time.
- The last modification date of the current document. Subject to
timefmtlike the other dates.
These are all accessed in the same way,
<!--#echo var="DOCUMENT_NAME" -->