📄 ch7.htm
字号:
by issuing the command <TT><FONT FACE="Courier">kill -SIGHUP 'cat
httpd-pid<B>'</B></FONT></TT> and that's it-your server now supports
SSI.
</OL>
<P>
The preceding configuration is the preferred one, but you can
configure the CERN server to parse all your <TT><FONT FACE="Courier">.html</FONT></TT>
files with the directive <TT><FONT FACE="Courier">Exec /*.html
/cgi-bin/fakessi.pl</FONT></TT>. Be careful about using <TT><FONT FACE="Courier">/*</FONT></TT>
instead of <TT><FONT FACE="Courier">/*.html</FONT></TT> because
with this, all the files would be parsed-even graphics files!
The <TT><FONT FACE="Courier">fakessi.pl</FONT></TT> is reported
not to work well with this kind of parsing.
<H3><A NAME="Apache">Apache</A></H3>
<P>
From the beginning, Apache was designed to provide SSI functionality.
Configuring this server in order to actually use it is quite simple:
<OL>
<LI>Edit your <TT><FONT FACE="Courier">access.conf</FONT></TT>
file and include the option Includes in the Options line of your
document hierarchy. It should be something like <TT><FONT FACE="Courier">Options
Indexes FollowSymLinks Includes</FONT></TT>. Insert a line with
<TT><FONT FACE="Courier">XBitHack Full</FONT></TT>, too.
<LI>Edit your <TT><FONT FACE="Courier">srm.conf</FONT></TT> file,
go to the AddType section, and put in <TT><FONT FACE="Courier">Addtype
application/x-httpd-cgi .cgi</FONT></TT> and <TT><FONT FACE="Courier">Addtype
text/x-server-parsed-html .shtml</FONT></TT>.
<LI>Enter the <TT><FONT FACE="Courier">kill -SIGHUP 'cat httpd.pid'</FONT></TT>
command, making your server reload its configuration.
</OL>
<P>
You have just configured the Apache server to parse <TT><FONT FACE="Courier">.shtml</FONT></TT>
files and files with the x bit on. You could use only one of these
alternatives if you prefer to, by either forgetting the <TT><FONT FACE="Courier">XBitHack</FONT></TT>
or the <TT><FONT FACE="Courier">AddType</FONT></TT> <TT><FONT FACE="Courier">.shtml</FONT></TT>
directive. In order to test the SSI feature, you should create
an <TT><FONT FACE="Courier">.shtml</FONT></TT> file or an <TT><FONT FACE="Courier">.html</FONT></TT>
with the execution attribute (x bit) turned on (<TT><FONT FACE="Courier">chmod
700</FONT></TT> the file on UNIX systems).
<H2><A NAME="AListofUsefulSSIDirectives"><FONT SIZE=5 COLOR=#FF0000>A
List of Useful SSI Directives</FONT></A></H2>
<P>
As you have seen previously, the format of an SSI directive is
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><!--#command argument="value"--></FONT></TT>
</BLOCKQUOTE>
<P>
Following is the list of the six possible <TT><FONT FACE="Courier">command</FONT></TT>s
available in SSI, along with the arguments and values they allow:
<UL>
<LI><TT><B><FONT FACE="Courier">include<BR>
</FONT></B></TT>The only supported argument is <TT><FONT FACE="Courier">file</FONT></TT>,
which allows the specification of a path to an HTML document (relative
to the current directory). The value is the path of the document
you want to include. This document should be either normal ASCII
text or HTML tags (no need for the <TT><FONT FACE="Courier"><HTML></FONT></TT>
or <TT><FONT FACE="Courier"><BODY></FONT></TT> tags, just
put the tags you would normally put in the <TT><FONT FACE="Courier"><BODY></FONT></TT>
section of a file). For example, if you want to include a file
called <TT><FONT FACE="Courier">nav_bar.html</FONT></TT>, you
would use <TT><FONT FACE="Courier"><!--#include file="nav_bar.html"--></FONT></TT>
in the exact place of the main file where you want the contents
of the file <TT><FONT FACE="Courier">nav_bar.html</FONT></TT>
to appear. See Figure 7.1 and 7.2 for an illustration of this
example.
</UL>
<P>
<A HREF="f7-1.gif" ><B>Figure 7.1:</B> <I>The HTML source of the document displaying a navigation bar.</I></A>
<P>
<A HREF="f7-2.gif" ><B>Figure 7.2:</B> <I>A page with a navigation bar.</I></A>
<UL>
<LI> The nav_bar.html file in this
example contains only<BR>
<A HREF="/">HOME</A> | <A HREF="index.html">BACK</A>
|<BR>
<A HREF="something.html">NEXT</A> | <A
HREF="mailform.html">MAIL-US</A>
<LI><TT><B><FONT FACE="Courier">echo<BR>
</FONT></B></TT>The only supported argument is <TT><FONT FACE="Courier">var</FONT></TT><I>.</I>
This allows the printing of one of the include variables (the
values) that the server makes available, such as <TT><FONT FACE="Courier">DOCUMENT_NAME</FONT></TT>:
The current filename.<BR>
<TT><FONT FACE="Courier">DOCUMENT_URI</FONT></TT>: The virtual
path to the SSI document.<BR>
<TT><FONT FACE="Courier">DATE_LOCAL</FONT></TT>: The local date.
<BR>
<TT><FONT FACE="Courier">DATE_GMT</FONT></TT>: The GMT (Greenwich
Mean Time) date.<BR>
<TT><FONT FACE="Courier">LAST_MODIFIED</FONT></TT>: The last modification
date of the SSI container document. This is the variable that
allows the printing of the last modified date on a document<BR>
<TT><FONT FACE="Courier">This document was last updated on<BR>
<!--#echo var="LAST_MODIFIED"--><BR>
</FONT></TT>which results in<BR>
<TT><FONT FACE="Courier">This document was last updated on Monday,
13-May-96 21:15:00<BR>
HTTP_USER_AGENT</FONT></TT>: The name of the browser used to display
the document.<BR>
Other variables available are described in ncSA's CGI Environment
Variables at <TT><FONT FACE="Courier"><A HREF="http://hoohoo.ncsa.uiuc.edu/cgi/env.html">http://hoohoo.ncsa.uiuc.edu/cgi/env.html</A></FONT></TT>.
<LI><TT><B><FONT FACE="Courier">exec<BR>
</FONT></B></TT>The two supported arguments are <TT><FONT FACE="Courier">cgi</FONT></TT>
and <TT><FONT FACE="Courier">cmd</FONT></TT>. The first one may
receive an executable CGI program path as a value (<TT><FONT FACE="Courier">/cgi-bin/finger.cgi</FONT></TT>,
for example) and the second one a system command, such as <TT><FONT FACE="Courier">ls</FONT></TT>
or <TT><FONT FACE="Courier">cat</FONT></TT>. The <TT><FONT FACE="Courier">exec</FONT></TT>
directive is one of the most powerful and allows the execution
of any program whose results are to be displayed on-screen. This
is the way to go if you want to have a visitor counter on your
home page! Just use something like <TT><FONT FACE="Courier"><!--#exec
cgi="/cgi-bin/counter.cgi"--></FONT></TT> on your
document. The <TT><FONT FACE="Courier">counter.cgi</FONT></TT>
script should read a file containing the number of visitors, display
it, and update (increment) the file.<BR>
Remember that some sites may have the <TT><FONT FACE="Courier">exec</FONT></TT>
directive turned off, or they may not allow the use of CGI programs
on user's directories. If you are not the Webmaster of the server
you use, check with him or her to see if you can use this directive.
The reason for this is that the <TT><FONT FACE="Courier">exec</FONT></TT>
directive can be a serious security problem if it is not monitored
to prevent bad uses.
<LI><TT><B><FONT FACE="Courier">config<BR>
</FONT></B></TT>This directive informs the server of how to handle
various aspects of file parsing or result displaying. There are
two supported arguments:<BR>
<TT><FONT FACE="Courier">timefmt</FONT></TT>, which determines
the format to display dates. The value should be a string as specified
in the <TT><FONT FACE="Courier">strftime</FONT></TT> library.
Execute the command <TT><FONT FACE="Courier">man strftime</FONT></TT>
in your UNIX system, so that you can see the list of possibilities.
In a non-UNIX system, use the on-line help, if available.<BR>
<TT><FONT FACE="Courier">Sizefmt</FONT></TT>, which determines
the format to display file sizes. The values allowed are <TT><FONT FACE="Courier">bytes</FONT></TT>
(formats the size display for the number of bytes a file occupies)
or <TT><FONT FACE="Courier">abbrev</FONT></TT> (formats the size
display for the number of kilobytes and megabytes a file occupies).
<LI><TT><B><FONT FACE="Courier">fsize<BR>
</FONT></B></TT>As the name implies, this displays the size of
a given file. The only argument supported is <TT><FONT FACE="Courier">file</FONT></TT>,
and the value is the path (relative to the current directory)
to the file for which you want the size to be displayed.
<LI><TT><B><FONT FACE="Courier">flastmod<BR>
</FONT></B></TT>Prints the last modification date for the specified
file. The only argument supported is <TT><FONT FACE="Courier">file</FONT></TT>,
and the value is the path (relative to the current directory)
to the desired file.
</UL>
<H2><A NAME="SomeSSIExamples"><FONT SIZE=5 COLOR=#FF0000>Some
SSI Examples</FONT></A></H2>
<P>
Having seen the various possibilities for SSI directives, let's
take a look at two examples: a visitor counter and a random image
generator.
<P>
For both examples, we will present the program listing, the SSI
tags, and the resulting page.
<P>
Because both programs are expected to output HTML text, they must
inform the server using the <TT><FONT FACE="Courier">Content-Type:
text/html</FONT></TT> MIME header followed by two newline characters
(represented by <TT><FONT FACE="Courier">\n</FONT></TT>, usually)
before the actual result. Don't forget to output the correct MIME
header each time you develop a script that will be used by an
exec SSI directive; otherwise, your Web server could output an
error message instead of the desired result.
<H3><A NAME="Counter">Counter</A></H3>
<P>
A visitor counter is probably the most used SSI feature; at least,
it is one of the most desired. See for yourself on the <TT><FONT FACE="Courier">comp.infosystems.www.authoring.*</FONT></TT>
groups. The aim of this SSI tag and corresponding script is to
display the actual page with a counter embedded (in the top or
in the bottom, usually), updated each time someone requests the
page.
<P>
The SSI part is only the tag
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><!--#exec cgi="/cgi-bin/counter.cgi"--></FONT></TT>
</BLOCKQUOTE>
<P>
which you should insert in your HTML document in the place you
want the counter to appear. You can complete it with the surrounding
string
<BLOCKQUOTE>
<TT><FONT FACE="Courier">Welcome, you are the user number</FONT></TT>
</BLOCKQUOTE>
<BLOCKQUOTE>
<!--#exec cgi="/cgi-bin/counter.cgi"-->
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">to visit these pages!</FONT></TT>
</BLOCKQUOTE>
<P>
Then, there is the counter program itself, which is called <TT><FONT FACE="Courier">counter.cgi</FONT></TT>.
It is executed each time the page is requested. For this example,
I have chosen to develop a simple script (see Listing 7.1) in
Perl. The <TT><FONT FACE="Courier">$count</FONT></TT>_file variable
(see the beginning of Listing 7.1) should have the complete path
to the file that keeps the counts, for example, <TT><FONT FACE="Courier">/usr/local/etc/httpd/logs/count_file</FONT></TT>.
<HR>
<BLOCKQUOTE>
<B>Listing 7.1. The counter script in Perl.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">#!/usr/bin/perl<BR>
#<BR>
# counter.cgi - A simple visitor counter<BR>
#<BR>
# amcf@esoterica.pt, March 96<BR>
<BR>
# Place the file in a directory which the web server can access
<BR>
$count_file="/somewhere/count_file";<BR>
<BR>
open(CFILE, $count_file);<BR>
@counts=<CFILE>;<BR>
close(CFILE);<BR>
<BR>
$doc=$ENV{'DOCUMENT_URI'}; # HTTP_REFERER
works for CERN server<BR>
<BR>
# If it was called from the command line consider it an experience
<BR>
if ($doc eq "") { $doc = "experience" };<BR>
<BR>
# Aliases for the Homepage<BR>
if ($doc eq "/index.html") { $doc = "/" }
<BR>
<BR>
# Read the count file, pick the correct entry and increment it
<BR>
$found = 0;<BR>
for $line (@counts) {<BR>
chop ($line);<BR>
($page,$count)=split(/ /, $line);<BR>
$page=~s/'//g;<BR>
if ($page eq $doc)<BR>
{<BR>
$count++;<BR>
$found = 1;<BR>
$line = "'$page'
$count";<BR>
$found_count =
$count;<BR>
}<BR>
push (@newcount, $line);<BR>
}<BR>
if ($found == 1) {<BR>
$count = $found_count<BR>
} else {<BR>
$count = 1;<BR>
push (@newcount, "'$doc' 1");
<BR>
}<BR>
@newcount=sort(@newcount);<BR>
<BR>
# Updates the count file<BR>
open (CFILE, ">$count_file");<BR>
flock(CFILE, 2); # lock<BR>
for $line (@newcount) { print CFILE "$line\n"; }<BR>
flock(CFILE, 8); # unlock<BR>
close CFILE;<BR>
print "Content-Type: text/html\n\n";<BR>
print "$count";<BR>
<BR>
### End of counter.cgi ###</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Type in the script in Listing 7.1 and save it in your <TT><FONT FACE="Courier">cgi-bin</FONT></TT>
directory. Configure your server in order to provide SSI functionality
(or ask your Webmaster to do it for you) and insert the previously
shown SSI tag into your page. Then, use your favorite browser
and open the page. You should be presented with the result shown
in Figure 7.3.
<P>
<A HREF="f7-3.gif" ><B>Figure 7.3:</B> <I>The output of a visitor counter script.</I></A>
<P>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
If you don't get the results shown in Figure 7.3., make sure that</BLOCKQUOTE>
<UL>
<LI>Your httpd server supports SSI (ncSA and Apache do; CERN needs a special patch. Please refer to the previous section dedicated to the CERN server).
<LI>You have turned on the X bit of the HTML file in which you have inserted the SSI tag. Use the command <TT><FONT FACE="Courier">chmod 700 the_file</FONT></TT> to do it.
<LI>The script has been saved in the cgi-bin directory of your httpd server and has execute permissions (use the command <TT><FONT FACE="Courier">chmod 700 the_script</FONT></TT> to do it).
</UL>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
The corresponding HTML source is in Figure 7.4.
<P>
<A HREF="f7-4.gif" ><B>Figure 7.4:</B> <I>The HTML source with the SSI tag for the counter script.</I></A>
<H3><A NAME="RandomImageGenerator">Random Image Generator</A>
</H3>
<P>
The script <TT><FONT FACE="Courier">randimg.cgi</FONT></TT> presented
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -