Server Side Includes

Server Side Includes (SSI) are a primitive scripting language. Since the advent of JSP, ASP, PHP and JavaScript, SSI scripting has faded away. It is still used to include files and to display the modification times of files.

Sections that will be covered in the exam have headings that look like this.

Special SSI Variables Available to the echo command

LAST_MODIFIED
Last modified on Thursday, 17-Jun-2021 14:46:55 EDT
Last modified on <!--#echo var="LAST_MODIFIED"-->
DOCUMENT_NAME
The document name is ssi.shtml
The document name is <!--#echo var="DOCUMENT_NAME"-->
DOCUMENT_URI
The document URI is /~downeyt/cgs4825/ssi.shtml
The document URI is <!--#echo var="DOCUMENT_URI"-->
DATE_GMT
The GMT date is Monday, 25-Nov-2024 19:52:17 GMT
The GMT date is <!--#echo var="DATE_GMT"-->
DATE_LOCAL
The local date is Monday, 25-Nov-2024 14:52:17 EST
The local date is <!--#echo var="DATE_LOCAL"-->

All Environment Variables are also Available

HTTP_USER_AGENT
The user agent is Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
The user agent is <!--#echo var="HTTP_USER_AGENT"-->
HTTP_HOST
The http host is users.cs.fiu.edu
The http host is <!--#echo var="HTTP_HOST"-->
SERVER_NAME
The server name is users.cs.fiu.edu
The server name is <!--#echo var="SERVER_NAME"-->
HTTP_ACCEPT
Http accept is */*
Http accept is <!--#echo var="HTTP_ACCEPT"-->
HTTP_ACCEPT_LANGUAGE
Http accept language is (none)
Http accept language is <!--#echo var="HTTP_ACCEPT_LANGUAGE"-->

Including Files

Relative references are relative to the current directory. It is not possible to go up the directory tree.
Absolute references start with / and are relative to the document root.
Relative references use the file attribute:
<!--#include file="ssi_include.txt" -->
This is a line of text included from a file in this directory.
Absolute references use the virtual attribute:
<!--#include virtual="/~downeyt/includes/menu.htm" -->

Creating Local Variables

It is possible to create a local variable inside a file that uses SSI commands.

Set a variable

<!--#set var="fiu" value="www.fiu.edu" -->
Echo a variable
The value of fiu is www.fiu.edu
The value of fiu is <!--#echo var="fiu" -->

Size and Last Modified of a file

Last Modified.
Relative references are relative to the current directory.
Absolute references are relative to the document root.
Relative reference: Last modified on <!--#flastmod file="ssi_include.txt" -->
Last modified on Friday, 19-Oct-2018 15:29:15 EDT

Absolute reference: Last modified on <!--#flastmod virtual="/~downeyt/includes/menu.htm" -->
Last modified on Wednesday, 19-Dec-2018 15:47:33 EST
File Size
Relative references are relative to the current directory.
Absolute references are relative to the document root.
Relative reference: File size in K <!--#fsize file="ssi_include.txt" -->
File size in K 63

Absolute reference: File size in K <!--#fsize virtual="includes/menu.htm" -->
File size in K 3.3K

Change the format of the output

For a list of the time formatting codes, visit this site: http://internetconnection.net/support/tech-ssitime.html

Change the last modified date to m/d/y, the decimal number day of the year, and am/pm
<!--#config timefmt="%A %D (day %j) at %r" -->

Change the file size to bytes
<!--#config sizefmt="bytes" -->

Last Modified
Relative references are relative to the current directory.
Absolute references are relative to the document root.
Relative reference: Last modified on <!--#flastmod file="ssi_include.txt" -->
Last modified on Friday 10/19/18 (day 292) at 03:29:15 PM

Absolute reference: Last modified on <!--#flastmod virtual="/~downeyt/includes/menu.htm" -->
Last modified on Wednesday 12/19/18 (day 353) at 03:47:33 PM
File Size
Relative references are relative to the current directory.
Absolute references are relative to the document root.
Relative reference: File size in bytes <!--#fsize file="ssi_include.txt" -->
File size in bytes 63

Absolute reference: File size in bytes <!--#fsize virtual="/~downeyt/includes/menu.htm" -->
File size in bytes 3,414

Including the output of commands (carefully)

Include the output of a cgi script.
The cgi parameter is relative to the document root of the server.
<!--#exec cgi="/~downeyt/cgi-bin/test.cgi" -->
Hello from test.cgi

<!--#exec cmd="/netdepot/J2SE-1.6/bin/java -cp /home/bear-008/users/downeyt/public/server/data/java SayHello" -->


Including the output of a Unix command.
The cmd parameter is an absolute file system path.
<!--#exec cmd="/usr/bin/date"-->



Using Conditional Statements

Since Apache 2.3.12, the syntax for SSI conditional statements has changed. The new syntax is buggy and needs to be updated. This page uses the old Apache 2.2.x syntax with the directive SSILegacyExprParser on.

It is possible to use an if statment with SSI files. The conditional directives are if, elif, else, endif. It is also possible to do pattern matching using  /pattern/. Here is an example that tests if this file was last modified on a weekday

Test if it was modified on a weekday

<!--#if expr="${LAST_MODIFIED} = /Sat/" -->
    <p>Not modified on a weekday; modified on Saturday 
<!--#elif expr="${LAST_MODIFIED} = /Sun/" --> 
    <p>Not modified on a weekday; modified on Sunday 
<!--#else --> 
    <p>Modified on a weekday 
<!--#endif -->

Modified on a weekday

Change LAST_MODIFIED to a different day

<!--#if expr="${LAST_MODIFIED} = /Sat/" -->
    <!--#set var="LAST_MODIFIED" value="Monday" -->
<!--#elif expr="${LAST_MODIFIED} = /Sun/" -->
    <!--#set var="LAST_MODIFIED" value="Monday" -->
<!--#else -->
    <!--#set var="LAST_MODIFIED" value="Sunday" -->
<!--#endif -->

Test if it was modified on a weekday again

<!--#if expr="${LAST_MODIFIED} = /Sat/" -->
    <p>Not modified on a weekday; modified on Saturday 
<!--#elif expr="${LAST_MODIFIED} = /Sun/" --> 
    <p>Not modified on a weekday; modified on Sunday
<!--#else --> 
    <p>Modified on a weekday 
<!--#endif -->

Not modified on a weekday; modified on Sunday

Displaying all of the environment variables

<!--#printenv -->
UNIQUE_ID=Z0TVcS@9OTXVErXw17yyYAAAAIE
SCRIPT_URL=/~downeyt/cgs4825/ssi.shtml
SCRIPT_URI=https://users.cs.fiu.edu/~downeyt/cgs4825/ssi.shtml
HTTPS=on
SSL_TLS_SNI=users.cs.fiu.edu
SSL_SERVER_S_DN_CN=*.cs.fiu.edu
SSL_SERVER_I_DN_C=US
SSL_SERVER_I_DN_ST=Arizona
SSL_SERVER_I_DN_L=Scottsdale
SSL_SERVER_I_DN_O=GoDaddy.com, Inc.
SSL_SERVER_I_DN_OU=http://certs.godaddy.com/repository/
SSL_SERVER_I_DN_CN=Go Daddy Secure Certificate Authority - G2
SSL_SERVER_SAN_DNS_0=*.cs.fiu.edu
SSL_SERVER_SAN_DNS_1=cs.fiu.edu
SSL_VERSION_INTERFACE=mod_ssl/2.4.37
SSL_VERSION_LIBRARY=OpenSSL/1.1.1k
SSL_PROTOCOL=TLSv1.3
SSL_SECURE_RENEG=true
SSL_COMPRESS_METHOD=NULL
SSL_CIPHER=TLS_AES_256_GCM_SHA384
SSL_CIPHER_EXPORT=false
SSL_CIPHER_USEKEYSIZE=256
SSL_CIPHER_ALGKEYSIZE=256
SSL_CLIENT_VERIFY=NONE
SSL_SERVER_M_VERSION=3
SSL_SERVER_M_SERIAL=6E35378C54738ACB
SSL_SERVER_V_START=Jan 20 18:05:35 2024 GMT
SSL_SERVER_V_END=Feb 20 18:05:35 2025 GMT
SSL_SERVER_S_DN=CN=*.cs.fiu.edu
SSL_SERVER_I_DN=CN=Go Daddy Secure Certificate Authority - G2,OU=http://certs.godaddy.com/repository/,O=GoDaddy.com\, Inc.,L=Scottsdale,ST=Arizona,C=US
SSL_SERVER_A_KEY=rsaEncryption
SSL_SERVER_A_SIG=sha256WithRSAEncryption
SSL_SESSION_ID=304e2210a7c57f34fef8e2f627fe1cf021a9cd4a9ed8214b8731a14bb1166078
SSL_SESSION_RESUMED=Initial
HTTP_ACCEPT=*/*
HTTP_USER_AGENT=Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
HTTP_ACCEPT_ENCODING=gzip, br, zstd, deflate
HTTP_HOST=users.cs.fiu.edu
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
SERVER_SIGNATURE=
SERVER_SOFTWARE=Apache
SERVER_NAME=users.cs.fiu.edu
SERVER_ADDR=131.94.130.45
SERVER_PORT=443
REMOTE_ADDR=18.219.25.226
DOCUMENT_ROOT=/var/www/html
REQUEST_SCHEME=https
CONTEXT_PREFIX=/~downeyt
CONTEXT_DOCUMENT_ROOT=/home/bear-d/users/downeyt/public_html
SERVER_ADMIN=root@localhost
SCRIPT_FILENAME=/home/bear-d/users/downeyt/public_html/cgs4825/ssi.shtml
REMOTE_PORT=43904
GATEWAY_INTERFACE=CGI/1.1
SERVER_PROTOCOL=HTTP/1.1
REQUEST_METHOD=GET
QUERY_STRING=
REQUEST_URI=/~downeyt/cgs4825/ssi.shtml
SCRIPT_NAME=/~downeyt/cgs4825/ssi.shtml
DATE_LOCAL=Monday 11/25/24 (day 330) at 02:52:17 PM
DATE_GMT=Monday 11/25/24 (day 330) at 07:52:17 PM
LAST_MODIFIED=Sunday
DOCUMENT_URI=/~downeyt/cgs4825/ssi.shtml
DOCUMENT_ARGS=
USER_NAME=downeyt
DOCUMENT_NAME=ssi.shtml
fiu=www.fiu.edu