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

📄 ch21.htm

📁 prrl 5 programs codes in the book
💻 HTM
📖 第 1 页 / 共 5 页
字号:

happens. The second directory is the directory that the fuNCtion

is currently looking at. It will change with each recursion.

<H3><A NAME="ExampleGettingUserFeedback">

Example: Getting User Feedback</A></H3>

<P>

One of the hallmarks of a professional Web site, at least in my

opinion, is that every page has a section that identifies the

organization that created the page and a way to provide feedback.

Most Web sites simply place a little hypertext link that contains

the Webmaster's e-mail address. However, this places a large burden

on the user to adequately describe the Web page so that the Webmaster

knows which one they are referring to. Wouldn't it be nice if

you could automate this? Picture this scenario: the user clicks

a button and a user feedback form appears that automatically knows

which page the user was on when the button was pressed. Perhaps

the feedback form looks like Figure 21.3.

<P>

<A HREF="f21-3.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/f21-3.gif"><B>Figure 21.3 : </B><I>A sample user feedback form</I>.</A>

<P>

You can have this nice feature at your site with a little work

by following these steps:

<OL>

<LI>INClude a small HTML form at the end of every Web page at

your site. This footer contains the button that summons the feedback

form.

<LI>Create a CGI Perl script that generates a feedback form on-the-fly.

This form will be customized to each Web page.

</OL>

<P>

In step one, you need to add a small HTML form to each Web page

at your site. This form does not have to be very complex; just

one button will do. You can get started by adding the following

form to the bottom of your home page just before the <TT>&lt;/BODY&gt;</TT>

tag.

<BLOCKQUOTE>

<PRE>&lt;FORM METHOD=POST Action=&quot;cgi-bin/feedback.pl&quot;&gt;

  &lt;INPUT TYPE=hidden NAME=&quot;to&quot; VALUE=&quot;xxxxxxxxxxxxxxxxxx&quot;&gt;

  &lt;INPUT TYPE=hidden NAME=&quot;subject&quot; VALUE=&quot;Home Page&quot;&gt;

  &lt;CENTER&gt;

    &lt;INPUT TYPE=submit VALUE=&quot;Send a comment to the webmaster&quot;&gt;

  &lt;/CENTER&gt;

&lt;/FORM&gt;<BR>

</PRE>

</BLOCKQUOTE>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD><B>Note</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

You might need to change directory locations in the action clause to correspond to the requirements of your own server.</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<P>

The first field, <TT>to</TT>, is the

destination of the feedback information. Change the xs to your

personal e-mail address. The second field, <TT>subject</TT>,

is used to describe the Web page that the HTML form is contained

on. This is the only field that will change from Web page to Web

page. The last item in the form is a submit button. When this

button is clicked, the feedback.pl Perl script will be invoked.

<P>

This HTML form will place a submit button onto your home page

like the one shown in Figure 21.4.

<P>

<A HREF="f21-4.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/f21-4.gif"><B>Figure 21.4 : </B><I>The customized submit button</I>.</A><BR>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD><B>Note</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

In the course of researching the best way to create a customized feedback form, I  pulled information from a CGI script (<TT>mailer.cgi</TT>) by Matt Kruse (mkruse@saunix.sau.edu) and <FONT FACE="LI Helvetica Light Oblique">Serving the Web</FONT>, a book 
by Robert Jon Mudry.

</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<P>

Step Two requires you to create the feedback Perl script. Listing

21.8 contains a bare-bones script that will help you get started.

This script will generate the HTML that created the Web page in

Figure 21.3.

<P>

<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>

<BLOCKQUOTE>

<I>Turn on the warning option.<BR>

Turn on the strict pragma.<BR>

Declare a hash variable to hold the form's data.<BR>

Call the </I><TT><I>getFormData()</I></TT><I>

fuNCtion.<BR>

Output the web page's MIME type.<BR>

Output the start of the web page.<BR>

Output the feedback form.<BR>

Output the end of the web page.<BR>

Define the </I><TT><I>getFormData()</I></TT><I>

fuNCtion.<BR>

Declare a local variable to hold hash refereNCe in parameter array.

<BR>

Declare and initialize buffer to hold the unprocessed form data.

<BR>

Declare some temporary variables.<BR>

Read all of the form data into the </I><TT><I>$in</I></TT><I>

variable.<BR>

Iterate over the elements that result from splitting the input

buffer using &amp; as the delimiter.<BR>

Convert plus signs into spaces.<BR>

Split each item using the = as a delimiter.<BR>

Store the form data into the hash parameter.</I>

</BLOCKQUOTE>

<HR>

<P>

<B>Listing 21.8&nbsp;&nbsp;21LST08.PL-How to Generate an On-the-Fly

Feedback Form<BR>

</B>

<BLOCKQUOTE>

<PRE>#!/usr/bin/perl -w

use strict;



my(%formData);



getFormData(\%formData);



print &quot;Content-type: text/html\n\n&quot;;

print(&quot;&lt;HTML&gt;&quot;);

print(&quot;&lt;HEAD&gt;&lt;TITLE&gt;Web Page Comment Form&lt;/TITLE&gt;&lt;/HEAD&gt;\n&quot;);

print(&quot;&lt;BODY&gt;\n&quot;);

print(&quot;&lt;H1 ALIGN=CENTER&gt;Web Page Comment Form&lt;/H1&gt;\n&quot;);



print(&quot;&lt;FORM METHOD=\&quot;POST\&quot; Action=\&quot;mailto:$formData{'to'}\&quot;&gt;\n&quot;);

print(&quot;&lt;TABLE CELLPADDING=3&gt;&quot;);

print(&quot;&lt;TR&gt;&lt;TD&gt;To:&lt;/TD&gt;&lt;TD&gt;$formData{'to'}&lt;TD&gt;&lt;/TR&gt;\n&quot;);

print(&quot;&lt;TR&gt;&lt;TD&gt;Subject:&lt;/TD&gt;&lt;TD&gt;$formData{'subject'}&lt;/TD&gt;&lt;/TR&gt;\n&quot;);

print(&quot;&lt;TR&gt;&quot;);

print(&quot;&lt;TD&gt;&lt;B&gt;Your email address:&lt;/B&gt;&lt;/TD&gt;&quot;);

print(&quot;&lt;TD&gt;&lt;INPUT TYPE=\&quot;text\&quot; NAME=\&quot;addr\&quot; SIZE=40 MAXLENGTH=80&gt;&lt;/TD&gt;&quot;);

print(&quot;&lt;/TR&gt;\n&quot;);

print(&quot;&lt;TR&gt;&lt;TD VALIGN=top&gt;&lt;B&gt;How urgently do you need a reply:&lt;/B&gt;&lt;/TD&gt;\n&quot;);

print(&quot;&lt;TD&gt;&lt;INPUT TYPE=\&quot;radio\&quot; NAME=\&quot;urgeNCy\&quot; VALUE=\&quot;fyi\&quot; CHECKED&gt;

Just FYI\n&quot;);

print(&quot;&lt;INPUT TYPE=\&quot;radio\&quot; NAME=\&quot;urgeNCy\&quot; VALUE=\&quot;plr\&quot;&gt; Please

Reply\n&quot;);

print(&quot;&lt;INPUT TYPE=\&quot;radio\&quot; NAME=\&quot;urgeNCy\&quot; VALUE=\&quot;rur\&quot;&gt; Reply Urgently&lt;/TD&gt;&lt;TR&gt;\n&quot;);

print(&quot;&lt;TR&gt;&lt;TD VALIGN=top&gt;&lt;B&gt;What is the nature of your feedback:&lt;/B&gt;&lt;/TD&gt;\n&quot;);

print(&quot;&lt;TD&gt;&lt;SELECT NAME=\&quot;nature\&quot; SIZE=3 MULTIPLE&gt;\n&quot;);

print(&quot;&lt;OPTION SELECTED&gt;General Comments\n&quot;);

print(&quot;&lt;OPTION&gt; Found Typo\n&quot;);

print(&quot;&lt;OPTION&gt; Bug Report\n&quot;);

print(&quot;&lt;/SELECT&gt;&lt;/TD&gt;&lt;/TR&gt;\n&quot;);

print(&quot;&lt;TR&gt;&lt;TD VALIGN=top&gt;&lt;B&gt;Please enter your comments:&lt;/B&gt;&lt;/TD&gt;\n&quot;);

print(&quot;&lt;TD&gt;&lt;TEXTAREA NAME=\&quot;comment\&quot; COLS=50 ROWS=5&gt;&lt;/TEXTAREA&gt;&lt;/TD&gt;&lt;/TR&gt;\n&quot;);

print(&quot;&lt;/TABLE&gt;&lt;P&gt;&quot;);

print(&quot;&lt;CENTER&gt;&lt;INPUT TYPE=\&quot;submit\&quot; VALUE=\&quot;Mail Your Comments\&quot;&gt;&lt;/CENTER&gt;\n&quot;);

print(&quot;&lt;/FORM&gt;&quot;);

print(&quot;&lt;/BODY&gt;&quot;);

print(&quot;&lt;/HTML&gt;&quot;);



sub getFormData {

    my($hashRef) = shift;	# ref to hash to hold form data.

    my($in) = &quot;&quot;;             # buffer for unprocessed form data.

    my($key, $value);         # temporary variables.





read(STDIN, $in, $ENV{'CONTENT_LENGTH'});



    foreach (split(/&amp;/, $in)) {

        s/\+/ /g;

        ($key, $value) = split(/=/, $_);

        %{$hashRef}-&gt;{$key} = $value;

    }

}

</PRE>

</BLOCKQUOTE>

</BLOCKQUOTE>

<HR>

<P>

This form will send all of the information from the feedback form

