📄 ch21.htm
字号:
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></BODY></TT>
tag.
<BLOCKQUOTE>
<PRE><FORM METHOD=POST Action="cgi-bin/feedback.pl">
<INPUT TYPE=hidden NAME="to" VALUE="xxxxxxxxxxxxxxxxxx">
<INPUT TYPE=hidden NAME="subject" VALUE="Home Page">
<CENTER>
<INPUT TYPE=submit VALUE="Send a comment to the webmaster">
</CENTER>
</FORM><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 & 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 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 "Content-type: text/html\n\n";
print("<HTML>");
print("<HEAD><TITLE>Web Page Comment Form</TITLE></HEAD>\n");
print("<BODY>\n");
print("<H1 ALIGN=CENTER>Web Page Comment Form</H1>\n");
print("<FORM METHOD=\"POST\" Action=\"mailto:$formData{'to'}\">\n");
print("<TABLE CELLPADDING=3>");
print("<TR><TD>To:</TD><TD>$formData{'to'}<TD></TR>\n");
print("<TR><TD>Subject:</TD><TD>$formData{'subject'}</TD></TR>\n");
print("<TR>");
print("<TD><B>Your email address:</B></TD>");
print("<TD><INPUT TYPE=\"text\" NAME=\"addr\" SIZE=40 MAXLENGTH=80></TD>");
print("</TR>\n");
print("<TR><TD VALIGN=top><B>How urgently do you need a reply:</B></TD>\n");
print("<TD><INPUT TYPE=\"radio\" NAME=\"urgeNCy\" VALUE=\"fyi\" CHECKED>
Just FYI\n");
print("<INPUT TYPE=\"radio\" NAME=\"urgeNCy\" VALUE=\"plr\"> Please
Reply\n");
print("<INPUT TYPE=\"radio\" NAME=\"urgeNCy\" VALUE=\"rur\"> Reply Urgently</TD><TR>\n");
print("<TR><TD VALIGN=top><B>What is the nature of your feedback:</B></TD>\n");
print("<TD><SELECT NAME=\"nature\" SIZE=3 MULTIPLE>\n");
print("<OPTION SELECTED>General Comments\n");
print("<OPTION> Found Typo\n");
print("<OPTION> Bug Report\n");
print("</SELECT></TD></TR>\n");
print("<TR><TD VALIGN=top><B>Please enter your comments:</B></TD>\n");
print("<TD><TEXTAREA NAME=\"comment\" COLS=50 ROWS=5></TEXTAREA></TD></TR>\n");
print("</TABLE><P>");
print("<CENTER><INPUT TYPE=\"submit\" VALUE=\"Mail Your Comments\"></CENTER>\n");
print("</FORM>");
print("</BODY>");
print("</HTML>");
sub getFormData {
my($hashRef) = shift; # ref to hash to hold form data.
my($in) = ""; # buffer for unprocessed form data.
my($key, $value); # temporary variables.
read(STDIN, $in, $ENV{'CONTENT_LENGTH'});
foreach (split(/&/, $in)) {
s/\+/ /g;
($key, $value) = split(/=/, $_);
%{$hashRef}->{$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, "Form Processing" 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, "Internet Resources," 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 + -