Enabling SSI
If your server is already SSI enabled, all that needs to be done to call SSI
to life from any web page is to give the web page a file name extension of .shtml
instead of the usual .html. This will cause the SSI enabled server to scour
the web page looking for comments that are, in fact, commands which tell the
server to perform certain tasks before it allows the web page to be displayed.
This process of prescouring (if that's a word) the page is known as parsing.
Check with your server administrator to see if it is SSI enabled if it's not,
consider changing to another host or ISP like us.
To call the SSI program to life from any web page simply name the page with
the .shtml extension instead of the usual .html extension i.e., mywebpage.shtml.
This .shtml extension tells the program on our server to scour this page looking
for those special commands. Look at your browser's address window and you will
see that this page is called tut_ssi.shtml
A Commanding Comment
What are <--Comments -->?
To accomplish the above we inserted a comment in the .shtml file (or SSI enabled
web page). Comments in any HTML web page adhere to the following syntax;
<!--comment -->
This comment tag allows the web page designer to insert comments into the .html
or .shtml file without having those comments displayed on the browser. Comment
tags do the same thing that a:
REMark statement does in a DOS batch file
; semicolon does in a Windows system ini file
// double slash does in a JavaScript
# pound sign does in a perl script.
It makes the program ignore the comments but allows the author to make notes
within the file.
Any <!--#comment --> which includes the # pound sign is looked at by
an SSI enabled server as a command rather than simply as a comment. Since it
is in the form of a comment, it does not get displayed on the web page by the
browser but because it has the pound sign it is handled as a command and the
results of the command are then displayed on the browser. The allowable commands
can vary widely from server to server but 'echo' is one common command. "Echo"
bounces back to the .shtml document the results of the command.
Creating a Back Button
In the second example above we used the <!--#echo var="HTTP_REFERER"
--> to show the link that you used to get here. This command can be used
in conjunction with the linking <a href="...."> anchor tag to
create a return link or "Back Button". Note: If you need a refresher
to understand <a href="....">, visit our HTML tutorials page.
With this command a web surfer can return back to the page from which they
came, that is, unless they got to the page by way of typing the page's name
directly into their browser's address window or from a link in an email. In
this case (none) would appear as the "HTTP_REFERER".
The syntax for creating a back button:
<a href="<!--#echo var="HTTP_REFERER" -->">BACK
BUTTON</a>
Using SSI To Run A Program
If you want to activate a cgi script from an SSI enabled web page, The execute
command would be incorporated. The syntax for this command follows
<!--#exec cgi="scriptname.cgi" -->
This could be used to call a cgi script that in turn calls a header.txt file
which would then display the results of the header file in the location that
the <!--#exec cgi="scriptname.cgi" --> command is placed. We
have a free header/footer cgi script if you would like to try this. With this
method you will need to set the permissions of the cgi file to chmod 755. If
you don't know what this chmod 755 means then visit our tutorial on setting
file permissions.
If that seems too confusing, good, because there is an easier way to perform
the same function and it's listed just below. If you prefer to learn more about
installing and running a cgi header script from an SSI enabled page, visit our
"Free CGI" page
Using SSI To Include A File
'Include' is another SSI command and it uses the 'file' attribute. This can
be a very useful SSI call since some server's and host administrator's won't
allow the use of the SSI command 'exec' mentioned above. With the 'include file'
command you can include a text or html file into your shtml file (SSI enabled
web page). This gives you the ability to create headers and footers without
using cgi scripts.
The syntax for this comment tag is <!--#include file="myfile.txt"
--> or <!--#include file="myfile.html" -->
let's Assume The Following;
We have a header file called "header.txt" and in this text file is,
fleece was white
Our SSI enabled web page called "marylamb.shtml" contains;
<html>
<head><title>Mary's lamb</title></head>
<body>
Mary had a little lamb, it's <!--#include file="header.txt" -->
as snow.
</body>
</html>
The SSI enabled web page (marylamb.shtml) would look like this in a browser,
Mary had a little lamb, it's fleece was white as snow.
This small example can be extended to include any sized header or footer file.
Simply create the header or footer and name it header.txt and call it with the
include command <!--#include file="header.txt" -->. While the
header and footer can have the extension .txt or .html, the SSI enabled web
page must have the .shtml extension in order for the server to scour (parse)
the page looking for SSI commands. It is a good idea to make the header/footer
file a .txt file rather than an .html file. HTML tags can be placed in the header.txt
file and the .shtml page will insert the commands into it's page just as if
they were part of the original .shtml page.
Displaying the SSI results of one page in another page
To have one shtml file call another file that has contained within it an SSI
request, do this.
FileOne.shtml is the primary web page and it has contained in it a call to
a second file (FileTwo.shtml). FileTwo.shtml has within it an SSI request for
the date. The key to making the results of one SSI file show up in another file
is to make that file an shtml file not a text file then SSI will work its magic
on the second shtml file being called by the first shtml file.
FileOne.shtml contains
<html>
<head><title>Call to file 2</title></head>
<body>
This file is calling another file that will use SSI to display the date
<!--#include file="FileTwo.shtml" --><BR>
</body>
</html>
FileTwo.shtml contains
This is the second file and is an shtml file not a text file because of this
the date will be displayed on FileOne <!--#echo var="DATE_LOCAL"
-->
To see this example click here
Include Virtual
If you wish to place an .shtml file in one directory and have it call a header.txt
file or any file in another directory, use the include virtual= command to accomplish
this. The syntax is <!--#include virtual="/header.txt" -->.
Note that there is now a slash before the /header.txt file name this is calling
the header.txt file in the user's home directory from any other directory.
If you encounter an error message that looks like this;
[an error occurred while processing this directive]
Then it's because you need to use the include virtual method.