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

📄 ch20.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 5 页
字号:
Define the </I><TT><I>getFormData()</I></TT><I>fuNCtion.<BR>Declare a local variable to hold the refereNCe to the input fieldhash.<BR>Initialize a buffer.<BR>If the </I><TT><I>GET</I></TT><I>method is used, copy the form information into the buffer.<BR>If the </I><TT><I>POST</I></TT><I>method is used, read the form information into the buffer.<BR>Iterate over the array returned by the </I><TT><I>split()</I></TT><I>fuNCtion.<BR>Decode both the input field name and value.<BR>Compress multiple </I><TT><I>&lt;P&gt;</I></TT><I>tags into one.<BR>Convert </I><TT><I>&lt;</I></TT><I>into </I><TT><I>&amp;lt;</I></TT><I>and </I><TT><I>&gt;</I></TT><I> into</I><TT><I>&amp;gt;</I></TT><I> stoppingHTML tags from interpretation.<BR>Turn back on the bold and italic HTML tags.<BR>Remove unneded carriage returns.<BR>Convert two newlines into a HTML paragraph tag.<BR>Convert single newlines into spaces.<BR>Create an entry in the input field hash variable.<BR>Define the </I><TT><I>decodeURL()</I></TT><I>fuNCtion.<BR>Get the eNCoded string from the parameter array.<BR>Translate all plus signs into spaces.<BR>Convert character coded as hexadecimal digits into regular characters.<BR>Return the decoded string.<BR>Define the </I><TT><I>zeroFill()</I></TT><I>fuNCtion-turns &quot;1&quot; into &quot;01&quot;.<BR>Declare a local variable to hold the number to be filled.<BR>Declare a local variable to hold the string length that is needed.<BR>Find differeNCe between current string length and needed length.<BR>If the string is big enough (like &quot;12&quot;) then returnit.<BR>If the string is too big, prefix it with some zeroes.<BR>Define the </I><TT><I>saveFormData()</I></TT><I>fuNCtion.<BR>Declare two local variables to hold the hash and file name.<BR>Open the file for appending.<BR>Store the contents of the hash in the data file.<BR>Close the file.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 20.6&nbsp;&nbsp;20LST06.PL-A More AdvaNCed Guestbook<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>#! /user/bin/perl -w#use strict;    my(%fields);    my($sec, $min, $hour, $mday, $mon, $year) = (localtime(time))[0..5];    my($dataFile) = &quot;data/gestbook.dat&quot;;    $mon  = zeroFill($mon, 2);    $hour = zeroFill($hour, 2);    $min  = zeroFill($min, 2);    $sec  = zeroFill($sec, 2);    $fields{'timestamp'} = &quot;$mon/$mday/$year, $hour:$min:$sec&quot;;    getFormData(\%fields);    if ($ENV{'QUERY_STRING'}) {        if ($ENV{'QUERY_STRING'} eq 'display') {            displayPage();        }        elsif ($ENV{'QUERY_STRING'} eq 'add') {            print(&quot;Location: /addgest.htm\n\n&quot;);        }        else {            displayError(&quot;Unknown Command: &lt;B&gt;$ENV{'QUERY_STRING'}&lt;/B&gt;&quot;);        }    }    else {        if (length($fields{'name'}) == 0) {            displayError(&quot;Please fill the name field,&lt;BR&gt;\n&quot;);        }if (length($fields{'comments'}) == 0) {            displayError(&quot;Please fill the comments field,&lt;BR&gt;\n&quot;);        }        saveFormData(\%fields, $dataFile);        displayPage();    }    exit(0);sub displayError {    print(&quot;Content-type: text/html\n\n&quot;);    print(&quot;&lt;HTML&gt;\n&quot;);    print(&quot;&lt;HEAD&gt;&lt;TITLE&gt;Guestbook Error&lt;/TITLE&gt;&lt;/HEAD&gt;\n&quot;);    print(&quot;&lt;H1&gt;Guestbook&lt;/H1&gt;\n&quot;);    print(&quot;&lt;HR&gt;\n&quot;);    print(&quot;@_&lt;BR&gt;\n&quot;);    print(&quot;&lt;HR&gt;\n&quot;);    printENV();    print(&quot;&lt;/BODY&gt;\n&quot;);    print(&quot;&lt;/HTML&gt;\n&quot;);    exit(0);}        sub displayPage {    my(%entries);    readFormData($dataFile, \%entries);    print(&quot;Content-type: text/html\n\n&quot;);    print(&quot;&lt;HTML&gt;\n&quot;);    print(&quot;&lt;HEAD&gt;&lt;TITLE&gt;Guestbook&lt;/TITLE&gt;&lt;/HEAD&gt;\n&quot;);    print(&quot;&lt;TABLE&gt;&lt;TR&gt;&lt;TD VALIGN=top&gt;&lt;H1&gt;Guestbook&lt;/H1&gt;&lt;/TD&gt;\n&quot;);    print(&quot;&lt;TD VALIGN=top&gt;&lt;UL&gt;&lt;LI&gt;&lt;A HREF=\&quot;/cgi-bin/gestbook.pl?add\&quot;&gt;Add an Entry&lt;/A&gt;\n&quot;);    print(&quot;&lt;LI&gt;&lt;A HREF=\&quot;/cgi-bin/gestbook.pl?display\&quot;&gt;Refresh&lt;/A&gt;&lt;/UL&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TABLE&gt;\n&quot;);    print(&quot;&lt;HR&gt;\n&quot;);    foreach (sort(keys(%entries))) {        my($arrayRef) = $entries{$_};        my($timestamp, $name, $email, $comments) = ($_, @{$arrayRef});        print(&quot;$timestamp: &lt;B&gt;$name&lt;/B&gt; &lt;A HREF=mailto:$email&gt;$email&lt;/A&gt;\n&quot;);        print(&quot;&lt;OL&gt;$comments&lt;/OL&gt;\n&quot;);        print(&quot;&lt;HR&gt;\n&quot;);    }    print(&quot;&lt;/BODY&gt;\n&quot;);    print(&quot;&lt;/HTML&gt;\n&quot;);}sub readFormData {    my($file)    = shift;    my($hashRef) = shift;    open(FILE, &quot;&lt;$file&quot;) or displayError(&quot;Unable to open Guestbook data file.&quot;);    while (&lt;FILE&gt;) {        my($timestamp, $name, $email, $comments) = split(/~/, $_);        $hashRef-&gt;{$timestamp} = [ $name, $email, $comments ];    }    close(FILE);}sub getFormData {    my($hashRef) = shift;    my($buffer) = &quot;&quot;;    if ($ENV{'REQUEST_METHOD'} eq &quot;GET&quot;) {        $buffer = $ENV{'QUERY_STRING'};    }       else {        read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});    }    foreach (split(/&amp;/, $buffer)) {        my($key, $value) = split(/=/, $_);        $key   = decodeURL($key);        $value = decodeURL($value);        $value =~ s/(&lt;P&gt;\s*)+/&lt;P&gt;/g;   # compress multiple &lt;P&gt; tags.        $value =~ s/&lt;/&amp;lt;/g;           # turn off all HTML tags.        $value =~ s/&gt;/&amp;gt;/g;        $value =~ s/&amp;lt;b&amp;gt;/&lt;b&gt;/ig;    # turn on the bold tag.        $value =~ s!&amp;lt;/b&amp;gt;!&lt;/b&gt;!ig;        $value =~ s/&amp;lt;i&amp;gt;/&lt;b&gt;/ig;    # turn on the italic tag.        $value =~ s!&amp;lt;/i&amp;gt;!&lt;/b&gt;!ig;        $value =~ s!\cM!!g;            # Remove unneeded carriage returns.        $value =~ s!\n\n!&lt;P&gt;!g;        # Convert 2 newlines into paragraph.        $value =~ s!\n! !g;            # convert newline into space.        %{$hashRef}-&gt;{$key} = $value;    }}sub decodeURL {    $_ = shift;    tr/+/ /;    s/%(..)/pack('c', hex($1))/eg;    return($_);}sub zeroFill {    my($temp) = shift;    my($len)  = shift;    my($diff) = $len - length($temp);    return($temp) if $diff &lt;= 0;    return(('0' x $diff) . $temp);}sub saveFormData {    my($hashRef) = shift;    my($file)    = shift;    open(FILE, &quot;&gt;&gt;$file&quot;) or die(&quot;Unable to open Guestbook data file.&quot;);    print FILE (&quot;$hashRef-&gt;{'timestamp'}~&quot;);    print FILE (&quot;$hashRef-&gt;{'name'}~&quot;);    print FILE (&quot;$hashRef-&gt;{'email'}~&quot;);    print FILE (&quot;$hashRef-&gt;{'comments'}&quot;);    print FILE (&quot;\n&quot;);    close(FILE);}sub printENV {    print &quot;The Environment report&lt;BR&gt;\n&quot;;    print &quot;----------------------&lt;BR&gt;&lt;PRE&gt;\n&quot;;    print &quot;REQUEST_METHOD:  *$ENV{'REQUEST_METHOD'}*\n&quot;;    print &quot;SCRIPT_NAME:     *$ENV{'SCRIPT_NAME'}*\n&quot;;    print &quot;QUERY_STRING:    *$ENV{'QUERY_STRING'}*\n&quot;;    print &quot;PATH_INFO:       *$ENV{'PATH_INFO'}*\n&quot;;    print &quot;PATH_TRANSLATED: *$ENV{'PATH_TRANSLATED'}*&lt;/PRE&gt;\n&quot;;    if ($ENV{'REQUEST_METHOD'} eq 'POST') {        print &quot;CONTENT_TYPE:    $ENV{'CONTENT_TYPE'}&lt;BR&gt;\n&quot;;        print &quot;CONTENT_FILE:    $ENV{'CONTENT_FILE'}&lt;BR&gt;\n&quot;;        print &quot;CONTENT_LENGTH:  $ENV{'CONTENT_LENGTH'}&lt;BR&gt;\n&quot;;    }    print(&quot;&lt;BR&gt;&quot;);    foreach (sort(keys(%ENV))) {        print(&quot;$_: $ENV{$_}&lt;BR&gt;\n&quot;);    }    print(&quot;&lt;BR&gt;&quot;);     foreach (sort(keys(%fields))) {        print(&quot;$_: $fields{$_}&lt;BR&gt;\n&quot;);    }    print(&quot;&lt;BR&gt;&quot;);}</PRE></BLOCKQUOTE><HR><P>One of the major changes between Listing 20.5 and Listing 20.6is in the <TT>readFormData()</TT>fuNCtion. Instead of actually printing the Guest book data, thefuNCtion now creates hash entries for it. This change was doneso that an error page could be generated if the data file couldnot be opened. Otherwise, the error message would have appearedit the middle of the Guest book page-leading to confusion on thepart of vistors.<P>A table was used to add two hypertext links to the top of theweb page. One link will let visitors add a new entry and the otherrefreshes the page. If a second visitor has added a Guest bookentry while the first visitor was reading, refreshing the pagewill display the new entry.<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></A></H2><P>This chapter introduced you to HTML forms and form processing.You learned that HTML tags are used to provide guidelines abouthow the content of a document. For example, the <TT>&lt;P&gt;</TT>tag indicates a new paragraph is starting and the <TT>&lt;H1&gt;..&lt;/H1&gt;</TT>tags indicate a text heading.<P>A &quot;correct&quot; HTML document will be entirely eNClosedinside of a set of &lt;HTML&gt;..&lt;/HTML&gt; tags. Inside the<TT>&lt;HTML&gt;</TT> tag are <TT>&lt;HEAD&gt;..&lt;/HEAD&gt;</TT>(surrounds document identification information) and <TT>&lt;BODY&gt;..&lt;/BODY&gt;</TT>(surrounds document content information) tags.<P>After the brief introduction to HTML, you read about Server-SideINCludes. They are used to insert information into a documentat the time that the page is sent to the Web browser. This letsthe document designer create dynamic pages without needing CGIprograms. For example, you can display the last modification dateof a document, or iNClude other document such as a standard footerfile.<P>Next, HTML forms were discussed. HTML forms display input fieldsthat query the visitor to your Web site. You can display inputboxes, checkboxes, radio but-tons, selection lists, submit buttonsand reset buttons. Everything inside a set of <TT>&lt;FORM&gt;..&lt;/FORM&gt;</TT>tags is considered one form. You can have multiple forms on asingle Web page.<P>The <TT>&lt;FORM&gt;</TT> tag takestwo modifiers. The <TT>ACTION</TT>modifier tell the web browser the name of the CGI program thatgets invoked when the form's submit button is clicked. And the<TT>METHOD</TT> modifier determineshow the form information should be sent to the CGI program. Ifthe <TT>GET</TT> method is used, theinformation from the form's fields will be available in the <TT>QUERY_STRING</TT>environment variable. IF the <TT>POST</TT>method is used, the form information will be available via the<TT>STDIN</TT> variable.<P>The <TT>getFormData()</TT> fuNCtionwas developed to process form information about make it availablevia a hash variable. This fuNCtion is the first line of defenseagainst hackers. By investing time developing this fuNCtion toclose security holes, you are rewarded by having a safer, morestable web site.<P>Debugging a CGI script takes a little bit of preparation. First,create a batch or shell file that defines the environment variablesthat your CGI program needs. Then, create a test input file ifyou are using the <TT>POST</TT> method.Lastly, execute the CGI program from the command line using redirectionto point STDIN to your test input file.<P>Next, a Guestbook application was presented. This applicationused an HTML form to gather comments from a user. The commentsare saved to a database. Then, all of the comments stored in thedatabase are displayed. The first version of the Guestbook requiredthe user to add an entry before seeing the contents of the Guest-book. The second version of the Guestbook let users view the contentswithout this requirement. In addition, better error checking andnew features were added.<P>The next chapter, &quot;Using Perl with Web Servers,&quot; exploresweb server log files and ways to automatically create Web pages.<H2><A NAME="ReviewQuestions"><FONT SIZE=5 COLOR=#FF0000>Review Questions</FONT></A></H2><P>Answers to Review Questions are in Appendix A. <OL><LI>What does the acronym HTML stand for?<LI>What are the &lt;H1&gt;..&lt;/H1&gt; set of tags used for?<LI>What is the down side of using SSI directives?<LI>Can an HTML form have two submit buttons?<LI>Why should all angle brackets be replaced in form information?<LI>How much text can be entered into a &lt;TEXTAREA&gt; inputfield?<LI>Can you debug a CGI script?</OL><H2><A NAME="ReviewExercises"><FONT SIZE=5

⌨️ 快捷键说明

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