⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch23.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<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>,

&quot;Introduction to Web Pages and CGI.&quot; 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">&lt;TABLE BORDER&gt;<BR>

&lt;TH&gt; Header&nbsp;&nbsp;Column 1 &lt;/TH&gt;<BR>

&lt;TH&gt; Header&nbsp;&nbsp;Column 2 &lt;/TH&gt;<BR>

&lt;TH&gt; Header&nbsp;&nbsp;Column 3 &lt;/TH&gt;<BR>

...<BR>

&lt;TR&gt;<BR>

&lt;TD&gt; Row 1 Column 1 &lt;/TD&gt;<BR>

&lt;TD&gt; Row 1 Column 2 &lt;/TD&gt;<BR>

&lt;TD&gt; Row 1 Column 3 &lt;/TD&gt;<BR>

&lt;TD&gt; Row 1 Column 4 &lt;/TD&gt;<BR>

...<BR>

<BR>

&lt;TR&gt;<BR>

&lt;TD&gt; Row 2 Column 1 &lt;/TD&gt;<BR>

&lt;TD&gt; Row 2 Column 2 &lt;/TD&gt;<BR>

&lt;TD&gt; Row 2 Column 3 &lt;/TD&gt;<BR>

&lt;TR&gt;<BR>

&nbsp;&nbsp;...<BR>

&lt;/TABLE&gt;</FONT></TT>

</BLOCKQUOTE>

<P>

The <TT><FONT FACE="Courier">&lt;TABLE&gt;</FONT></TT> and <TT><FONT FACE="Courier">&lt;/TABLE&gt;</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">&lt;TD&gt;</FONT></TT>

and <TT><FONT FACE="Courier">&lt;/TD&gt;</FONT></TT>, is for the

cell at a current row number. Rows start at every row tag, <TT><FONT FACE="Courier">&lt;TR&gt;</FONT></TT>.

The table header tags, <TT><FONT FACE="Courier">&lt;TH&gt;</FONT></TT>

and <TT><FONT FACE="Courier">&lt;/TH&gt;</FONT></TT>, specify

the titles in the columns in the first row. Table data input is

finished with the <TT><FONT FACE="Courier">&lt;/TABLE&gt;</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">&nbsp;1 &lt;html&gt;&lt;head&gt;<BR>

&nbsp;2 &lt;/head&gt;<BR>

&nbsp;3 <BR>

&nbsp;4 &lt;body&gt;<BR>

&nbsp;5 &lt;center&gt;&lt;h1&gt;Show Tables&lt;/h1&gt;&lt;/center&gt;

<BR>

&nbsp;6 &lt;p&gt;<BR>

&nbsp;7 &lt;hr&gt;<BR>

&nbsp;8 &lt;B&gt; Column Span &lt;/B&gt;<BR>

&nbsp;9 &lt;hr&gt;<BR>

10 &lt;TABLE BORDER&gt;<BR>

11 &lt;TH&gt; Column 1 &lt;/TH&gt;<BR>

12 &lt;TH&gt; Column 2 &lt;/TH&gt;<BR>

13 &lt;TH colspan=2&gt; Column 3 and 4 &lt;/TH&gt;<BR>

14 <BR>

15 &lt;TR&gt;<BR>

16 &lt;TD&gt; Row 1 Column 1 &lt;/TD&gt;<BR>

17 &lt;TD&gt; Row 1 Column 2 &lt;/TD&gt;<BR>

18 &lt;TD&gt; Row 1 Column 3 &lt;/TD&gt;<BR>

19 &lt;TD&gt; Row 1 Column 4 &lt;/TD&gt;<BR>

20 <BR>

21 &lt;/TABLE&gt;<BR>

22 &lt;P&gt;<BR>

23 &lt;HR&gt;<BR>

24 &lt;B&gt; Row Span &lt;/B&gt;<BR>

25 &lt;hr&gt;<BR>

26 &lt;TABLE BORDER&gt;<BR>

27 &lt;TH rowspan=3&gt; Column 1 &lt;/TH&gt;<BR>

28 &lt;TH&gt; Column 2 &lt;/TH&gt;<BR>

29 &lt;TH&gt; Column 3 &lt;/TH&gt;<BR>

30 &lt;TR&gt;<BR>

31 &lt;TD&gt; Row 1 Column 1 &lt;/TD&gt;<BR>

32 &lt;TD&gt; Row 1 Column 2 &lt;/TD&gt;<BR>

33 &lt;TR&gt;<BR>

34 &lt;TD&gt; Row 2 Column 1 &lt;/TD&gt;<BR>

35 &lt;TD&gt; Row 2 Column 2 &lt;/TD&gt;<BR>

36 &lt;TR&gt;<BR>

37 &lt;TD&gt; Row 3 Column 1 &lt;/TD&gt;<BR>

38 &lt;TD&gt; Row 3 Column 2 &lt;/TD&gt;<BR>

39 &lt;TD&gt; Row 3 Column 3 &lt;/TD&gt;<BR>

40 &lt;TR&gt;<BR>

41 &lt;TD&gt; Row 4 Column 1 &lt;/TD&gt;<BR>

42 &lt;TD&gt; Row 4 Column 2 &lt;/TD&gt;<BR>

43 &lt;TD&gt; Row 4 Column 3 &lt;/TD&gt;<BR>

44 &lt;/TABLE&gt;<BR>

45&nbsp;&nbsp;4647 &lt;P&gt;<BR>

46 &lt;HR&gt;<BR>

47 &lt;B&gt; Row&nbsp;&nbsp;and Column Span &lt;/B&gt;<BR>

48 &lt;hr&gt;49 &lt;TABLE BORDER&gt;<BR>

50 &lt;TH rowspan=2&gt; Column 1 &lt;/TH&gt;<BR>

51 &lt;TH colspan=2&gt; Column 2&nbsp;&nbsp;&lt;/TH&gt;<BR>

52 &lt;TR&gt;<BR>

53 &lt;TD&gt; Row 1 Column 1 &lt;/TD&gt;<BR>

54 &lt;TD&gt; Row 1 Column 2 &lt;/TD&gt;<BR>

55 &lt;TR&gt;<BR>

56 &lt;TD&gt; Row 2 Column 1 &lt;/TD&gt;<BR>

57 &lt;TD&gt; Row 2 Column 2 &lt;/TD&gt;<BR>

58 &lt;TR&gt;<BR>

59 &lt;TD&gt; Row 3 Column 1 &lt;/TD&gt;<BR>

60 &lt;TD&gt; Row 3 Column 2 &lt;/TD&gt;<BR>

61 &lt;TD&gt; Row 3 Column 3 &lt;/TD&gt;<BR>

62 &lt;TR&gt;<BR>

63 &lt;TD&gt; Row 4 Column 1 &lt;/TD&gt;<BR>

64 &lt;TD&gt; Row 4 Column 2 &lt;/TD&gt;<BR>

65 &lt;TD&gt; Row 4 Column 3 &lt;/TD&gt;<BR>

66 &lt;/TABLE&gt;<BR>

67 <BR>

68 &lt;/body&gt;&lt;/html&gt;</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] &quot;GET /iistv.html<BR>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;HTTP/1.0&quot;

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 + -