?? ch06_02.htm
字號:
<p><tt class="literal">%e</tt></p></td><td><p>Date</p></td><td><p>1</p></td></tr><tr><td><p><tt class="literal">%H</tt></p></td><td><p>24-hour clock hour</p></td><td><p>13</p></td></tr><tr><td><p><tt class="literal">%I</tt></p></td><td><p>12-hour clock hour</p></td><td><p>01</p></td></tr><tr><td><p><tt class="literal">%j</tt></p></td><td><p>Decimal day of the year</p></td><td><p>360</p></td></tr><tr><td><p><tt class="literal">%m</tt></p></td><td><p>Month number</p></td><td><p>11</p></td></tr><tr><td><p><tt class="literal">%M</tt></p></td><td><p>Minutes</p></td><td><p>08</p></td></tr><tr><td><p><tt class="literal">%p</tt></p></td><td><p>AM | PM</p></td><td><p>AM</p></td></tr><tr><td><p><tt class="literal">%r</tt></p></td><td><p>Time as <tt class="literal">%I:%M:%S %p</tt></p></td><td><p>07:17:39 PM</p></td></tr><tr><td><p><tt class="literal">%S</tt></p></td><td><p>Seconds</p></td><td><p>09</p></td></tr><tr><td><p><tt class="literal">%T</tt></p></td><td><p>24-hour time as <tt class="literal">%H:%M:%S</tt></p></td><td><p>16:55:15</p></td></tr><tr><td><p><tt class="literal">%U</tt></p></td><td><p>Week of the year (also <tt class="literal">%W</tt>)</p></td><td><p>49</p></td></tr><tr><td><p><tt class="literal">%w</tt></p></td><td><p>Day of the week number</p></td><td><p>5</p></td></tr><tr><td><p><tt class="literal">%y</tt></p></td><td><p>Year of the century</p></td><td><p>95</p></td></tr><tr><td><p><tt class="literal">%Y</tt></p></td><td><p>Year</p></td><td><p>1995</p></td></tr><tr><td><p><tt class="literal">%Z</tt></p></td><td><p>Time zone</p></td><td><p>EST</p></td></tr></table></div><a name="ch06-24838" /><div class="sect2"><h3 class="sect2">6.2.5. Including Boilerplates</h3><p><a name="INDEX-1323" /><a name="INDEX-1324" /><a name="INDEX-1325" /> <a name="INDEX-1,326" /><a name="INDEX-1327" />Thereare times when you will have certain information that you repeat innumerous documents on the server such as a copyright notice, thewebmaster's email address, etc. Instead of maintaining thisinformation separately in each file, you can include one file thathas all of this information. It is much easier to update a singlefile if this information changes (for example, you may need to updatethe copyright notice the beginning of next year). <a href="ch06_02.htm#ch06-54923">Example 6-1</a> shows an example of such a file that itselfcontains SSI commands (note the <em class="emphasis">.shtml</em>extension).</p><a name="ch06-54923" /><div class="example"><h4 class="objtitle">Example 6-1. footer.shtml </h4><blockquote><pre class="code"><HR><P><FONT SIZE="-1">Copyright 1999-2000 by My Company, Inc.<BR>Please report any problems to <A HREF="mailto:<!--#echo var="SERVER_ADMIN"-->"> <!--#echo var="SERVER_ADMIN"--></A>.<BR>This document was last modified on <!--#echo var="LAST_MODIFIED"-->.<BR></FONT></P></pre></blockquote></div><p>It may look messy to include an SSI command within another HTML tag,but don't worry about this being invalid HTML because the webserver will parse it before it sends it to the client. Also, you maywonder if we were to include this file in another file which file theserver uses to determine the <a name="INDEX-1328" />LAST_MODIFIEDvariable. LAST_MODIFIED is set once by the server for the file thatthe client requested. If that file includes other files, such as<em class="filename">footer.shtml</em>, LAST_MODIFIED will still refer tothe original file; so this footer will do what we want.</p><p>Because included<a name="INDEX-1329" />files are not complete HTMLdocuments (they have no <HTML>, <HEAD>, or <BODY>tags), it can be easier to maintain these files if you differentiatethem by creating a standard extension for them or keeping them in aparticular directory. In our example we'll create a foldercalled <em class="emphasis">/includes</em> in the documentroot and place <em class="emphasis">footer.shtml</em> here. We can theninclude the file by adding the following line to other<em class="filename">.shtml</em> files:</p><blockquote><pre class="code"><!--#include virtual="/includes/footer.shtml" --></pre></blockquote><p>This SSI command will be replaced with a footer containing acopyright notice, the email address of the server administrator, andthe modification date of the file requested.</p><p>You can also use the<tt class="command">file</tt><a name="INDEX-1330" /> attribute insteadof <tt class="command">virtual</tt> to reference the file, but<tt class="command">file</tt> has limitations. You cannot use absolutepaths, the web server does no processing on the requested file (e.g.,for CGI scripts or other SSI commands), and you may not referencefiles outside the document root. This last restriction preventssomeone from including a file like <em class="filename">/etc/passwd</em>in an HTML document (since it's possible that someone is ableto upload files to a server without otherwise having access to thisfile). Given these restrictions, it's typically easier tosimply use <tt class="command">virtual</tt>.</p></div><a name="ch06-13635" /><div class="sect2"><h3 class="sect2">6.2.6. Executing CGI Programs</h3><p><a name="INDEX-1331" /> <a name="INDEX-1,332" /><a name="INDEX-1333" />You can use Server Side Includes toembed the results of an entire CGI program into a static HTMLdocument by using either <em class="emphasis">exec cgi</em> or<em class="emphasis">include virtual</em><a name="INDEX-1334" />. This is convenient for those timeswhen you want to display just one piece of dynamic data, such as:</p><blockquote><pre class="code">This page has been accessed 9387 times.</pre></blockquote><p>Let's look at an example of inserting output from CGI programs.<a name="INDEX-1335" /> <a name="INDEX-1,336" /> <a name="INDEX-1,337" /><a name="INDEX-1338" /><a name="INDEX-1339" />Suppose youhave a simple CGI program that keeps track of the number of visitors,called using the <em class="emphasis">include</em> SSI command in an HTMLdocument:</p><blockquote><pre class="code">This page has been accessed<!--#include virtual="/cgi/counter.cgi"--> times.</pre></blockquote><p>We can include this tag in any SSI-enabled HTML page on our webserver; each page will have its own count. We don't need topass any variables to tell the CGI which URL we need the count for;the DOCUMENT_URI environment variable will contain the URL of theoriginal document requested. Even though this is not a standard CGIenvironment variable, the additional SSI variables are provided toCGI scripts invoked via SSI.</p><p>The code behind an access counter is quite short. A<a name="INDEX-1340" />Berkeley DB hash file on the servercontains a count of the number of visitors that have accessed eachdocument we're tracking. Whenever a user visits the document,the SSI directive in that document calls a CGI program that reads thenumerical value stored in the data file, increments it, and outputsit. The counter is shown in <a href="ch06_02.htm#ch06-66032">Example 6-2</a>.</p><a name="ch06-66032" /><div class="example"><h4 class="objtitle">Example 6-2. counter.cgi </h4><blockquote><pre class="code">#!/usr/bin/perl -wTuse strict;use Fcntl;use DB_File;use constant COUNT_FILE => "/usr/local/apache/data/counter/count.dbm";my %count;my $url = $ENV{DOCUMENT_URI};local *DBM;print "Content-type: text/plain\n\n";if ( my $db = tie %count, "DB_File", COUNT_FILE, O_RDWR | O_CREAT ) { my $fd = $db->fd; open DBM, "+<&=$fd" or die "Could not dup DBM for lock: $!"; flock DBM, LOCK_EX; undef $db; $count{$url} = 0 unless exists $count{$url}; my $num_hits = ++$count{$url}; untie %count; close DBM; print "$num_hits\n";} else { print "[Error processing counter data]\n";}</pre></blockquote></div><p>Don't worry about how we access the hash file; we'lldiscuss this in <a href="ch10_01.htm">Chapter 10, "Data Persistence"</a>. Note that we output themedia type. You must do this for included files even though theheader is not returned to the client. An important thing to note isthat a CGI program called by an SSI directive cannot output anythingother than text because this data is embedded within the documentthat invoked the directive. As a result, it doesn't matterwhether you output a content type of <tt class="command">text/plain</tt> or<tt class="command">text/html</tt>, as the browser will interpret the datawith the media type of the calling document. Needless to say, yourCGI program cannot output graphic images or other binary data.</p></div><a name="ch06-20332" /><div class="sect2"><h3 class="sect2">6.2.7. Common Errors</h3><p><a name="INDEX-1341" /><a name="INDEX-1342" /> <a name="INDEX-1,343" /><a name="INDEX-1344" />There are a few common errors thatyou can make when using server side includes. First, you should notforget the <tt class="literal">#</tt>:</p><blockquote><pre class="code"><!--echo var="REMOTE_USER" --></pre></blockquote><p>Second, do not add extra spaces between the preceding<tt class="literal"><!--</tt> and <tt class="literal">#</tt>:</p><blockquote><pre class="code"><!-- #echo var="REMOTE_USER" --></pre></blockquote><p>Finally, if you do not enclose the value of the final attribute in<a name="INDEX-1345" /><a name="INDEX-1346" />quotes, you may need to insert an additionalspace before the trailing <tt class="literal">--></tt>. Otherwise, theSSI parser may interpret those characters as part of theattribute's value:</p><blockquote><pre class="code"><!--#echo var=REMOTE_USER--></pre></blockquote><p>Generally it is simpler and clearer to use quotes.</p><p>If you make either of the first two mistakes, the server will notrecognize the SSI command and will pass it on as an HTML comment. Inthe last case, the command will probably be replaced with <a name="INDEX-1347" /> <a name="INDEX-1,348" /> <a name="INDEX-1,349" />an<a name="INDEX-1350" />error message.</p></div><hr align="left" width="515" /><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch06_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td width="171" valign="top" align="center"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td width="172" valign="top" align="right"><a href="ch06_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td width="172" valign="top" align="left">6. HTML Templates</td><td width="171" valign="top" align="center"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td width="172" valign="top" align="right">6.3. HTML::Template</td></tr></table></div><hr align="left" width="515" /><img src="../gifs/navbar.gif" alt="Library Navigation Links" usemap="#library-map" border="0" /><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area href="../index.htm" coords="1,1,83,102" shape="rect" /><area href="../lnut/index.htm" coords="81,0,152,95" shape="rect" /><area href="../run/index.htm" coords="172,2,252,105" shape="rect" /><area href="../apache/index.htm" coords="238,2,334,95" shape="rect" /><area href="../sql/index.htm" coords="336,0,412,104" shape="rect" /><area href="../dbi/index.htm" coords="415,0,507,101" shape="rect" /><area href="../cgi/index.htm" coords="511,0,601,99" shape="rect" /></map></body></html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -