On the Server Side of the Street
April 5, 1999
Server-side Includes (SSI)
is such a handy tool, I'm surprised
it's not more popular. With SSI, you insert a link to a file
into a Web page. When the page loads, that file is inserted at
the desired place, and it can include any text you wish,
including HTML, scripting, or whatever.
For example, if you wanted to insert a navbar at the top of a
page, simply include the following code at the appropriate spot:
<!--#include virtual="navbar.txt" -->
where navbar.txt is the HTML code for your navbar.
How much woudja pay for a handy tool like this? Wait, there's
more! With some imagination, SSI can be used for all kinds of
other tricks. It can be used to run a script or program which
can select a file at random, or based on certain parameters, so
you can include an appropriate text snippet from a list. This
can be used to rotate content, so that when a visitor comes to
your site, they see a randomly-selected article, or one targeted
to their browser, OS or geographic location. It can even be
used to create a rudimentary ad banner rotation system. Or not
so rudimentary! The possibilities are limited only by your
scripting skills.
SSI also allows you to print variables that are set by the
server, such as the current time on the server, the date the
page was last modified, and the environment variables typically
set for CGI programs to use. You can also plug in such variables
as the IP address or name the user's browser is running on,
the browser identification string, etc.
SERVER_SOFTWARE - The Web server software name and version
REMOTE_ADDR - The user's IP Address
REMOTE_HOST - The user's domain name (not always known by the server)
HTTP_USER_AGENT - The user's browser and OS identification string
SERVER_NAME - The domain name of the Web server
Apache
has extra strength SSI which offers basic scripting
elements, such as variables and IF statements. It's quite simple
to set up Apache so that you can have different sections of HTML
inserted into your page depending on which browser version the
visitor is using. This comes in especially handy for using
style sheets and
DHTML.
You can use it to deliver different HTML to different browsers,
for example sending DHTML pages to higher level browsers, and
tamer HTML (THTML?) to lesser browsers. Another useful trick
would be to use different style sheets, or an alternate
page without style sheets, depending on whether a visitor is
viewing on a PC or Mac.
So, golly, how much woudja pay for a useful and flexible little
tool like this? Hold onto your hats folks, it's free! SSI is
built into all the most popular server packages - Apache,
Netscape, IIS, Website, and others. If you need a quick and
easy way to insert text, HTML or scripting into a Web page,
just SSI it.
SSI is no free lunch, of course. It puts an extra load on the
server, since the server has to parse the page, whereas without
SSIs the server can dump it straight to the browser without even
looking at what's in it.
As a way of building dynamic pages, SSI isn't very efficient.
It's even worse when you run several programs via SSI on one
page, because each one is a separate process. Each SSI program
you include on the page at least doubles the server load for
viewing that page once. Although this is inefficient, it's easy
and cheap to do. A preferable solution is one where the entire
page is generated by a single process, preferably a process
which is persistent - it starts running when the server is
started, rather than being run again each time the page is
viewed. FastCGI, ASP, Server-side Java, PhP and ColdFusion are
examples of technologies which are better for building a fully
dynamic site.
SSI is a quick fix, not a long-term dynamic solution. But it's
free and it's easy, so it's a good widget to have in your toolkit.
Although PhP and some other alternatives are free also, SSI is
simple enough that it doesn't require programming to use, and
is easy to set up if your hosting provider lets you. Some
hosting providers, especially the cheaper ones, don't allow
you that level of control because of the server load and
potential security risks.
Additional Resources:
Kief Morris, technical director of The Web Developer's Journal, contributed to this article.
Searchin'
Content Management Tips and Tricks
|