Another common and practical usage of SSI is to display
basic information about your server or your visitors, such as the last
modified date of the webpage, current server time, visitor IP address etc.
These are all tasks that client side languages such as JavaScript cannot
accomplish, but can be done using the #echo command of SSI.. Here's a quick overview of some of variables you can use with
SSI's #echo to display useful information:
| DATE_GMT | The current server date, in Greenwich mean time. Format using #config. |
| DATE_LOCAL | The current server date. Format using #config. |
| DOCUMENT_NAME | The file name of the current document. |
| DOCUMENT_URI | The virtual path of the current document. |
| LAST_MODIFIED | The last modified date of the document. Format using #config. |
| HTTP_REFERER | URL of the document the client derived from to get to current document. |
| REMOTE_ADDR | IP address of the visitor. |
| #flashmod | Command to display last modified date/time of a document or file on the server. Format using #config. |
| #fsize | Command to display file size of a document or file. Format using #config. |
To echo something using SSI, the syntax is:
<!--#echo var="VARIABLE HERE" -->
Lets see how to use these variables exactly.
Echo current server date and time
To display the current server date and time, use either the
"DATE_GMT" or "DATE_LOCAL" variable. In its simplest form:
<!--#echo var="DATE_LOCAL" -->
Output: Saturday, 24-Aug-2013 04:49:10 MDT
Not bad eh for one simple line of code.
Echo last modified date of current document or file
It's very useful at times to show the last modified date of
a web page:
This document last modified:
<!--#echo var="LAST_MODIFIED" -->
Output: This document last modified: Saturday, 04-Mar-2006 01:41:24 MST
Echo last modified date of any document or file
You can also display the last modified date of any document
or file on your server besides the present, by using another command called
#flastmod instead of #echo:
greenday.mp3 last modified: <!--#flastmod file="grenday.mp3"--> Index page last modified: <!--#flastmod virtual="/index.html"-->
Sample output: greenday.mp3 last modified Thursday,
06-Jan-2005 05:35:27 EST.
Echoing visitor IP address
This is also a commonly requested question and answer- how to display the
user's IP address:
Your IP:
<!--#echo var="REMOTE_ADDR" -->
Output: Your IP: 117.253.218.65
Displaying file size of a document
Finally, you can display the file size of any document
on your server using #echo, by using a different command called #fsize.
This document's file size:
<!--#fsize file="current.shtml" -->
The file size of main index page:
<!--#fsize virtual="/index.shtml" -->
Sample output: This document's file size: 8.4K
Interesting Uses of SSI
The interesting thing to note about using the output commands
of SSI is that they can be embedded anywhere inside your HTML source, even
in unexpected places to do interesting things. For example, you can use SSI
echo to populate a JavaScript variable with the visitor's IP address, then
continue to use JavaScript to react accordingly:
<script type="text/javascript">
var userIP="<!--#echo var="REMOTE_ADDR" -->"
if (userIP=="list of bad IPs to check")
alert("You are not allowed on this site.")
</script>
Another unconventional usage is to pass the current server
time to the JavaScript Date object, then use JavaScript to display the
current live time of your server:
var currentime="<!--#echo var="DATE_LOCAL" -->" var serverdate=new Date(currenttime) //rest of script
Using #config to customize time format and more
On the previous page I showed you SSI's ability to
output
various server information, such as the size of a file, current date
and time
etc. This is all great stuff, but a question that quickly follows is
"Can I
customize the format of the output such as of the date and time?"
Sorry, got to learn to just be content! Just kidding. Yes, it's
certainly possible, thanks to another SSI command called #config.
Take a
look at this:
<!--#config timefmt="%m/%d/%y" --> <!--#echo var="DATE_LOCAL" -->
Output:
08/24/13
Instead of a long string containing both the date and time,
I've used #config to pound things into exactly the format I want. Lets see
now the various parameters of the #config command at your disposal:
| CODE | PURPOSE OF CODE | Sample output |
| %a | abbreviated weekday name | Sun |
| %A | full weekday name | Sunday |
| %b | abbreviated month name | Jan |
| %B | full month name | January |
| %c | locale's appropriate date and time | Sun Dec 28 04:45:57 2005 |
| %d | day of month - 01 to 31 | 25 |
| %D | date as %m/%d/%y | 12/25/05 |
| %e | day of month - 1 to 31 | 25 |
| %H | hour - 00 to 23 | 15 |
| %I | hour - 01 to 12 | 03 |
| %j | day of year - 001 to 366 | 361 |
| %m | month of year - 01 to 12 | 12 |
| %M | minute - 00 to 59 | 09 |
| %n | insert a newline character | |
| %p | string containing AM or PM | PM |
| %r | time as %I:%M:%S %p | 06:08:05 PM |
| %R | time as %H:%M | 15:09 |
| %S | second - 00 to 59 | 02 |
| %t | insert a tab character | |
| %T | time as %H:%M:%S | 15:21:07 |
| %U | week number of year (Sunday is the first day of the week) - 00 to 53 | 52 |
| %w | day of week - Sunday=0 | 0 |
| %W | week number of year (Monday is the first day of the week) - 00 to 53 | 51 |
| %x | Country-specific date format | 12/25/05 |
| %X | Country-specific time format | 04:50:29 |
| %y | year within century - 00 to 99 | 05 |
| %Y | year as CCYY (4 digits) | 2005 |
| %Z | timezone name | PST |
Here are a couple more examples:
<!--#config timefmt="%A %d %B, %Y" --> <!--#echo var="DATE_LOCAL" -->
Output:
Saturday 24 August, 2013
<!--#config timefmt="%D %r"-->
This document last modified:
<!--#echo var="LAST_MODIFIED" -->
Output:
This document last modified: 03/04/06 01:41:24 AM
Formatting file size with #config
So far on this page I've only used the #config command to
format time related output. But you can also use this command on file size
output:
<!--#config sizefmt="abbrev"--> <!--#fsize file="current.shtml" -->
<!--#config sizefmt="bytes"--> <!--#fsize file="current.shtml" -->
The first code tells the server to display the file size in
abbreviated form, rounded to the nearest kilobytes. The second example
obviously displays the size in bytes instead.

About the Author: Lally Mahey
0 comments: