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

📄 ch19.htm

📁 this is a book on pearl , simple example with explanation is given here. it could be beneficial for
💻 HTM
📖 第 1 页 / 共 5 页
字号:
        print(&quot;Content-type: text/html\n\n&quot;);        print(&quot;&lt;HTML&gt;&quot;);        print(&quot;&lt;HEAD&gt;&lt;TITLE&gt;$ENV{'HTTP_USER_AGENT'} doesn't supportCookies&lt;/TITLE&gt;&lt;/HEAD&gt;&quot;);        print(&quot;&lt;BODY&gt;&quot;);        print(&quot;Your browser, $ENV{'HTTP_USER_AGENT'}, doesn't appear tosupport cookies.&quot;);        print(&quot;Cookie Specification.&quot;);        print(&quot;&lt;/BODY&gt;&lt;/HTML&gt;&quot;);    }}</PRE></BLOCKQUOTE><HR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Note</B></TD></TR><TR><TDz><BLOCKQUOTE>You can find more information about cookies at these Web sites:</BLOCKQUOTE><BLOCKQUOTE><TT>http://home.netscape.com/newsref/std/cookie_spec.html<BR>http://www.netscapeworld.com/netscapeworld/nw-07-1996/nw-07-cookies.html<BR>http://www.emf.net/~mal/cookiesinfo.html<BR>http://ds.internic.net/internet-drafts/draft-ietf-http-state-mgmt-03.txt<BR>http://www.illuminatus.com/cookie/<BR>http://www.jasmin.com/cook0696.html<BR>http://www.bravado.net/rodgers/InterNetNews.html</TT></BLOCKQUOTE></TD></TR></TABLE></CENTER><P><H2><A NAME="DebuggingCGIPrograms"><FONT SIZE=5 COLOR=#FF0000>Debugging CGI Programs</FONT></A></H2><P>One of the main reasons to use CGI programs is to generate HTMLdocuments. When something goes wrong, the common error messagewill be 500 Server Error. This message can be caused by severalthings. For example, the #! comment at the top of your scriptcould be invalid, the first line of output was an invalid HTTPheader, there might not be a blank line after the HTTP header,or you could simply have a syntax error.<H3><A NAME="SendingOutputtotheServersLogFile">Sending Output to the Server's Log File</A></H3><P>When your CGI program starts, the <TT>STDERR</TT>file handle is connected to the server's error log.You can use <TT>STDERR </TT>to savestatus messages that you don't want the user to see. The advantageof using <TT>STDERR </TT>is that youdon't need to open or close a file. In addition, you'll alwaysknow where the messages are. This is important if you're workingon a team.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Send the HTTP header indicating a plain text document.<BR>Send a line of text.<BR>Call the </I><TT><I>logError()</I></TT><I>fuNCtion to send a message to the server's log file.<BR>Send a line of text.<BR>Define the </I><TT><I>logError()</I></TT><I>fuNCtion<BR>Declare a local variable to hold the message.<BR>Print the message to </I><TT><I>STDERR </I></TT><I>witha timestamp.<BR>Define the </I><TT><I>timeStamp()</I></TT><I>fuNCtion.<BR>Declare some local variables to hold the current date and time.<BR>Call the </I><TT><I>zeroFill()</I></TT><I>fuNCtion to format the numbers.<BR>Return a formatted string holding the current date and time.<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.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 19.6&nbsp;&nbsp;19LST06.PL-Sending Messages to theServer's Error Log<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>print(&quot;Content-type: text/plain\n\n&quot;);print(&quot;This is line one.\n&quot;);logError(&quot;GOOD Status\n&quot;);logError(&quot;BAD  Status\n&quot;);print(&quot;This is line two.\n&quot;); sub logError {    my($msg) = shift;    print STDERR (timeStamp(), &quot; $msg&quot;);}sub timeStamp {    my($sec, $min, $hour, $mday, $mon, $year) = (localtime(time))[0..5];    $mon  = zeroFill($mon, 2);    $hour = zeroFill($hour, 2);    $min  = zeroFill($min, 2);    $sec  = zeroFill($sec, 2);    return(&quot;$mon/$mday/$year, $hour:$min:sec&quot;);}sub zeroFill {    my($temp) = shift;    my($len)  = shift;    my($diff) = $len - length($temp);    return($temp) if $diff &lt;= 0;    return(('0' x $diff) . $temp);}</PRE></BLOCKQUOTE><HR><p><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR><TD><B>Caution</B></TD></TR><TR><TD><BLOCKQUOTE>According to the CGI specifications, the <TT>STDERR</TT> file handle should be connected to the server's error log. However, I found that this was not true when using Windows 95 and O'Reilly's Website server software. There may be other combinations of operating systems and server software that also fail to connect <TT>STDERR</TT> to the error log.</BLOCKQUOTE></TD></TR></TABLE></CENTER><H3><A NAME="SendingSTDERRtotheWebBrowser">Sending STDERR to the Web Browser</A></H3><P>If you want your users to see the error messages your script generates,use the <TT>open()</TT> fuNCtion toredirect <TT>STDERR</TT> to <TT>STDOUT</TT>like this:<BLOCKQUOTE><PRE>open(STDERR, &quot;&gt;&amp;STDOUT&quot;);</PRE></BLOCKQUOTE><P>After that statement is executed, the output of all print statementsthat use the STDERR file handle will be displayed in the Web browserwindow.<P>You need to be a little careful when using this ability. Yournormal error messages will not have the HTML tags required tomake them display properly.<H3><A NAME="CGITap">CGITap</A></H3><P>CGITap (<B>http://scendtek.com/cgitap/</B>) is a CGI debuggingaid that can help pinpoint the problem in a troubling CGI application.CGITap installs in the <TT>cgi-bin</TT>directory and runs any CGI programs in &quot;tap mode.&quot; Tapmode runs the CGI program as normal; however, the output containsadditional diagnostic and environment information. This informationcan greatly speed up the process of debugging your CGI scripts.<P>CGITap may be installed in any CGI enabled directory and requiresperl4.036 or later. You can install CGITap by following thesesteps:<OL><LI>Download the CGITap script from the <B>http://scendtek.com/cgitap/</B>Web page.<LI>Install CGITap in a CGI enabled directory-typically namedcgi-bin.<LI>As with any Perl script, be sure the first line of CGITapcontains the correct path to your system's Perl interpreter. Youshould be familiar with the location. If not, try typing <B>whichperl</B> on the UNIX command line.<LI>Check the file permissions to ensure that CGITap is executable.CGITaphas two methods of debugging. The first is adequate for simpleCGI applications that do not use HTML forms for input. The secondmethod is used for CGI programs that process HTML form information.</OL><P>For simple CGIs, add <TT>cgitap</TT>to the URL. For example, normally a CGI program that just printsthe date is called like this:<P><TT>http://localhost/cgi-bin/date</TT><P>That CGI program might display the following in the browser'swindow:<BLOCKQUOTE><PRE>Sun Aug 18 16:07:37 EST 1996</PRE></BLOCKQUOTE><P>In order to use CGITap for debugging, use a similar URL but with<TT>cgitap</TT> inserted.<BLOCKQUOTE><PRE>http://localhost/cgi-bin/cgitap/date</PRE></BLOCKQUOTE><P>CGITap will extract your CGI program's name, display the CGI environmentto the browser, perform some checks on the program, then executethe program and return the actual results (both in HTML sourceand the actual document).<P>CGI programs that process HTML forms will be discussed in Chapter20, &quot;Form Processing,&quot; but while I'm talking about CGITap,let me also mention how to use CGITap with HTML forms. A slightlymore complicated method must be used for debugging complex CGIscripts that require form processing.<P>The URL of a form's action is hard coded (via the ACTION modifierof the <TT>&lt;FORM&gt;</TT> tag)and you may not want to change it to iNClude <TT>cgitap</TT>.To allow CGITap to execute automatically when the form posts toits normal action URL, you can make use of UNIX symbolic links.If you are using Windows NT or Windows 95, you must change theURL in the HTML form. The steps for UNIX platforms are:<OL><LI>Move your CGI script to a new script called <TT>yourscript.tap</TT>by typing <B>mv yourscript yourscript.tap</B>.<LI>Make a symbolic link called <TT>yourscript</TT>to <TT>cgitap</TT> by typing <B>ln-s cgitap yourscript.</B>For example, let's assume you have aCGI script called <TT>mailit</TT>that processes form input data, mails the information to you,and returns an HTML page to the Web browser. To debug this script,move <TT>mailit</TT> to <TT>mailit.tap,</TT>using the following command:</OL><BLOCKQUOTE><PRE>mv mailit mailit.tap</PRE></BLOCKQUOTE><P>Then create a link to <TT>cgitap,</TT>using this command:<BLOCKQUOTE><PRE>ln -s cgitap mailit</PRE></BLOCKQUOTE><P>Now, you can fill in the HTML form and submit it as usual.<P>This method allows UNIX-based scripts and forms to be debuggedwithout having to change hard-coded URLs in your HTML documents.When the form is posted, the results will be the CGITap debugginginformation, followed by the normal output of <TT>mailit</TT>.<H3><A NAME="GeneratinganErrorHTMLPage">Generating an Error HTML Page</A></H3><P>It is a good idea to isolate all program statements that havea high probability to generate errors at the beginning of yourprogram, or, at least, before the HTTP header is sent. This letsyou create HTML response pages that correspond to the specificerror that was eNCountered. Listing 19.7 shows a simple exampleof this coNCept. You could expand this example to cover many differenterrors that can occur.<P><IMG SRC="pseudo.gif" BORDER=1 ALIGN=RIGHT><p><BLOCKQUOTE><I>Try (and fail) to open a file. Call the </I><TT><I>error()</I></TT><I>fuNCtion.<BR>Define the </I><TT><I>error()</I></TT><I>fuNCtion.<BR>Declare a local variable to hold the error message string.<BR>Output an HTML page to display the error message.</I></BLOCKQUOTE><HR><BLOCKQUOTE><B>Listing 19.7&nbsp;&nbsp;19LST07.PL-Generating an Error ResponseUsing HTML<BR></B></BLOCKQUOTE><BLOCKQUOTE><PRE>open(FILE, 'AAAA.AAA') or error(&quot;Could not open file AAAA.AAA&quot;);sub error {    my($message) = @_;    print(&quot;Content-type: text/html\n\n&quot;);    print(&quot;&lt;HTML&gt;\n&quot;);    print(&quot;&lt;HEAD&gt;&lt;TITLE&gt;CGI Error&lt;/TITLE&gt;&lt;/HEAD&gt;\n&quot;);    print(&quot;&lt;H1&gt;Status: 500 An Error Has Occurred&lt;/H1&gt;\n&quot;);    print(&quot;&lt;HR&gt;\n&quot;);    print(&quot;$message\n&quot;);    print(&quot;&lt;/BODY&gt;\n&quot;);    print(&quot;&lt;/HTML&gt;\n&quot;);}</PRE></BLOCKQUOTE><HR><P>I'm sure you agree that error messages that you provide are moreinformative than the standard ones.<H2><A NAME="Summary"><FONT SIZE=5 COLOR=#FF0000>Summary</FONT></A></H2><P>This chapter certainly covered a lot of material. It started bydefining CGI as an interface between Web servers and externalprograms. Then, you read why Perl is a great programming languageto use when writing CGI programs. Next, the chapter touched onCGI applications versus Java applets and how they are complementarytechnologies.<P>After those introductory comments, the fun started. CGI programswere shown to be invoked by a URL. The URL could be entered directlyinto a Web browser or stored in a Web page as a hypertext linkor the destination for HTML form information.<P>Before CGI program can be run under the UNIX operating systems,their file permissions need to be set correctly. Files have threetypes of permissions: read, write, and execute. And there arethree types of users that access files: user, group, and others.CGI programs must be both readable and executable by others.<P>The first line of output of any CGI program must be some typeo

⌨️ 快捷键说明

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