TweetFollow Us on Twitter

Server Side Includes

Volume Number: 15 (1999)
Issue Number: 8
Column Tag: Web Development

Server-Side Includes

by Rich Morin and Vicki Brown

Generating A Dynamic Appearance in Static HTML

A Desire for Consistency

We realize that static web pages are passé, but we still need to generate them from time to time. We also like to have a certain consistency (and controlled variability) to our web pages. So, we use server-parsed HTML documents, more commonly known as server-side includes, to manage page headers and footers.

Server-side includes (SSIs) provide a convenient way to embed variables (such as the current time, date, URL, or size of the current page) into any HTML document "on the fly". SSIs can also be used to insert the contents of "boilerplate" files into the current document. This makes them an excellent choice for storing common information, such as Copyright notices, contact information, and navigation bars.

SSI support is provided by several of the common Web servers, including Apache http://www.apache.org for Unixish systems, WebTen http://www.tenon.com and WebSTAR http://www.starnine.com for Mac OS, and WebSite http://www.oreilly.com; for Microsoft Windows. If you use a different server, consult your documentation to see if it supports SSIs.

[Footnote: Tenon's WebTen 2.1.9 is a point-and-click Web server based on Apache 1.2.6].

A sample web page (as the user's browser receives it) might look as follows:

Sample web page as seen in the browser

	<HTML>
	  <HEAD>
	    <TITLE>
	      Sample Web Page
	    </TITLE>
	  </HEAD>
 
 	  <H1>Sample Web Page</H1>
	  <HR>

	  <H2>Welcome to a Sample Web Page!</H2>
	  <P>
	    With some typical sample text...

	    <P><HR><P>
	   Copyright 1999 Prime Time Freeware<BR>
	      Send comments, inquiries, or trouble reports to
<A HREF="mailto:www@ptf.com?subject=http://www.ptf.com/sample.shtml">www@ptf.com</A>.
	  </BODY>
	</HTML>

This is generated by a much smaller HTML file (sample.shtml):

sample.shtml

	<!-#set var="page_title" value="Typical Web Page"  ->
	<!-#set var="page_uri"   value="${DOCUMENT_URI}"   ->
	<!-#include virtual="/h.shtml"                     ->

	  <H2>Welcome to a Typical Web Page!</H2>
	  <P>
	    With some typical sample text...
	  <!-#include virtual="/t.shtml"->

The man behind the curtain...

We'll agree with anyone who says that the syntax is clunky. On the other hand, it works reasonably well and is clear, once you understand the ground rules.

The code sets two variables (page_title and page_uri). The first is just a text string ("Typical Web Page"). The second, however, is a bit more involved.

We want the "mailto" HREF at the end of the page to include a subject line; specifically, the URL for the original HTML file. We can't hard-code the URL into the trailer file, however, because we want to use the same trailer file for many pages. Worse, the calling page's URI (Uniform Resource Identifier; the "local" portion of the URL) is not available to the trailer file.

We work around the problem by saving the value of the calling file's DOCUMENT_URI in page_uri. Our trailer file (/t.shtml) can then pick up and use this "global" information.

Once the variables have been defined, sample.shtml can invoke the header file (/h.shtml), fill in some body text, and invoke the trailer file (/t.shtml).

Now for the underlying details. First, the header file (/h.shtml):

/h.shtml	
<HTML>
	  <HEAD>
	    <TITLE>
	      <!-#echo var="page_title"->
	    </TITLE>
	  </HEAD>

	    <H1><!-#echo var="page_title"-></H1>
	    <HR>

This file combines "constant" text with echos of an inherited variable (page_title). Conveniently, we can use page_title twice.

The trailer file (/t.shtml) applies the same trick to generate the desired URL in the mailto's Subject line:

/t.shtml	   
<P><HR><P>
	   Copyright 1999 Prime Time Freeware<BR>
	      Send comments, inquiries, or trouble reports to
<A HREF="mailto:www@ptf.com?subject=http://www.ptf.com<!-#echo

	  var="page_uri"->">www@ptf.com</A>.
	  </BODY>
	</HTML>
	
Server-side includes also allow conditional evaluation (roughly, flow control), which we won't try to cover here. The Apache web pages (http://www.apache.org, http://www.apache.org/docs-1.2/mod/mod_include.html) are a big help, even if they get a bit terse from time to time.

How Do I Make Them Work?

Depending on which server you use, server-side includes may not be enabled by default. Although we develop many of our Web pages on a Macintosh, we deploy them on Apache (running on a FreeBSD server). In Apache, setting up server-side includes requires a bit of administrative effort, mostly in editing configuration files. If you use another server, consult your documentation for how to configure the server-side include mechanism.

The configuration process for Apache can be a little tricky, as there are several steps you must go through. If you don't have an Apache wizard to call upon, you may want to use the following recipe. (Even if you don't use Apache, you may want to skim the following section, as it will give you some clues to what is going on "under the covers".)

In the srm.conf file, uncomment the AddHandler and Addtype lines for .shtml files:

# To use server-parsed HTML files
AddType text/html .shtml
AddHandler server-parsed .shtml

Note that neither the server nor the browser really care what suffix you use for these files, only that you specify it here. You could specify the conventional .html ending here, in which case all of your .html files would be scanned for include directives. This causes a lot of extra work for your server, however, and may hurt your response times. Besides, use of a special file name extension is a good way to remind yourself which files use SSIs!

Next, create (or modify) the Directory Options directive in either httpd.conf or access.conf. (we recommend htpd.conf). In the Directory part, be sure to name the root of the directory tree for which server-side includes will be enabled. Note that this must be a full, OS (not WWW) path name; that is, it is rooted in the filesystem, not the DocumentRoot.

<Directory /usr/local/Server/WWW/web>
Options Indexes IncludesNoExec FollowSymLinks
</Directory>

It is very important to get the options right. Most Apache options are on (by default) if you don't explicitly set them; only a few are not. However, if you do explicitly set certain options, you must then explicitly set the all others you want. That is, the defaults only work as long as you use only the defaults.

Options, in detail...

To enable SSI, you must choose one of two possible inclusion options. To enable all forms of server-side includes, use the Includes option. This option turns on variables as well as allowing program (CGI) execution. If you just want variable substitution and file inclusion, but not program execution, use IncludesNoExec. This option only controls CGI execution from inside .shtml files; it has no connection to general CGI execution (e.g., from a URL).

You must also explicitly set several other options if you wish to use the associated features. If you want to be able to execute server-side CGI scripts from any directory (not just cgi-bin), you'll need to set the option ExecCGI. This option is related to the SSI options, but also controls execution of CGI scripts, exclusive of .html files. Consider wisely, as opening up CGI execution could lead to problems (e.g., if you have many users creating their own pages).

Set the Indexes option if you want to allow the web server to access directories that do not have an explicit index.html file. If you intend to use symbolic links (aliases, in Mac OS jargon) within your Web page hierarchy, be sure to set the FollowSymLinks option.

If you're unsure how the options will work, try them out. Create a few HTML directories with and without index.html files, add a few symbolic links, test out server-side includes with some variables, and try to execute a few CGI scripts. This is definitely a case where you can learn best by doing. You may also want to consult a book or two. How to Set Up and Maintain a Web Site, by Lincoln Stein (2nd. ed. with CD-ROM) is an excellent resource.

Try it Out

Now you're ready to include something! The format of a server side include (in your HTML code) is:

<!-#directive param1="value1" param2="value2" .. ->

Note that an include directive looks suspiciously like a comment; the difference is the hash mark and the directive immediately following the dashes at the front.

     <!- this is a comment ->
     <!-#include ... ->

The directive can be any of several choices. Apache supports the following directives; other servers may support more. Again, check your documentation.

config control and configure various aspects of SSI parsing
echo echo (print) the value of a variable
exec execute a named CGI script; the IncludesNoExec option disables this
include insert the text from another document at this point, unless including the document would cause a program to be executed and the IncludesNoExec option is set
fsize include the size of the specified file
flastmod include the last modification date of the specified file
printenv print out a listing of all environment variables and their values (Apache 1.2 and later)
set set the value of a variable (Apache 1.2 and later)

As you can see, SSIs provide a reasonable level of flexibility. With a simple SSI directive, you can easily include a dynamic "Last modified On..." line in your HTML documents. You can include any of the standard variables from the server's environment, or you can set, and include, your own variables.

	<!-#set var="page_title" value="Typical Web Page"  ->
	<!-#set var="page_uri"   value="${DOCUMENT_URI}"   ->

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

As we saw in the examples at the beginning of this discussion, we make extensive use of the variable setting (and echoing) capabilities of SSIs. Note that the set directive was added more recently than the others; if it is not available in your server implementation, you should contact your vendor to determine if an upgrade is available.

Including Files

Including files is perhaps even more useful than including variables. We like to have a standard "boilerplate" footer at the bottom of our pages, but what do we do if something changes? For example, we probably want to update our Copyright notice with the new year!

There are two ways to include the contents of a file; the syntax depends upon the type of file you are including. To include a file named in relation to the DocumentRoot or by full URL, use the virtual parameter.

<!-#include virtual="/t.shtml"->

(It may seem odd to use the word "virtual" when you are specifying an absolute location in the file tree hierarchy, but that's the way it is). Alternatively, to include a file from the current directory, use the file parameter.

<!-#include file="righthere.html"->

In neither case can you include the contents of a file beyond the server root. In addition, the file parameter will accept only relative pathnames from the current point downwards, rejecting both absolute pathnames as well as any path beginning with "..".

If the file you are including also makes use of SSIs, its name must also end in the .shtml extension. When we include t.shtml, it resolves the page_uri variable to dynamically set the webmaster's mailto link at the bottom of the page - includes within includes!

t.shtml with variable-setting

	   <P><HR><P>
	   Copyright 1999 Prime Time Freeware<BR>
	      Send comments, inquiries, or trouble reports to
	<A HREF="mailto:www@ptf.com?subject=http://www.ptf.com<!-#echo
	  var="page_uri"->">www@ptf.com</A>.
	  </BODY>
	</HTML>

Bibliography and References

Apache is the industry standard Open Source Web Server, used by more than half the hosts on the Internet. See http://www.apache.org for additional information and details. See http://www.apache.org/docs-1.2/mod/mod_include.html for details regarding server-side includes.


Rich Morin has been programming computers for thirty years. He has been a freelance computer programmer, consultant, technical writer and small-time entrepreneur since 1975. Rich currently operates Prime Time Freeware, a publisher of mixed-media book/CDs relating to Open Source software. Rich lives in San Bruno, on the San Francisco peninsula.

Vicki Brown has been programming computers for about 20 years now. She discovered Unix in 1983 and the Macintosh in 1986. Unix is her favorite OS, but the Mac OS is her favorite user interface. Vicki is currently employed as a Perl programmer for a Silicon Valley Biotech firm. She is the co-author of MacPerl: Power and Ease. When she’s not programming or writing, Vicki enjoys relaxing with her spouse and their two Maine Coon cats.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Recruit two powerful-sounding students t...
I am a fan of anime, and I hear about a lot that comes through, but one that escaped my attention until now is A Certain Scientific Railgun T, and that name is very enticing. If it's new to you too, then players of Blue Archive can get a hands-on... | Read more »
Top Hat Studios unveils a new gameplay t...
There are a lot of big games coming that you might be excited about, but one of those I am most interested in is Athenian Rhapsody because it looks delightfully silly. The developers behind this project, the rather fancy-sounding Top Hat Studios,... | Read more »
Bound through time on the hunt for sneak...
Have you ever sat down and wondered what would happen if Dr Who and Sherlock Holmes went on an adventure? Well, besides probably being the best mash-up of English fiction, you'd get the Hidden Through Time series, and now Rogueside has announced... | Read more »
The secrets of Penacony might soon come...
Version 2.2 of Honkai: Star Rail is on the horizon and brings the culmination of the Penacony adventure after quite the escalation in the latest story quests. To help you through this new expansion is the introduction of two powerful new... | Read more »
The Legend of Heroes: Trails of Cold Ste...
I adore game series that have connecting lore and stories, which of course means the Legend of Heroes is very dear to me, Trails lore has been building for two decades. Excitedly, the next stage is upon us as Userjoy has announced the upcoming... | Read more »
Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »

Price Scanner via MacPrices.net

May 2024 Apple Education discounts on MacBook...
If you’re a student, teacher, or staff member at any educational institution, you can use your .edu email address when ordering at Apple Education to take up to $300 off the purchase of a new MacBook... Read more
Clearance 16-inch M2 Pro MacBook Pros in stoc...
Apple has clearance 16″ M2 Pro MacBook Pros available in their Certified Refurbished store starting at $2049 and ranging up to $450 off original MSRP. Each model features a new outer case, shipping... Read more
Save $300 at Apple on 14-inch M3 MacBook Pros...
Apple has 14″ M3 MacBook Pros with 16GB of RAM, Certified Refurbished, available for $270-$300 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year warranty is... Read more
Apple continues to offer 14-inch M3 MacBook P...
Apple has 14″ M3 MacBook Pros, Certified Refurbished, available starting at only $1359 and ranging up to $270 off MSRP. Each model features a new outer case, shipping is free, and an Apple 1-year... Read more
Apple AirPods Pro with USB-C return to all-ti...
Amazon has Apple’s AirPods Pro with USB-C in stock and on sale for $179.99 including free shipping. Their price is $70 (28%) off MSRP, and it’s currently the lowest price available for new AirPods... Read more
Apple Magic Keyboards for iPads are on sale f...
Amazon has Apple Magic Keyboards for iPads on sale today for up to $70 off MSRP, shipping included: – Magic Keyboard for 10th-generation Apple iPad: $199, save $50 – Magic Keyboard for 11″ iPad Pro/... Read more
Apple’s 13-inch M2 MacBook Airs return to rec...
Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices currently... Read more
Best Buy is clearing out iPad Airs for up to...
In advance of next week’s probably release of new and updated iPad Airs, Best Buy has 10.9″ M1 WiFi iPad Airs on record-low sale prices for up to $200 off Apple’s MSRP, starting at $399. Sale prices... Read more
Every version of Apple Pencil is on sale toda...
Best Buy has all Apple Pencils on sale today for $79, ranging up to 39% off MSRP for some models. Sale prices for online orders only, in-store prices may vary. Order online and choose free shipping... Read more
Sunday Sale: Apple Studio Display with Standa...
Amazon has the standard-glass Apple Studio Display on sale for $300 off MSRP for a limited time. Shipping is free: – Studio Display (Standard glass): $1299.97 $300 off MSRP For the latest prices and... Read more

Jobs Board

Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
*Apple* App Developer - Datrose (United Stat...
…year experiencein programming and have computer knowledge with SWIFT. Job Responsibilites: Apple App Developer is expected to support essential tasks for the RxASL Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.