SitemapSearchHelp
ECU Home

Server Side Includes

When a web server receives a request to load a particular web page, it first examines the HTML in the web page for certain instructions referred to as Server Side Includes or SSI. These instructions are in the form:

<!--#instruction -->

where instruction can be a variety of commands. When the web server encounters an SSI, it inserts the requested information in place of the SSI statement. Two of the most common uses for SSI are for including the contents of another file in a web page, or to add the date the file was created to the page.

Note: A server side include can be included in a server side include, so for example, the date last updated SSI example below can be included in the footer file.

Page URL and page last updated using #echo

#echo prints the value of one of the include variables. The only valid argument to #echo is var, whose value is the name of the variable that you want to echo. Two of the available #echo variables are:

<!--#echo var="DOCUMENT_URI" -->

This displays the URL of the document (i.e. "/public_html/index.html") relative to the server root; note that it is "URI", not "URL" in the command! If your site is located on the main ECU web server, the server root is www.ecu.edu.au so you could include the web page URL as follows:

http://www.ecu.edu.au<!--#echo var="DOCUMENT_URI" -->

This would display:

http://www.ecu.edu.au/webadmin/aboutssi.html

<!--#echo var="LAST_MODIFIED" -->

It is good practice to always include a last updated line on all your web pages because readers want to know if the information they are reading is current. However, it is easy to forget to update that line when you update the page. However, you can add a server side include to the web page asking the web server to insert the creation date of the file. To do this, you would add the following statement to your HTML file:

This page was last updated on <!--#echo var="LAST_MODIFIED"-->

This statement will output not just the date the file was created or last updated, but also the time. You may not want that level of detail; you can add a statement that configures the display format of the time:

This page was last updated on
<!--#config timefmt="%B %e, %Y"--><!--#echo var="LAST_MODIFIED"-->

This will produce output that looks like this:

This page was last updated on February 8, 2001

The following table provides a reference for common timefmt field descriptors, which allow you to reformat date and time display so it is easier to read.

Field Descriptor Description Example
%a Abbreviated weekday name Mon
%A Full weekday name Monday
%b Abbreviated month name Jun
%B Full month name June
%c Date and time
(like %A, %d %B %Y %I:%M:%S %P)
Monday, 10 November, 1997 11:08:59 AM
%d Day of month from 01 through 31 05
%H Hour in military time from 00 through 23 22
%I Hour in civilian time from 00 through 12 10
%m Month from 01 through 12 06
%M Minute from 00 through 59 21
%p AM or PM PM
%S Seconds from 00 through 59 14
%x Date, in the form Day Month, Year (%d %B, %Y) 10 November, 1997
%X Time, using the HH:MM:SS (Hours:Minutes:Seconds) format 22:21:14
%y Year in two digit format, from 00 through 99 97
%Y Year in four digit format 1997

Including other files using #include

If many pages in your web site contain a standard header and footer, you can include the header and footer via a server side include. Then if you need to change the header or footer, you only need to make the change to the single header or footer file; this will save you lots of work, and ensure that your web site has a consistent appearance.

It is good practice to create a directory in your site called includes or ssi and store in that folder any files that are called by a server side include.

In the case of your header and footer, these files would probably be called header.html and footer.html. These files will contain HTML tags and text, but are not complete HTML files because they don't contain the HTML, HEAD and BODY tags. For example, header.html might be some images and text in a table.

You want to have the text and graphics contained in header.html appear as the beginning of each of your web pages, and the text and graphics contained in footer.html appear at the base of each page. To achieve this, you would construct your pages as follows:

<HTML>
<HEAD>
<TITLE>My Web Page</TITLE>
</HEAD>
<BODY>
<!--#include virtual="includes/header.html"-->
(main content of page)
<!--#include virtual="includes/footer.html"-->
</BODY>
</HTML>

Note: Make sure that there are no spaces between the ""--" and the #).

When the web server loads this page, the contents of the specified files will be substituted for the includes. You can't see an SSI from a browser, because the substitution has already taken place; you only see the results of the inclusion.

If your server side includes don't work

If your server side includes don't seem to be working, check the following details:

  1. Syntax errors - check the spacing around your dashes
  2. Are server side includes supported on your web server? Some web servers on campus automatically scan every file for an SSI, others only scan if instructed. The main ECU web server supports ssi. Check with your friendly webmaster.