to your e-mail address. ONCe there you need to perform further

processing in order to make use of the information. You might

want to have the feedback submit button call a second CGI script

that stores the feedback information into a database. The database

will make it much easier for you to track the comments and see

which Web pages generate the most feedback.

<P>

The <TT>getFormData()</TT> fuNCtion

does not do a very good job of processing the form data. Chapter

20, &quot;Form Processing&quot; describes more robust methods

of processing the data. This fuNCtion was kept simple to conserve

space.

<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>

Summary</FONT></A></H2>

<P>

Perl is an excellent tool to use when maintaining a Web site.

There are many tasks that can be automated such as analysis of

server logs and automatically generating HTML pages.

<P>

Server log files are created and maintained by Web servers for

a variety of reasons. They are created to monitor such things

as HTTP requests, CGI activity, and errors. Most Web servers use

a common log file format so programs written to support one server

will usually work on another.

<P>

Each log file entry in the access log holds information about

a single HTTP request. There is information such s the remote

site name, the time and date of the request, what documents was

requested, and the server's response to the request.

<P>

After reading about the log file format, you saw an example that

showed how to read a log file. The sample program evolved from

simply opening the log file and reading whole lines to opening

the log file and using a regular expression to parse the log file

entries. Using regular expressions lets you modify your code quickly

if you move to another server that has a nonstandard log file

format.

<P>

The next sample program showed how to count the number of times

each document has been accessed. This program uses the reporting

features of Perl to print a formatted report showing the document

and the number of accesses. A hash was used to store the document

names and the number of accesses.

<P>

The status code field in the log file entries is useful. Especially,

when you need to find out if unauthorized users have been attempting

to access secured documents. Status codes are three digits numbers.

Codes in the 400-499 range indicate problems on the client side.

These are the numbers to watch if you think someone is trying

to attack your site. Table 21.1 lists the most common status codes.

<P>

The next topic covered is converting a program that uses a report

into a program that generates Web pages. Instead of using format

statements, HTML tables were used to format the information.

<P>

There is no need for you to create Perl scripts to do all of the

analyzing. Some programmers have already done this type of work

and many of them have made their programs available on the Web

for little or no cost. You can find a complete list of these analysis

programs at:

<BLOCKQUOTE>

<PRE>http://www.yahoo.com/Computers_and_Internet/Internet/

    World_Wide_Web/HTTP/Servers/Log_Analysis_Tools/

</PRE>

</BLOCKQUOTE>

<P>

At times creating your own log file is good to do. You might want

to track the types of Web browsers visiting your site. Or you

might want to track the remote site addresses. Listing 21.6 showed

how to create your own log file.

<P>

The next major topic was communicating with your users. Of course,

communication is done through a variety of Web pages. One very

popular feature is a What's New page. This page is typically changed

every week and lets the user see what has changed in the past

week. Listing 21.7 showed a sample program that generates the

HTML for a What's New page. The program uses a data file to remember

the last time that it was run.

<P>

Another popular feature is the user feedback form. With a little

forethought, you can have the feedback automatically generated

by a CGI script. Listing 21.8 shows how to generate a form when

the user clicks a feedback button. This simple program can be

expanded as needed to generate different forms based on which

Web page the user clicked feedback on. You need to create a second

CGI script to process the results of the feedback form.

<P>

The next chapter, &quot;Internet Resources,&quot; will direct

you to some resources that are available on the Internet. The

chapter covers Usenet Newsgroups, Web sites, and the IRC.

<H2>Review Questions</H2>

<P>

Answers to Review Questions are in Appendix A.

<OL>

<LI>What is the access log used for?

<LI>Does the <TT>fullName</TT> field

in the log file correspond to the user's mail address?

<LI>Why is the status code of the log file entry important?

<LI>Can you find log file analysis programs on the Internet?

<LI>What good is a customized log file?

<LI>What are two popular features of Web sites?

<LI>What does recursion mean?

</OL>

<H2><A NAME="ReviewExercises"><FONT SIZE=5 COLOR=#FF0000>

Review Exercises</FONT></A></H2>

<OL>

<LI>Open your access server log and count and display the total

number of HTTP requests.

<LI>Modify the program from exercise 1 to display and average

the number of requests per day.

<LI>Change Listing 21.3 so that HTTP requests that don't specify

a filename translate into specifying <TT>index.html</TT>

or the filename of that directory's default page..

<LI>Change Listing 21.3 to sort by the access count instead of

the document name.

</OL>

<HR>



<CENTER><P><A HREF="ch20.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch20.htm"><IMG SRC="pc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="#CONTENTS"><IMG SRC="cc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/cc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="index-1.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/index-1.htm"><IMG SRC="hb.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/hb.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>

<A HREF="ch22.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch22.htm"><IMG SRC="nc.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/nc.gif" BORDER=0 HEIGHT=88 WIDTH=140></A>

<HR WIDTH="100%"></P></CENTER>

</BODY>

</HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -