home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

HTML 4.0 Sourcebook
(Publisher: John Wiley & Sons, Inc.)
Author(s): Ian S. Graham
ISBN: 0471257249
Publication Date: 04/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Example 27: Server-Side Includes

The following example (shown in Figures 11.4 and 11.5) illustrates the use of server-side includes. The example consists of a main document stuff.shtml (the suffix .shtml indicates parsable HTML documents) that includes a second parsable document inc_file.shtml and that also executes the CGI program test_script.cgi. The listings for these examples are shown in Figure 11.4, while the browser rendering of the document stuff.shtml is shown in Figure 11.5.

In this example the document stuff.shtml is accessed using the URL:

<http://leonardo:8080/stuff.shtml/extra/path/info?arg1+arg2>

Note that this passes query strings and extra path information to stuff.shtml, as if it were a gateway program (see Chapter 10 for more information about CGI programs and passed variables)—thus this information is available, within the HTML parsing phase, via environment variables. All the example documents are designed to print these environment variables and display them within the returned document. As seen in Figure 11.4, these variables are empty inside the parsable HTML document. However, the bottom of Figure 11.5 shows that these variables are present inside the CGI program (in the environment variables QUERY_STRING and PATH_INFO) executed from within the parsable document. You can, therefore, access a parsable document and, through it, pass query information to a CGI program, just as if you were accessing the CGI program directly.

Figures 11.4 and 11.5 also illustrate the include and echo commands. These are useful for printing information about local files. However, you really want to do this as sparingly as possible, as every such request slows down the server. For example, it is a waste of server resources to use the LAST_MODIFIED variable to display the last time you edited a simple HTML document, since you could just as easily add this information while editing it.

For additional information about server-side includes and how to configure the NCSA server to support server-side includes, please consult the NCSA online documentation at:

hoohoo.ncsa.uiuc.edu/docs/tutorials/includes.html

Some Example CGI Programs

This section presents a few example CGI programs, representing some of the common uses of the CGI facility. The first example is a program that returns a document containing a count of the number of times the page was accessed. The second uses a gateway program and server-side includes to insert a randomly selected HTML snippet into an HTML document. The third and last example looks at WebNotice, a large-scale gateway-program application, designed by the author. This package integrates 28 gateway programs, 19 HTML document templates, and 10 HTML documents to create a Web-based system for depositing and viewing notices to be made available to the public.

Example 28: Page Access Counter

The simple perl script of this example answers the common question, “How do I include, within my page, a number indicating how many times the document has been accessed?” This program reads in the document to be returned to the client and edits it, replacing a dummy comment string in the document (the string <!-- counter -->) with the desired count. The count is obtained from a log file that


Figure 11.4 Example of server-side includes, showing the listings for three example documents. The main file is stuff.shtml, which includes the file inc_file.shtml and also the output of the CGI program test_script.cgi. The resulting HTML document, upon accessing the URL http://leonardo:8080/stuff.shtml/extra/path/info?arg1+arg2 is shown in Figure 11.5.

1. stuff.shtml

<html>
<head>
<title> Test of NCSA Server-side Includes </title>
<body>
<h1> Test of NCSA Server-side Includes </h1>
<pre>
Stuff.shtml was last modified:  <!--#flastmod virtual=“/stuff.shtml”  -->.
Size of stuff.shtml is: <!--#fsize file=“stuff.shtml”  -->.
DOCUMENT_NAME = <!--#echo var=“DOCUMENT_NAME” -->
DOCUMENT_URI =  <!--#echo var=“DOCUMENT_URI” -->
DATE_LOCAL =    <!--#echo var=“DATE_LOCAL” -->
QUERY_STRING =  <!--#echo var=“QUERY_STRING” -->
PATH_LOCAL =    <!--#echo var=“QUERY_STRING” -->
DATE_GMT =      <!--#echo var=“DATE_GMT” -->
LAST_MODIFIED = <!--#echo var=“LAST_MODIFIED” -->
</pre>

<!--#config errmsg=“Unable to parse scripts”  -->

<p><em>....now include inc_example.shtml....</em>

<!--#include file=“inc_file.shtml” -->

<p> <em>..... now include test_script.cgi CGI program output...... </em>

<!--#exec cgi=“/cgi-bin/test_script.cgi” -->
</body>
</html>

2. inc_file.shtml

<pre>
Inc_file.shtml last modified:   <!--#flastmod virtual=“/inc_file.shtml”-->.
Size of inc_file.shtml is:      <!--#fsize file=“inc_file.shtml”  -->.
DOCUMENT_NAME:  <!--#echo var=“DOCUMENT_NAME” -->
DOCUMENT_URI:       <!--#echo var=“DOCUMENT_URI” -->
DATE_LOCAL:     <!--#echo var=“DATE_LOCAL” -->
DATE_GMT        <!--#echo var=“DATE_GMT” -->
LAST_MODIFIED   <!--#echo var=“LAST_MODIFIED” -->

</pre>

3. test_script.cgi (in the cgi-bin directory)

#!/bin/sh

echo “Content-type: text/html”
echo
echo “<pre>”
echo “This is  CGI script output.”
echo “QUERY_STRING is \”$QUERY_STRING\“.”
echo “PATH_INFO =  \”$PATH_INFO\“. ”
echo “</pre>”

tracks the number of times the file has been accessed. The gateway program opens this log file and increments the counter by 1. The program listing is found in Figure 11.6.


Figure 11.5  Browser rendering of the server-side executable document stuff.shtml when accessed using the URL http://leonardo:8080/stuff.shtml/extra/path/info?arg1+arg2

To use this gateway program, you must access a document using a URL of the form

http://some.where.edu/cgi-bin/counter.pl/path/file.html

where path/file.html is the path to the document, relative to the root of the server document directory.

The specific location of the file to be returned is contained within the PATH_TRANSLATED environment variable; this value is placed into the local variable $path at line 3. Line 2 checks to make sure this variable actually exists—if it does not, there was no extra path information in the URL, meaning the author forgot to reference a file. Lines 8 through 11 process the variable $path, to extract the path to the directory containing the document being returned ($path) as well as the name of the file being returned. The count file is given the same name as the file, but preceded by a dot. Thus if the file being returned is home.html, then the count file will be named .home.html. Every file has its own distinct count file, with a matching name.

Line 15 checks to see if the count file exists—if it does not, lines 16 through 19 create it and give it an initial value of 0.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.