📄 ch07.htm
字号:
print $q->textarea(-name=>`Address',-rows=>3,-cols=>32);
print "<TR><TD>City:<TD>\n";
print $q->textfield(-name=>`City',-size=>32,-maxlength=>32);
print "<TR><TD>State:<TD>\n";
print $q->popup_menu(-name=>`State',-value=>\@states);
print "<TR><TD>Zip Code:<TD>\n";
print $q->textfield(-name=>`ZipCode',-size=>10,-maxlength=>10);
print "<TR><TD>Phone:<TD>\n";
print $q->textfield(-name=>`Phone',-size=>12,-maxlength=>12);
print "<TR><TD>Fax:<TD>\n";
print $q->textfield(-name=>`Fax',-size=>12,-maxlength=>12);
print "<TR><TD>e-mail:<TD>\n";
print $q->textfield(-name=>`email',-size=>32,-maxlength=>32);
print "<TR><TD>How did you hear about us?<BR>";
print $q->radio_group(-name=>`How',-values=>\@hows,-linebreak=>`true');
print "<TD>Which product(s) are you interested in?<BR>";
print $q->scrolling_list(-name=>`What',-values=>\@products,
-multiple=>`true'-linebreak=>`true');
print "</TABLE>\n";
print "Any Comments?<BR>\n";
print $q->textarea(-name=>`Comment',-rows=>10,-cols=>60);
print "<HR>\n";
print $q->submit(-name=>`Action',-value=>`Submit');
print " ";
print $q->reset(-value=>`Start from Scratch');
print $q->endform();
}
</FONT></PRE>
<H4 ALIGN="CENTER"><A NAME="Heading7"></A><FONT COLOR="#000077">Processing the POST</FONT></H4>
<P>When the user clicks Submit, the form data is sent back up to the server and the
Perl script is called with the request method <TT>POST</TT>. All the values in the
form can be obtained using the <TT>param()</TT> method. The Perl subroutine in Listing
7.2 gathers all the information into a single record of data and returns the delimited
string that makes up a row in the database.
<H3 ALIGN="CENTER"><A NAME="Heading8"></A><FONT COLOR="#000077">Listing 7.2. Perl
subroutine to process the form data.</FONT></H3>
<PRE><FONT COLOR="#0066FF">sub gatherData {
my($q)=@_;
my(@orderedList)=();
push(@orderedList,$q->param(`Name'));
push(@orderedList,$q->param(`Title'));
push(@orderedList,$q->param(`Company'));
push(@orderedList,$q->param(`Address'));
push(@orderedList,$q->param(`City'));
push(@orderedList,$q->param(`State'));
push(@orderedList,$q->param(`ZipCode'));
push(@orderedList,$q->param(`Phone'));
push(@orderedList,$q->param(`Fax'));
push(@orderedList,$q->param(`email'));
push(@orderedList,$q->param(`How'));
push(@orderedList,$q->param(`What'));
push(@orderedList,$q->param(`Comment'));
return join(`<*>`,@orderedList);
}
</FONT></PRE>
<H4 ALIGN="CENTER"><A NAME="Heading9"></A><FONT COLOR="#000077">Putting It All Together</FONT></H4>
<P>The main code in the guest book program is now pretty simple. All you need to
do is figure out whether you're handling a <TT>GET</TT> or a <TT>POST</TT> and do
the appropriate thing. An environment variable called <TT>REQUEST_METHOD</TT> tells
you whether it is <TT>POST</TT> or <TT>GET</TT>. You obtain the value of this environment
variable using the <TT>var()</TT> method of the inherited <TT>CGI::Base</TT> class.
Assume you keep the database in a file called <TT>guests.list</TT>. You can now put
this whole thing together with just a few lines of code, as shown in Listing 7.3.
<H3 ALIGN="CENTER"><A NAME="Heading10"></A><FONT COLOR="#000077">Listing 7.3. Main
Perl guest book CGI program.</FONT></H3>
<PRE><FONT COLOR="#0066FF">#!/public/bin/perl5
# Standard header stuff
use CGI::Form;
$q = new CGI::Form;
print $q->header();
print $q->start_html(-title=>`Welcome to Widget World',
-author=>`webmaster\@widgets.com');
if ($q->cgi->var(`REQUEST_METHOD') eq `GET') {
&guestBookForm($q);
} else {
open(DATABASE,">> guests.list") ||
die "Cannot open guest book list for append!\n";
print DATABASE &gatherData($q);
print DATABASE "\n";
close(DATABASE);
print "<P>Thank you for taking the time to enter our guest book! ";
print "We look forward to doing business with you.";
}
print "<HR>\n<P>If you have any problems with this form, please contact ";
print "our <A HREF=mailto:webmaster\@widgets.com>Web master</A>";
print $q->end_html();
</FONT></PRE>
<H4 ALIGN="CENTER"><A NAME="Heading11"></A><FONT COLOR="#000077">Displaying the Complete
Guest List</FONT></H4>
<P>One last thing you might want to do (now that you have a database with some data
in it) is to provide a CGI script that displays the guest book in a nice format (see
Figure 7.2). This can be done by parsing the database file and generating the HTML
markup on-the-fly. The example in Listing 7.4 shows one option on how you can do
this.
<H3 ALIGN="CENTER"><A NAME="Heading12"></A><FONT COLOR="#000077">Listing 7.4. CGI
program to display the guest list.</FONT></H3>
<PRE><FONT COLOR="#0066FF">#!/public/bin/perl5
use CGI::Form;
$q = new CGI::Form;
print $q->header();
print $q->start_html(-title=>`Guest List');
print "<H1>Guest List</H1>\n";
print "<TABLE BORDER>\n";
print "<TR><TH>Name<TH>Title<TH>Company<TH>Address<TH>City<TH>State<TH>Zip";
print "<TH>Phone<TH>Fax<TH>e-mail<TH>How they heard<TH>Products of interest";
print "<TH>Comments";
open(DATABASE,"< guests.list") ||
die "Cannot open guest list database for read!\n";
while(<DATABASE>) {
print "<TR>\n";
print "<TD>";
@fields=split(/\<\*\>/);
print join(`<TD>`,@fields);
print "</TR>";
}
close(DATABASE);
print "</TABLE>";
</FONT></PRE>
<P><A HREF="08wpp02.jpg" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/08wpp02.jpg"><TT><B>Figure 7.2.</B></TT></A><TT> </TT>The table
of guests in the guest book.
<H4 ALIGN="CENTER"><A NAME="Heading13"></A><FONT COLOR="#000077">Review</FONT></H4>
<P>What you've seen in this section is how to implement your own guest book and the
ease with which it can be done using Perl5 and the WWW libraries. You should also
consider adding some nice images to your guest book. You should use everything that
HTML offers to provide a nice experience for your visitor.
<H3 ALIGN="CENTER"><A NAME="Heading14"></A><FONT COLOR="#000077">Hit Counter</FONT></H3>
<P>Another common use of CGI scripts is the hit counter. A hit counter is used to
determine how many times your page has been accessed. Web servers can be configured
to perform certain levels of logging. Although this can slow down the server somewhat,
it can also provide valuable information to you. Many people want to know how popular
their Web sites are, and the hit counter allows a Webmaster to show off to new visitors
just exactly how popular a site is.
<H4 ALIGN="CENTER"><A NAME="Heading15"></A><FONT COLOR="#000077">Introduction</FONT></H4>
<P>Hit counters come in all different forms. You can have a normal ASCII text counter,
or you can get creative and make a graphical counter. A common approach is to use
the concept of an odometer on a car. I will show you how to obtain the number in
this example and give you one example on how to make the display graphical.</P>
<P>The number of accesses is not the only type of counter you can provide. You can
also find out how many times your page was referred by another page and also what
type of browsers are accessing your page.
<H4 ALIGN="CENTER"><A NAME="Heading16"></A><FONT COLOR="#000077">Setting Up the Web
Server to Log Access</FONT></H4>
<P>This section describes how to set up the NCSA httpd Web server for logging access
to your Web site. This mechanism also applies to the Apache Web server and a few
others that are based on httpd. Windows- and Macintosh-based servers usually provide
a GUI front-end to these server options.</P>
<P>The NCSA httpd server has a configuration file called <TT>httpd.conf</TT>. This
is an ASCII text file that is used to configure the server options. Within this configuration
file, four variables are used to define where certain logs are kept. <TT>ErrorLog</TT>
defines where the Web server should redirect <TT>STDERR</TT>; <TT>TransferLog</TT>
defines where the page accesses are logged; <TT>AgentLog</TT> defines where the client
information is logged; and <TT>RefererLog</TT> defines where the referring pages
are logged. <TT>ErrorLog</TT> isn't something you would worry about in this example,
although it is a very useful file to be aware of. This example focuses on <TT>TransferLog</TT>,
<TT>AgentLog</TT>, and <TT>RefererLog</TT>.
<H4 ALIGN="CENTER"><A NAME="Heading17"></A><FONT COLOR="#000077">Parsing the Access
Log</FONT></H4>
<P>To determine how many times a certain page has been visited, you need to scan
the <TT>TransferLog</TT>. First, find out where your log file is kept. Let's assume
that the Web server is installed in <TT>/usr/etc/httpd</TT> and that you have set
<TT>TransferLog</TT> to <TT>logs/access_log</TT>. The file that you need to look
at is <TT>/usr/etc/httpd/logs/access_log</TT>. Each line in this file pertains to
one hit on a single object in your Web site. By using a Perl regular expression,
you can search for the page in question and return the number of times that page
has been found in the access log. The following line is an example of a record from
the <TT>TransferLog</TT>.</P>
<PRE><FONT COLOR="#0066FF">www-proxy - - [06/Dec/1995:13:40:52 -0800] "GET /index.html HTTP/1.0" 200 638
</FONT></PRE>
<P>The Perl program in Listing 7.5 opens the access log and uses a regular expression
to search for the number of occurrences in the file. The page to search for is passed
in as an argument to this function.
<H3 ALIGN="CENTER"><A NAME="Heading18"></A><FONT COLOR="#000077">Listing 7.5. Perl
subroutine to count the number of hits on a given page.</FONT></H3>
<PRE><FONT COLOR="#0066FF">sub pageCount {
my($page)=@_;
# Pre-pend the GET method to limit the search scope.
my($srchStr) = "GET $page";
open(IN,"< /usr/etc/httpd/logs/access_log") ||
die "Cannot open access log! $?\n";
return(scalar(grep(/$srchStr/, <IN>)));}
</FONT></PRE>
<P>This code can be included in your CGI script to display the number of hits on
a given page. It can also be used outside of the Web site to provide statistics.
<TT>$page</TT> is defined as a document path, relative to the document root of your
server.</P>
<P>This routine can also be used in conjunction with some images to display a graphical
hit counter. Suppose, for example, you have an image for each digit. You could take
the resulting number from this function, treat it as a string, and use each digit
value to locate the image associated with the digit, as shown in Listing 7.6.
<H3 ALIGN="CENTER"><A NAME="Heading19"></A><FONT COLOR="#000077">Listing 7.6. CGI
script that displays a graphical hit counter.</FONT></H3>
<PRE><FONT COLOR="#0066FF">my($count)=&pageCount("/index.html");
my($len)=length($count);
my($i);
my($imageStr)="";
for ($i=0;$i<$len;$i++) {
my($digit)=substr($count,$i,1);
$imageStr .= "<IMG SRC=digit$digit.gif>";
}
print "$imageStr\n";
</FONT></PRE>
<P>Therefore, if the hit count turns out to be 342, this code results in HTML that
looks like</P>
<PRE><FONT COLOR="#0066FF"><IMG SRC=digit3.gif><IMG SRC=digit4.gif><IMG SRC=digit2.gif>
</FONT></PRE>
<P>which will appear in the browser as in Figure 7.3. <BR>
<BR>
<A HREF="08wpp03.jpg" tppabs="http://210.32.137.15/ebook/Web%20Programming%20with%20Perl%205/08wpp03.jpg"><TT><B>Figure 7.3.</B></TT></A><TT> </TT>Demonstration
of a graphical hit counter. <BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -