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

📄 ch19_04.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
📖 第 1 页 / 共 2 页
字号:
CLASS="replaceable"><I>number=10</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>^D</I></CODE></B></CODE>% perl -w  cgi-script name=joe number=10    # run with mock form input% perl -d  cgi-script name=joe number=10    # ditto, under the debugger# POST method script in csh% (setenv HTTP_METHOD POST; perl -w cgi-script name=joe number=10)# POST method script in sh% HTTP_METHOD=POST perl -w cgi-script name=joe number=10</PRE><PCLASS="para">Check the server's error log. Most web servers redirect CGI process's STDERR into a file. Find that file (try <ICLASS="filename">/usr/local/etc/httpd/logs/error_log</I>, <ICLASS="filename">/usr/local/www/logs/error_log</I>, or just ask your administrator) and see whether any warnings or error messages are showing up there.</P><PCLASS="para">Are you using an old version of Perl? Type <EMCLASS="emphasis">perl -v</EM> to find out. If you're not using 5.004 or better, you or your admins should upgrade, because 5.003 and earlier releases were not protected against buffer overruns. This is a grave security matter.</P><PCLASS="para">Are you using an old version of the libraries? You can either <EMCLASS="emphasis">grep -i version</EM> in the library file (probably in <ICLASS="filename">/usr/lib/perl5/</I>, <ICLASS="filename">/usr/local/lib/perl5</I>, <ICLASS="filename">/usr/lib/perl5/site_perl</I>, or some such). For CGI.pm, and in fact, with any module, you can do this to figure out which version you're using:</P><PRECLASS="programlisting">% perl -MCGI -le 'print CGI-&gt;VERSION'<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>2.49</I></CODE></B></CODE></PRE><PCLASS="para">Are you running the latest version of your web server? It's not often that it happens, but sometimes a web server has bugs that can interfere with your scripts.</P><PCLASS="para">Are you running with the <BCLASS="emphasis.bold">-w</B> switch? This makes Perl gripe about things like using uninitialized variables, reading from a write-only filehandle, and so on.</P><PCLASS="para">Are you running with the<BCLASS="emphasis.bold"> -T</B> flag? If Perl complains about insecure actions, you might be assuming things about your script's input and environment that aren't true. Make it taint-clean (read <ACLASS="xref"HREF="ch19_05.htm"TITLE="Writing a Safe CGI Program">Recipe 19.4</A>, see the <EMCLASS="emphasis">perlsec</EM> manpage to find out about tainting and its consequences for your program, and check the CGI Security FAQ for particular web traps to avoid) and you may sleep easier at night as well as have a working script.</P><PCLASS="para">Are you running with <CODECLASS="literal">use</CODE> <CODECLASS="literal">strict</CODE>? It makes you declare variables before you use them and quote your strings to avoid any confusion with subroutines, and in doing so finds a lot of errors.</P><PCLASS="para">Are you checking the return values of each and every one of your system calls? Many people blindly believe that every <CODECLASS="literal">open</CODE> or <CODECLASS="literal">system</CODE> or <CODECLASS="literal">rename</CODE> or <CODECLASS="literal">unlink</CODE> in their programs will work all the time. These functions return a value so you can find out whether they worked or not&nbsp;- check them!</P><PCLASS="para">Can Perl find the libraries you're using? Write a small script that just prints <CODECLASS="literal">@INC</CODE> (Perl's array of directories it looks for modules and libraries in). Check the permissions on the libraries (they must be readable by the user the script runs as). Don't try to copy modules from one machine to another&nbsp;- a lot of them have compiled and autoloaded components hidden away in the Perl library directory. Install them yourself from scratch.</P><PCLASS="para">Is Perl giving you warnings or errors? Try using CGI::Carp (see <ACLASS="xref"HREF="ch19_03.htm"TITLE="Redirecting Error Messages">Recipe 19.2</A>) to send Perl's error messages and warnings to the browser or a file you have access to.</P></DIV><DIVCLASS="sect3"><H4CLASS="sect3"><ACLASS="title"NAME="ch19-pgfId-1000005217">Is the script upholding its end of the CGI protocol?</A></H4><PCLASS="para">The HTTP header must come before the text or image you return. Don't forget the blank line between the header and body. Also, because STDOUT is not automatically autoflushed but STDERR is, if your script generates warnings or errors to STDERR the web server might see them before it sees your HTTP header and can generate an error on some servers. Add this at the top of your script (after the <CODECLASS="literal">#!</CODE> line) to also flush STDOUT:</P><PRECLASS="programlisting">$| = 1;</PRE><PCLASS="para">Don't ever try to decode the incoming form data by parsing the environment and standard input yourself. There are just too many places where it can go wrong. Use the CGI module and spend your time writing cool programs or reading Usenet instead of tracking down bugs in your implementation of an arcane protocol.</P></DIV><DIVCLASS="sect3"><H4CLASS="sect3"><ACLASS="title"NAME="ch19-pgfId-1000005225">Asking for help elsewhere.</A></H4><PCLASS="para">Check the FAQs and other documents mentioned at the end of the Introduction to this chapter. There is still a chance that you have made a common mistake on whatever system you're using&nbsp;- read the relevant FAQs to make sure you don't embarrass yourself by asking the CGI equivalent of "why doesn't my car run when it's out of gas and oil?"</P><PCLASS="para">Ask a friend. Almost everyone knows somebody they can ask for help. You'll probably get a reply much sooner than if you asked the Net.</P><PCLASS="para">Post to <CODECLASS="literal">comp.infosystems.www.authoring.misc</CODE> if your question is about a CGI script (the CGI module, decoding cookies, finding out where the user is coming from, etc.).<ACLASS="indexterm"NAME="ch19-idx-1000005402-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005402-1"></A><ACLASS="indexterm"NAME="ch19-idx-1000005402-2"></A><ACLASS="indexterm"NAME="ch19-idx-1000005402-3"></A></P></DIV></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-pgfId-340">See Also</A></H3><PCLASS="para"><ACLASS="xref"HREF="ch19_03.htm"TITLE="Redirecting Error Messages">Recipe 19.2</A>; the discussion on buffering in the introduction of <ACLASS="xref"HREF="ch08_01.htm"TITLE="File Contents">Chapter 8, <CITECLASS="chapter">File Contents</CITE></A>; the CGI FAQ at <ACLASS="systemitem.url"HREF="http://www.webthing.com/tutorials/cgifaq.html">http://www.webthing.com/tutorials/cgifaq.html</A></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_03.htm"TITLE="19.2. Redirecting Error Messages"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 19.2. Redirecting Error Messages"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_05.htm"TITLE="19.4. Writing a Safe CGI Program"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19.4. Writing a Safe CGI Program"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">19.2. Redirecting Error Messages</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">19.4. Writing a Safe CGI Program</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

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