📄 ch23.htm
字号:
<P>
Lines 9 through 16 declare a package for parsing the incoming
fields in the <TT><FONT FACE="Courier">QUERY_STRING</FONT></TT>.
The assignments are of the form <TT><I><FONT FACE="Courier">variable
= name</FONT></I></TT> in a query string. The local package <TT><FONT FACE="Courier">CREDIT</FONT></TT>
declares those Perl variables that are required by this script
as well as what strings to use to extract those values.
<P>
Line 17 prints the response header for the HTML request back to
the client. Line 18 takes the input from the client (in <TT><FONT FACE="Courier">QUERY_STRING</FONT></TT>)
and parses it into the members in the <TT><FONT FACE="Courier">CREDIT</FONT></TT>
package. Lines 23 through 69 process the error-handling for the
CGI script as before. Lines 70 through 79 echo the members of
the <TT><FONT FACE="Courier">CREDIT</FONT></TT> package. Line
81 terminates the output from the CGI script. Terminating the
CGI script destroys the <TT><FONT FACE="Courier">CGI::Base</FONT></TT>
and <TT><FONT FACE="Courier">CGI::Request</FONT></TT> objects
automatically. Line 80 is used for debugging and echoes all the
environment variables set by the <TT><FONT FACE="Courier">CGI::Base</FONT></TT>
object. This output is very similar to Listing 20.9 in <A HREF="ch20.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch20.htm" >Chapter 20</A>,
"Introduction to Web Pages and CGI." In fact, given
that Listing 20.9 is about 45 lines long, you can write a similar
application that is much shorter using CGI class, as shown in
Listing 23.2.
<HR>
<BLOCKQUOTE>
<B>Listing 23.2. Echoing CGI environment variables.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier">1 #!/usr/bin/perl<BR>
2 use CGI::Base;<BR>
3 use CGI::Request;<BR>
4 <BR>
5 print PrintHeader();<BR>
6 print FmtRequest();</FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
It's complicated enough to write CGI scripts. It's worse to write
lengthy Perl scripts to generate HTML for you. Using the CGI classes
certainly takes some (not all) of the drudgery away.
<P>
In the next section, you'll use some more features of HTML to
show data.
<H2><A NAME="UsingTablesinHTML"><B><FONT SIZE=5 COLOR=#FF0000>Using
Tables in HTML</FONT></B></A></H2>
<P>
The HTML 3.2 specification allows for displaying data in a clean
tabular form using HTML widgets. The possibilities of showing
data in a nice tabular format are tremendous.
<P>
Tables in HTML pages are in the following form:
<BLOCKQUOTE>
<TT><FONT FACE="Courier"><TABLE BORDER><BR>
<TH> Header Column 1 </TH><BR>
<TH> Header Column 2 </TH><BR>
<TH> Header Column 3 </TH><BR>
...<BR>
<TR><BR>
<TD> Row 1 Column 1 </TD><BR>
<TD> Row 1 Column 2 </TD><BR>
<TD> Row 1 Column 3 </TD><BR>
<TD> Row 1 Column 4 </TD><BR>
...<BR>
<BR>
<TR><BR>
<TD> Row 2 Column 1 </TD><BR>
<TD> Row 2 Column 2 </TD><BR>
<TD> Row 2 Column 3 </TD><BR>
<TR><BR>
...<BR>
</TABLE></FONT></TT>
</BLOCKQUOTE>
<P>
The <TT><FONT FACE="Courier"><TABLE></FONT></TT> and <TT><FONT FACE="Courier"></TABLE></FONT></TT>
tags delimit the table. The <TT><FONT FACE="Courier">BORDER</FONT></TT>
attribute instructs the browser to put lines around the cell.
If you do not want borders around the cells, omit the <TT><FONT FACE="Courier">BORDER</FONT></TT>
attribute. The table will be as wide as the width of all the columns.
Browsers adjust the width of columns to accommodate all the text
as best they can. The <TT><FONT FACE="Courier">ALIGN</FONT></TT>
attribute can take one of three values to align the text in a
table cell: <TT><FONT FACE="Courier">left</FONT></TT>, <TT><FONT FACE="Courier">right</FONT></TT>,
or <TT><FONT FACE="Courier">center</FONT></TT> (the default).
<P>
The data in between the table data tags, <TT><FONT FACE="Courier"><TD></FONT></TT>
and <TT><FONT FACE="Courier"></TD></FONT></TT>, is for the
cell at a current row number. Rows start at every row tag, <TT><FONT FACE="Courier"><TR></FONT></TT>.
The table header tags, <TT><FONT FACE="Courier"><TH></FONT></TT>
and <TT><FONT FACE="Courier"></TH></FONT></TT>, specify
the titles in the columns in the first row. Table data input is
finished with the <TT><FONT FACE="Courier"></TABLE></FONT></TT>
tag.
<P>
If not enough headers are specified, the headers for the table
will be empty. Note that the first row has four columns, but only
three headers. Therefore, the fourth column will not have a heading.
<P>
The Table widget provides other nifty features such as column
spanning, where the <TT><FONT FACE="Courier">colspan</FONT></TT>
attribute determines the number of columns a heading or item will
span. The <TT><FONT FACE="Courier">rowspan</FONT></TT> attribute
specifies the number of rows an item will span. Some sample code
is shown in Listing 23.3.
<HR>
<BLOCKQUOTE>
<B>Listing 23.3. Using row and column spanning.<BR>
</B>
</BLOCKQUOTE>
<BLOCKQUOTE>
<TT><FONT FACE="Courier"> 1 <html><head><BR>
2 </head><BR>
3 <BR>
4 <body><BR>
5 <center><h1>Show Tables</h1></center>
<BR>
6 <p><BR>
7 <hr><BR>
8 <B> Column Span </B><BR>
9 <hr><BR>
10 <TABLE BORDER><BR>
11 <TH> Column 1 </TH><BR>
12 <TH> Column 2 </TH><BR>
13 <TH colspan=2> Column 3 and 4 </TH><BR>
14 <BR>
15 <TR><BR>
16 <TD> Row 1 Column 1 </TD><BR>
17 <TD> Row 1 Column 2 </TD><BR>
18 <TD> Row 1 Column 3 </TD><BR>
19 <TD> Row 1 Column 4 </TD><BR>
20 <BR>
21 </TABLE><BR>
22 <P><BR>
23 <HR><BR>
24 <B> Row Span </B><BR>
25 <hr><BR>
26 <TABLE BORDER><BR>
27 <TH rowspan=3> Column 1 </TH><BR>
28 <TH> Column 2 </TH><BR>
29 <TH> Column 3 </TH><BR>
30 <TR><BR>
31 <TD> Row 1 Column 1 </TD><BR>
32 <TD> Row 1 Column 2 </TD><BR>
33 <TR><BR>
34 <TD> Row 2 Column 1 </TD><BR>
35 <TD> Row 2 Column 2 </TD><BR>
36 <TR><BR>
37 <TD> Row 3 Column 1 </TD><BR>
38 <TD> Row 3 Column 2 </TD><BR>
39 <TD> Row 3 Column 3 </TD><BR>
40 <TR><BR>
41 <TD> Row 4 Column 1 </TD><BR>
42 <TD> Row 4 Column 2 </TD><BR>
43 <TD> Row 4 Column 3 </TD><BR>
44 </TABLE><BR>
45 4647 <P><BR>
46 <HR><BR>
47 <B> Row and Column Span </B><BR>
48 <hr>49 <TABLE BORDER><BR>
50 <TH rowspan=2> Column 1 </TH><BR>
51 <TH colspan=2> Column 2 </TH><BR>
52 <TR><BR>
53 <TD> Row 1 Column 1 </TD><BR>
54 <TD> Row 1 Column 2 </TD><BR>
55 <TR><BR>
56 <TD> Row 2 Column 1 </TD><BR>
57 <TD> Row 2 Column 2 </TD><BR>
58 <TR><BR>
59 <TD> Row 3 Column 1 </TD><BR>
60 <TD> Row 3 Column 2 </TD><BR>
61 <TD> Row 3 Column 3 </TD><BR>
62 <TR><BR>
63 <TD> Row 4 Column 1 </TD><BR>
64 <TD> Row 4 Column 2 </TD><BR>
65 <TD> Row 4 Column 3 </TD><BR>
66 </TABLE><BR>
67 <BR>
68 </body></html></FONT></TT>
</BLOCKQUOTE>
<HR>
<P>
Now that you know how to put data in a table for an HTML page,
let's see what we can display using these tables. For this example,
you will display the statistics of which pages on the server get
hit the most. This way you can gauge what the most popular items
are on the Web site.
<P>
The statistics for the number of hits per file, including the
date and IP number of the requesting server, are kept in a file
called <TT><FONT FACE="Courier">access_log</FONT></TT> in your
Web server's <TT><FONT FACE="Courier">logs</FONT></TT> directory.
This is the file to look at if you want to know which file has
been hit the most. The location of this file is set when your
Web server is configured. You can find this file with the <TT><FONT FACE="Courier">find</FONT></TT>
command (<TT><FONT FACE="Courier">find / -name access_log -print</FONT></TT>)
if you are not sure where to look.
<P>
Rather than write a whole statistics utility from scratch, you
can use existing tools to get the information from <TT><FONT FACE="Courier">access_log</FONT></TT>.
In this example, you use <TT><FONT FACE="Courier">getstats</FONT></TT>.
The <TT><FONT FACE="Courier">getstats</FONT></TT> program was
written by Kevin Hughes at Enterprise Integration Technologies
(<TT><FONT FACE="Courier">www.eit.com</FONT></TT>). Get it via
FTP from <TT><FONT FACE="Courier">ftp.eit.com/web.software/getstats</FONT></TT>
or from <TT><A HREF="http://www.eit.com/goodies/software/getstats/src/statform.html" tppabs="http://www.eit.com/goodies/software/getstats/src/statform.html">http://www.eit.com/goodies/software/getstats/src/statform.html</A></TT>.
(It's available in source form only, and you have to use GNU's
<TT><FONT FACE="Courier">gcc</FONT></TT> to compile it. The source
might be called <TT><FONT FACE="Courier">getstats.<I>XX</I>.c</FONT></TT>,
where <TT><I><FONT FACE="Courier">XX</FONT></I></TT> is the version
number. The version I work with in this section is 12.)
<P>
One word of caution before you build the file: determine the type
of server you are running and the type of format your <TT><FONT FACE="Courier">access_log</FONT></TT>
is in. The most common server is the ncSA server. Chances are
that your <TT><FONT FACE="Courier">access_log</FONT></TT> looks
like this:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">crow.lib.uh.edu - - [04/Mar/1995:16:28:39
-0600] "GET /iistv.html<BR>
HTTP/1.0"
200 850</FONT></TT>
</BLOCKQUOTE>
<P>
The first item is the name of the calling browser, followed by
a hyphen or an IP number, and then either a user name or a hyphen.
Within the square brackets is the time of access, followed by
the method of access and the file accessed. The HTTP server version
number is listed next. Then the server result code is shown, followed
by the number of bytes sent back. This is known as the <TT><FONT FACE="Courier">COMMON</FONT></TT>
format.
<P>
If your log file is different from this one, determine the type
of server you have and get the correct tool for it. In almost
all cases, <TT><FONT FACE="Courier">getstats</FONT></TT> will
work for you, so try it anyway. You may be pleasantly surprised.
<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD ><B>Tip</B></TD></TR>
<TR VALIGN=TOP><TD >
<BLOCKQUOTE>
A search on the words <I>Perl</I>, <I>statistics</I>, and <I>Web</I> in the Netscape browser will produce some very interesting links to programs similar to <TT><FONT FACE="Courier">getstats</FONT></TT>. A good statistics program with a graphical output is
<TT><FONT FACE="Courier">gwstats</FONT></TT>.
</BLOCKQUOTE>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
Let's now work with the <TT><FONT FACE="Courier">getstats</FONT></TT>
program to see how to use it. Before you can use it, you have
to set some values for the <TT><FONT FACE="Courier">getstats</FONT></TT>
program sources and then recompile it.
<P>
First of all, in the <TT><FONT FACE="Courier">getstats.12.c</FONT></TT>
source file you have to set <TT><FONT FACE="Courier">#define COMMON</FONT></TT>
to <TT><FONT FACE="Courier">1</FONT></TT>, not the value <TT><FONT FACE="Courier">0</FONT></TT>,
which is the way it's delivered. Also, be sure to define the location
of the files in your <TT><FONT FACE="Courier">WWW</FONT></TT>
directory, especially the name of your server and the location
of the <TT><FONT FACE="Courier">access_log</FONT></TT> file. Creating
the program is as easy as typing the following command:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">gcc getstats.12.c -o getstats.</FONT></TT>
</BLOCKQUOTE>
<P>
Ignore any warnings you get with the <TT><FONT FACE="Courier">gcc</FONT></TT>
compiler. The warnings, if any, are harmless and are about comparing
an integer with a pointer.
<P>
To get all the statistics, you just type <TT><FONT FACE="Courier">getstats</FONT></TT>
at the prompt. If you do not get any output or the program appears
to hang, make sure you have defined the <TT><FONT FACE="Courier">COMMON</FONT></TT>
value to <TT><FONT FACE="Courier">1</FONT></TT> and then recompile.
<P>
The output from the <TT><FONT FACE="Courier">getstats</FONT></TT>
program is long and verbose, depending on what files you have
on your system. I am particularly interested in the following
section of output:
<BLOCKQUOTE>
<TT><FONT FACE="Courier">HTTP Server Request Statistics<BR>
Covers: 07/18/95 to 03/09/96 (236 days).<BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -