Your Daily Source for Apache News and Information  
Breaking News Preferences Contribute Triggers Link Us Search About
Apache Today [Your Apache News Source] To internet.com

Apache HTTPD Links
The Java Apache Project
The Jakarta Project
Apache-Perl Integration Project
Apache XML Project
ApacheCon
Apache-Related Projects
The Apache FAQ
PHP Server Side Scripting
The Apache Software Foundation
Apache Module Registry
Apache Project
The Linux Channel at internet.com
Linux Start
Linux Today
Linuxnewbie.org
Enterprise Linux Today
All Linux Devices
BSD Central
Linux Programming
PHPBuilder
BSD Today
Linux Apps
Apache Today
Just Linux
Linux Planet
Linux Central
SITE DESCRIPTIONS
Apache Guide: Introduction to Server Side Includes, Part 2
Jun 19, 2000, 11 :05 UTC (8 Talkback[s]) (16547 reads) (Other stories by Rich Bowen)

By

This is the second of a three-article series on SSI. In the first article, we talked about how to enable SSI on your server and passed on some very basic examples. In this article, there will be more examples, and we'll talk about some somewhat more involved things that you might want to do with SSI. In the final article, we'll talk about the more advanced features of Apache SSI, including conditional statements.

When was This Document Modified?

In the last article, we mentioned that you could use SSI to inform the user when the document was most recently modified. However, the actual method for doing that was left somewhat in question. The following code, placed in your HTML document, will put such a time stamp on your page. Of course, you will have to have SSI correctly enabled, as discussed in the last article.

        <!--#config timefmt="%A %B %d, %Y" -->
        This file last modified <!--#flastmod file="ssi.shtml" -->

Of course, you will need to replace the ssi.shtml with the actual name of the file that you're referring to. This can be inconvenient if you're just looking for a generic piece of code that you can paste into any file, so you probably want to use the LAST_MODIFIED variable instead:

        <!--#config timefmt="%D" -->
        This file last modified <!--#echo var="LAST_MODIFIED" -->

For more details on the timefmt format, go to your favorite search site and look for ctime. The syntax is the same.

Including a Standard Footer

If you are managing any site that is more than a few pages, you may find that making changes to all those pages can be a real pain, particularly if you are trying to maintain some kind of standard look across all those pages.

Using an include file for a header and/or a footer can reduce the burden of these updates. You just have to make one footer file, and then include it into each page with the include SSI command. The include element can determine what file to include with either the file attribute, or the vitrual attribute. The file attribute is a file path, relative to the current directory. That means that it cannot be an absolute file path (starting with /), nor can it contain ../ as part of that path. The virtual attribute is probably more useful, and should specify a URL relative to the document being served. It can start with a /, but must be on the same server as the file being served.

        <!--#include virtual="/footer.html" -->

I'll frequently combine the last two things, putting a LAST_MODIFIED directive inside a footer file to be included. SSI directives can be contained in the included file, and includes can be nested - that is, the included file can include another file, and so on.

What Else can I Config?

In addition to being able to config the time format, you can also config two other things.

Usually, when something goes wrong with your SSI directive, you get the message

        [an error occurred while processing this directive]

If you want to change that message to something else, you can do so with the errmsg attribute to the config element:

        <!--#config errmsg="[It appears that you don't know how to use SSI]" -->

Hopefully, end users will never see this message, because you will have resolved all the problems with your SSI directives before your site goes live. (Right?)

And you can config the format in which file sizes are returned with the sizefmt attribute. You can specify bytes for a full count in bytes, or abbrev for an abbreviated number in Kb or Mb, as appropriate.

Executing Commands

I expect that I'll have an article some time in the coming months about using SSI with small CGI programs. For now, here's something else that you can do with the exec element. You can actually have SSI execute a command using the shell (/bin/sh, to be precise--or the DOS shell, if you're on Win32). The following, for example will give you a directory listing.

        <pre>
        <!--#exec cmd="ls" -->
        </pre>

or, on Windows

        <pre>
        <!--#exec cmd="dir" -->
        </pre>

You might notice some strange formatting with this directive on Windows, because the output from dir contains the string "<dir>" in it, which confuses browsers.

Note that this feature is exceedingly dangerous, as it will execute whatever code happens to be embedded in the exec tag. If you have any situation where users can edit content on your web pages, such as with a guestbook, for example, make sure that you have this feature disabled. You can allow SSI, but not the exec feature, with the IncludesNOEXEC argument to the Options directive.

What's Next?

In the next column, I'll talk about some of the more advanced features of SSI, in particular, the flow-control feature (conditional statements) and using variables.

Rich Bowen is the Director of Web Application Development at The Creative Group and the author of Apache Server Unleashed.

  Current Newswire:
Apache 2.0.32 beta is available

Everything Solaris: Apache: The Basics

Apache Jakarta James Mailserver v2.0a2 Released

PostgreSQL v7.2 Final Release

Daemon News: Multiple webservers behind one IP address

Zend Technologies launches Zend Studio 2.0

NuSphere first to enable development of PHP web services

Covalent Technologies raises $18 million in venture capital

Apache 1.3.23 released

wdvl: Build Your Own Database Driven Website Using PHP and MySQL: Part 4

 Talkback(s) Name  Date
  ssi
server side include.
  
  Jun 19, 2000, 15:49:37
  File Last Modified
A way that I have found to have a piece of code that I can paste in to any file and still have the ability to reference another file if needed is to have this:

This document last modified:

This way, if I do need to reference another file, I can just change ${DOCUMENT_NAME}. I do not have to change the entire string (ie: This file last modified )   
  Jun 19, 2000, 15:59:18
   Re: File Last Modified
> A way that I have found to have a piece of code that I can paste in to any file and still have the ability to reference another file if needed is to have this:
This document last modified:
This way, if I do need to reference another file, I can just change ${DOCUMENT_NAME}. I do not have to change the entire string (ie: <!--#echo var="LAST_MODIFIED" -->)

Code should have been:

<!--#flastmod file="${DOCUMENT_NAME}" -->


  
  Jun 19, 2000, 17:13:55
  Does this consume extra CPU cycles?
When you have a server-parsed html file (i.e., one that has an ssi in it), does the server take notice of the last time any of both files changed, or does it parse and include the files each time they are requested? If so, how much CPU time does that consume?   
  Jun 22, 2000, 07:37:08
   Re: Does this consume extra CPU cycles?
>> When you have a server-parsed html file (i.e., one that has an ssi in it), does the server take notice of the last time any of both files changed, or does it parse and include the files each time they are requested? If so, how much CPU time does that consume?<<

Yes, but keep it in perspective -- it's nothing when compared with CGI-generated dynamic pages, particularly Perl-generated dynamic pages. This is pretty lightweight stuff.

The HTML is examined for SSI directives and those directives are acted upon every time it is fed through the Web server; nothing is preassembled, compiled or cached.

There is some overhead in parsing the HTML, obviously, even if there are no SSI directives -- that's why most Web servers are configured to parse .shtml files and not all .html files. However, it's not horrendous, and it's a problem that can be solved easily with a faster server.

At the company where I work, ALL HTML files are run through the SSI parser, and we serve millions of pages per day.

The parsing is actually less expensive in terms of CPU than many of the actions you take as a result of the parsing. Performing a file #include, for example, is radically more expensive than #echo var. Performing #exec is even more expensive.

You can test some of these points yourself by using the Apache benchmark utility. On most Linux distributions it will be installed as /usr/sbin/ab. Set up a couple of different test cases and ask ab to bang on the resulting URLs ten or twenty thousand times and you'll get some interesting numbers.
  
  Jun 25, 2000, 14:13:14
  Include File Paths
I don't understand why if the include file is a relative path why the path can't start with ../

One could at the doc root level have a directory where all include files sit. How does an html file in a different directory the same level relative to doc root call the file?

Example:

docroot/includes/include.txt
docroot/content/index.shtml <- How does this file call the include.txt file?

Regards,

Fek   
  Sep 23, 2000, 05:47:39
   Re: File Last Modified
> A way that I have found to have a piece of code that I can paste in to any file and still have the ability to reference another file if needed is to have this:
This document last modified:
This way, if I do need to reference another file, I can just change ${DOCUMENT_NAME}. I do not have to change the entire string (ie: This file last modified )

What i think for this simple function it is not really necessary to use the SSi and handled by server, just put 1 line of javascript:
document.write(document.lastModified)
then you can get same result.
I agree using SSI but not necessary for the simple function. This also reduced the server's workload and let client to share.   
  Feb 1, 2001, 06:17:31
  include file paths: somebody please help
I am trying to use SSI's and am experiencing difficulties. I have set up a series of html files and stored them in an includes folder in my root directory (ie. root/includes/header.html) These html files are basic tables that are to be used for navigation throughout the site. Multiple pages use the same navigation bar and I don't want to have to change it in 50 spots if I want to make one change.

It works in these file paths:
root/events/default.shtm
root/music/default.shtm

When I try to create subfolders the pictures for the navigation bar no longer work:
root/events/event1/default.shtm - doesn't work


I know i am not doing a great job of explaining the problem, but i was hoping somebody could offer some insight.
  
  Sep 17, 2001, 21:53:46
Enter your comments below.
Your Name: Your Email Address:


Subject: CC: [will also send this talkback to an E-Mail address]
Comments:

See our talkback-policy for or guidelines on talkback content.

About Triggers Media Kit Security Triggers Login


All times are recorded in UTC.
Linux is a trademark of Linus Torvalds.
Powered by Linux 2.4, Apache 1.3, and PHP 4
Copyright 2002 INT Media Group, Incorporated All Rights Reserved.
Legal Notices,  Licensing, Reprints, & Permissions,  Privacy Policy.
http://www.internet.com/