Echoing server information such as user IP, current date etc using SSI
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: Thursday, 21-Nov-2024 03:36:39 CST
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: Tuesday, 12-May-2015 11:07:21 CDT
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: 3.136.19.124
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
Here's a completed example.
- Tutorial Introduction
- Using SSI to include the contents of an external file
- Echoing server information such as user IP, current date etc using SSI
- Using #config to customize time format and more
- Manually enabling SSI on your server