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

📄 ch07.htm

📁 Web_Programming_with_Perl5,一个不错的Perl语言教程。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
   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-&gt;param(`Name'));



   push(@orderedList,$q-&gt;param(`Title'));



   push(@orderedList,$q-&gt;param(`Company'));



   push(@orderedList,$q-&gt;param(`Address'));



   push(@orderedList,$q-&gt;param(`City'));



   push(@orderedList,$q-&gt;param(`State'));



   push(@orderedList,$q-&gt;param(`ZipCode'));



   push(@orderedList,$q-&gt;param(`Phone'));



   push(@orderedList,$q-&gt;param(`Fax'));



   push(@orderedList,$q-&gt;param(`email'));



   push(@orderedList,$q-&gt;param(`How'));



   push(@orderedList,$q-&gt;param(`What'));



   push(@orderedList,$q-&gt;param(`Comment'));



   return join(`&lt;*&gt;`,@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-&gt;header();



print $q-&gt;start_html(-title=&gt;`Welcome to Widget World',



                    -author=&gt;`webmaster\@widgets.com');







if ($q-&gt;cgi-&gt;var(`REQUEST_METHOD') eq `GET') {



   &amp;guestBookForm($q);



} else {



   open(DATABASE,&quot;&gt;&gt; guests.list&quot;) ||



      die &quot;Cannot open guest book list for append!\n&quot;;



   print DATABASE &amp;gatherData($q);



   print DATABASE &quot;\n&quot;;



   close(DATABASE);



   print &quot;&lt;P&gt;Thank you for taking the time to enter our guest book! &quot;;



   print &quot;We look forward to doing business with you.&quot;;



}



print &quot;&lt;HR&gt;\n&lt;P&gt;If you have any problems with this form, please contact &quot;;



print &quot;our &lt;A HREF=mailto:webmaster\@widgets.com&gt;Web master&lt;/A&gt;&quot;;



print $q-&gt;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-&gt;header();



print $q-&gt;start_html(-title=&gt;`Guest List');



print &quot;&lt;H1&gt;Guest List&lt;/H1&gt;\n&quot;;



print &quot;&lt;TABLE BORDER&gt;\n&quot;;



print &quot;&lt;TR&gt;&lt;TH&gt;Name&lt;TH&gt;Title&lt;TH&gt;Company&lt;TH&gt;Address&lt;TH&gt;City&lt;TH&gt;State&lt;TH&gt;Zip&quot;;



print &quot;&lt;TH&gt;Phone&lt;TH&gt;Fax&lt;TH&gt;e-mail&lt;TH&gt;How they heard&lt;TH&gt;Products of interest&quot;;



print &quot;&lt;TH&gt;Comments&quot;;



open(DATABASE,&quot;&lt; guests.list&quot;) ||



   die &quot;Cannot open guest list database for read!\n&quot;;



while(&lt;DATABASE&gt;) {



   print &quot;&lt;TR&gt;\n&quot;;



   print &quot;&lt;TD&gt;&quot;;



   @fields=split(/\&lt;\*\&gt;/);



   print join(`&lt;TD&gt;`,@fields);



   print &quot;&lt;/TR&gt;&quot;;



}



close(DATABASE);



print &quot;&lt;/TABLE&gt;&quot;;



</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] &quot;GET /index.html HTTP/1.0&quot; 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) = &quot;GET $page&quot;;



   open(IN,&quot;&lt; /usr/etc/httpd/logs/access_log&quot;) ||



      die &quot;Cannot open access log! $?\n&quot;;



   return(scalar(grep(/$srchStr/, &lt;IN&gt;)));}



</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)=&amp;pageCount(&quot;/index.html&quot;);



my($len)=length($count);



my($i);



my($imageStr)=&quot;&quot;;



for ($i=0;$i&lt;$len;$i++) {



    my($digit)=substr($count,$i,1);



    $imageStr .= &quot;&lt;IMG SRC=digit$digit.gif&gt;&quot;;



}



print &quot;$imageStr\n&quot;;



</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">&lt;IMG SRC=digit3.gif&gt;&lt;IMG SRC=digit4.gif&gt;&lt;IMG SRC=digit2.gif&gt;



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