Server-Side Includes
What are Server-Side Includes?
A server-side include is a type of macro that can be expanded at the time
the Web server is sending the document to the browser. Special tags
embedded in an HTML document are recognized as instructions to the server
to perform a substitution for the tag. The substitution depends on the
tag. The supported tags include echo, include, exec, config, fsize, and
flastmod. With these tags, another document can be included inline, the
output of a script can be included, a file's last modification date and
time can be included, and more.
Which Files are Parsed?
All files are not searched for Server-side includes. A slight performance
penalty exists for doing this parsing. Only files with a certain content
type are parsed before they are sent. This content type is
text/x-server-parsed-html. A typical setup (and the default
setup) is to have the extensions .SHTML and .SHT
map to the content type text/x-server-parsed-html. This
means that only files with that extension are parsed for server-side
includes.
IFS files, QDLS files, spooled files and database files can all contain
server-side includes. The header and footer files for spooled files and
database files can also include server-side includes. However, the header
and footer files for dynamic indexes will send a server-side include file
as a regular HTML file.
Server-Side Include Format
The format of a server-side include is as follows:
<!--#tag parm1="value1"
parm2="value2" ... -->
where,
<!--#
- These five characters always begin a server-side include. This will
hide the command as a comment if the file ends up on a Web server that does
not support server-side includes.
- tag
- The tag indicates the type of server-side include to perform.
Valid values are
echo, include,
exec, config, fsize, and
flastmod. Tags are not case-sensitive. Each tag is described
below.
- parmX="valueX"
- Each tag has one or more parameters that are specified as
parm="value". If multiple parameters are specified, each
is separated by a space. The parameter name is not case-sensitive, but
the value might be depending on the parameter and operation.
-->
- A server-side include always ends with these three characters.
Example Include
The following document will include the Internet host name of the machine
that serves this document.
<HTML>
<BODY>
This document is being served by <!--#echo var="SERVER_NAME"-->.
</HTML>
</BODY>
Example output:
This document is being served by www.inetmi.com.
Supported Tags
Below is a list of the different operations a server-side include can
perform. Each tag and its parameters are described.
ECHO
The echo server-side include is replaced with the value of a
pre-defined variable that is specified as a parameter. The only valid
parameter is var.
The echo command is used to send the value of several pre-defined
variables to the browser. These variables include all the
pseudo-environment variables available to script programs and some just
available to server-side include documents.
- Script Pseudo-Environment Variables
All of the environment variables that are available to Web Server/400
scripts are also available through the echo server-side include.
- Server-Side Include Variables
The following lists the additional variables that are available for
including in server-side include documents.
- DOCUMENT_NAME
The fully qualified path of the document currently being parsed.
- DOCUMENT_URI
The URL-path entered to request
this document. The URL-path is still escaped. The query string is
not part of this value.
- QUERY_STRING_UNESCAPED
The query string used to request this document. The query string is
already unescaped.
- DATE_LOCAL
The current local date and time. The format of the date and time can
be configured using the timefmt parameter of the config
server-side include tag. This format string can contain
meta-symbols that describe how the date and
time are displayed. The default string is "%a %b %d %H:%M:%S %Y"
which produces a date and time string of "Wed Jan 15 14:21:45 1992".
- DATE_GMT
This is the same as DATE_LOCAL except in Greenwich Mean Time.
- LAST_MODIFIED
The local date and time the current document was last modified. This
is presented the same as DATE_LOCAL.
- Statistic Variables
The following variables are available to include information about the
usage and performance statistics of the current Web Server. These
values are updated for each new document.
- STATS_START_TIME
The date and time the server was started in local time.
- STATS_TOTAL_REQUESTS
The number of requests the server has handled since it was started.
- STATS_REQUESTS_SECOND
The average number of requests served per second.
- STATS_AVG_CPU_REQUEST_TIME
The average CPU time spent per request.
- STATS_TOTAL_CPU_REQUEST_TIME
The total CPU time spent handling all the requests.
- STATS_AVG_ELAPSED_REQUEST_TIME
The average elapsed (wall) time spent per request.
- STATS_TOTAL_ELAPSED_REQUEST_TIME
The total elapsed (wall) time spent handling all the requests.
- STATS_AVG_BYTES_SENT
The average number of bytes sent per request.
- STATS_TOTAL_BYTES_SENT
The total number of bytes sent for all requests.
- STATS_AVG_BYTES_RCVD
The average number of bytes received per request.
- STATS_TOTAL_BYTES_RCVD
The total number of bytes received for all requests.
- STATS_TOTAL_BYTES
The total number of bytes send and received for all requests.
- STATS_AVG_BYTES_PER_SECOND
The average number of bytes transmitted per second.
- STATS_TOTAL_NUM_RPS
The number of request processors currently handling requests on the
server.
INCLUDE
The include server-side include allows one document to include
another document. The main document and its included sub-documents are all
sent as one large document to the browser.
Included files can themselves include other files. The level of nesting
has no limit. However, circular references are not allowed. For instance,
if Document A includes Document B which includes
Document A again, an error will occur.
Any type of URL can be included except script URLs.
This includes dynamic indexes, database files, user directory files, and
spooled files.
The document to include can be specified in one of two ways:
- File
The include tag can have a file parameter that specifies
a file to include. The format of an include of a file
is:
<!--#include file="RelativeFilename"-->
The "RelativeFilename" is the name of the file to include. The
path of the included file is relative to the current file. The path cannot
contain any parent directory references ("../") and it cannot
start with a slash.
- Virtual
The include tag can have a virtual parameter that
specifies a new URL to include. The format of an include of a
virtual is:
<!--#include virtual="NewURL"-->
The "NewURL" is the new URL to include. This can include the
URL-path and
query string portions of a URL.
This new URL is resolved as if it was a request by itself.
EXEC
The exec server-side include is used to insert the output from a
script into a parsed document. Any valid CGI
script can be included, however the output of some scripts is not
appropriate for inclusion in the middle of another HTML document. For
instance, a script might have its own <HEAD> section even though the
main document already contains a <HEAD> section.
Any response headers returned by
the script are not included in the resulting document, but are still
necessary for proper interpretation of the script output.
If the result of the CGI script is a
redirection, then a hyperlink to the
redirected location is inserted in the document instead of the output of
the script.
The only valid parameter to exec is:
CONFIG
The config server-side include is used to configure the behavior
of server-side includes. This includes changing the default error message,
the time format, and the file size format. Configuration changes take
effect immediately and last until the end of the document or until the
configuration value is changed again.
- errmsg
Whenever an error occurs parsing a server-side include file, this error
message is sent in-line in the document. The errmsg config
parameter changes the default error message to any valid string. Any error
that occurs after this configuration tag will produce the new message.
<!--#config errmsg="Error message"-->
- timefmt
Many values displayed when parsing a server-side include document
include dates and times. These values are displayed using the
timefmt format string. This string can contain meta-symbols that describe how the date and time are
displayed. The default string is "%a %b %d %H:%M:%S %Y" which produces a
date and time string of "Wed Jan 15 14:21:45 1992".
<!--#config timefmt="meta-string"-->
The "meta-string" includes regular characters and meta-symbols
that are used to describe the format of the date and time being displayed.
- sizefmt
Some server-side includes display file sizes. This value configures how
those sizes are displayed.
<!--#config sizefmt="SizeFormat"-->
The "SizeFormat" can be either "bytes" or
"abbrev". A value of bytes indicates the size
should be displayed as the decimal number of bytes in a file. A value of
abbrev indicates the size should be displayed in bytes,
kilobytes, megabytes or gigabytes. For example, a file containing 4678
bytes would be displayed as 4K in abbrev mode. The size format
defaults to bytes.
FSIZE
The FSIZE server-side include expands to the size of a specified
file. The size is displayed as configured by the sizefmt
parameter to the config tag.
The target file can be specified in one of two ways:
- File
The fsize tag can have a file parameter that specifies
a target file. The format of a fsize of a file
is:
<!--#fsize file="RelativeFilename"-->
The "RelativeFilename" is the name of the target file.
The path of the file is relative to the current file. The path cannot
contain any parent directory references ("../") and it cannot
start with a slash.
- Virtual
The fsize tag can have a virtual parameter that
specifies a new URL of the target file. The format of a
fsize of a virtual is:
<!--#fsize virtual="NewURL"-->
The "NewURL" is the new URL of the target file. This should be a
full URL-path. This new URL is
resolved as if it was a request by itself.
FLASTMOD
The FLASTMOD server-side include expands to the last modification
date and time of a specified file. The last modification date and time are
displayed as configured by the timefmt parameter to the
config tag.
The target file can be specified in one of two ways:
- File
The flastmod tag can have a file parameter that specifies
the target file. The format of a flastmod of a file
is:
<!--#flastmod file="RelativeFilename"-->
The "RelativeFilename" is the name of the target file.
The path of the file is relative to the current file. The path cannot
contain any parent directory references ("../") and it cannot
start with a slash.
- Virtual
The flastmod tag can have a virtual parameter that
specifies a new URL of the target file. The format of a
flastmod of a virtual is:
<!--#flastmod virtual="NewURL"-->
The "NewURL" is the new URL of the target file. This should be a
full URL-path. This new URL is
resolved as if it was a request by itself.