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

📄 ch19_01.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<HTML><HEAD><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:45:11Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="index.htm"TITLE="Perl Cookbook"><LINKREL="prev"HREF="ch18_10.htm"TITLE="18.9. Program: expn and vrfy"><LINKREL="next"HREF="ch19_02.htm"TITLE="19.1. Writing a CGI Script"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch18_10.htm"TITLE="18.9. Program: expn and vrfy"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 18.9. Program: expn and vrfy"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch19_02.htm"TITLE="19.1. Writing a CGI Script"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 19.1. Writing a CGI Script"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="chapter"><H1CLASS="chapter"><ACLASS="title"NAME="ch19-41491">19. CGI Programming</A></H1><DIVCLASS="htmltoc"><P><B>Contents:</B><BR><ACLASS="sect1"HREF="#ch19-11601"TITLE="19.0. Introduction">Introduction</A><BR><ACLASS="sect1"HREF="ch19_02.htm"TITLE="19.1. Writing a CGI Script">Writing a CGI Script</A><BR><ACLASS="sect1"HREF="ch19_03.htm"TITLE="19.2. Redirecting Error Messages">Redirecting Error Messages</A><BR><ACLASS="sect1"HREF="ch19_04.htm"TITLE="19.3. Fixing a 500 Server Error">Fixing a 500 Server Error</A><BR><ACLASS="sect1"HREF="ch19_05.htm"TITLE="19.4. Writing a Safe CGI Program">Writing a Safe CGI Program</A><BR><ACLASS="sect1"HREF="ch19_06.htm"TITLE="19.5. Making CGI Scripts Efficient">Making CGI Scripts Efficient</A><BR><ACLASS="sect1"HREF="ch19_07.htm"TITLE="19.6. Executing Commands Without Shell Escapes">Executing Commands Without Shell Escapes</A><BR><ACLASS="sect1"HREF="ch19_08.htm"TITLE="19.7. Formatting Lists and Tables with HTML Shortcuts">Formatting Lists and Tables with HTML Shortcuts</A><BR><ACLASS="sect1"HREF="ch19_09.htm"TITLE="19.8. Redirecting to a Different Location">Redirecting to a Different Location</A><BR><ACLASS="sect1"HREF="ch19_10.htm"TITLE="19.9. Debugging the Raw HTTP Exchange">Debugging the Raw HTTP Exchange</A><BR><ACLASS="sect1"HREF="ch19_11.htm"TITLE="19.10. Managing Cookies">Managing Cookies</A><BR><ACLASS="sect1"HREF="ch19_12.htm"TITLE="19.11. Creating Sticky Widgets">Creating Sticky Widgets</A><BR><ACLASS="sect1"HREF="ch19_13.htm"TITLE="19.12. Writing a Multiscreen CGI Script">Writing a Multiscreen CGI Script</A><BR><ACLASS="sect1"HREF="ch19_14.htm"TITLE="19.13. Saving a Form to a File or Mail Pipe">Saving a Form to a File or Mail Pipe</A><BR><ACLASS="sect1"HREF="ch19_15.htm"TITLE="19.14. Program: chemiserie">Program: chemiserie</A></P><P></P></DIV><DIVCLASS="epigraph"ALIGN="right"><PCLASS="para"ALIGN="right"><I>A successful tool is one that was used to do something undreamt of by its author.</I></P><PCLASS="attribution"ALIGN="right">-&nbsp;Stephen C. Johnson </P></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch19-11601">19.0. Introduction</A></H2><PCLASS="para"><ACLASS="indexterm"NAME="ch19-idx-1000005328-0"></A>Changes in the environment or the availability of food can make certain species more successful than others at getting food or avoiding predators. Many scientists believe a comet struck the earth millions of years ago, throwing an enormous cloud of dust into the atmosphere. Subsequent radical changes to the environment proved too much for some organisms, say dinosaurs, and hastened their extinction. Other creatures, such as mammals, found new food supplies and freshly exposed habitats to compete in.</P><PCLASS="para">Much as the comet altered the environment for prehistoric species, the Web has altered the environment for modern programming languages. It's opened up new vistas, and although some languages have found themselves eminently unsuited to this new world order, Perl has positively thrived. Because of its strong background in text processing and system glue, Perl has readily adapted itself to the task of providing information using text-based protocols.</P><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-chap19_architecture_0">Architecture</A></H3><PCLASS="para">The <ACLASS="indexterm"NAME="ch19-idx-1000005341-0"></A>Web is driven by plain text. Web servers and web browsers communicate using a text protocol called HTTP, Hypertext Transfer Protocol. Many of the documents exchanged are encoded in a text markup system called HTML, Hypertext Markup Language. This grounding in text is the source of much of the Web's flexibility, power, and success. The only notable exception to the predominance of plain text is the Secure Socket Layer (SSL) protocol that encrypts other protocols like HTTP into binary data that snoopers can't decode.</P><PCLASS="para">Web pages are identified using the <ACLASS="indexterm"NAME="ch19-idx-1000005338-0"></A>Uniform Resource Locator (URL) naming scheme. URLs look like this:</P><PRECLASS="programlisting">http://www.perl.com/CPAN/http://www.perl.com:8001/bad/mojo.htmlftp://gatekeeper.dec.com/pub/misc/netlib.tar.Zftp://anonymous@myplace:gatekeeper.dec.com/pub/misc/netlib.tar.Zfile:///etc/motd</PRE><PCLASS="para">The first part (<CODECLASS="literal">http</CODE>, <CODECLASS="literal">ftp</CODE>, <CODECLASS="literal">file</CODE>) is called the <EMCLASS="emphasis">scheme</EM><ACLASS="indexterm"NAME="ch19-idx-1000005339-0"></A>, and identifies how the file is retrieved. The next part (<CODECLASS="literal">://</CODE>) signifies a hostname will follow, whose interpretation depends on the scheme. After the hostname comes the <EMCLASS="emphasis">path</EM><ACLASS="indexterm"NAME="ch19-idx-1000005340-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005340-1"></A> identifying the document. This path information is also called a <EMCLASS="emphasis">partial URL</EM>.</P><PCLASS="para">The Web is a client-server system. Client browsers like Netscape and Lynx request documents (identified by a partial URL) from web servers like Apache. This browser-to-server dialog is governed by the HTTP protocol. Most of the time, the server merely sends back the contents of a file. Sometimes, however, the web server will run another program to send back a document that could be HTML text, an image, or any other document type. The server-to-program dialog is governed by the CGI (Common Gateway Interface) protocol, so the program that the server runs is a <EMCLASS="emphasis">CGI program</EM> or <EMCLASS="emphasis">CGI script</EM>.</P><PCLASS="para">The server tells the CGI program what page was requested, what values (if any) came in through HTML forms, where the request came from, who they authenticated as (if they authenticated at all), and much more. The CGI program's reply has two parts: headers to say "I'm sending back an HTML document," "I'm sending back a GIF image," or "I'm not sending you anything, go to this page instead," and a document body, perhaps containing GIF image data, plain text, or HTML.</P><PCLASS="para">The CGI protocol is easy to implement wrong and hard to implement right, which is why we recommend using Lincoln Stein's excellent <ACLASS="indexterm"NAME="ch19-idx-1000005351-0"></A>CGI.pm module. It provides convenient functions for accessing the information the server sends you, and for preparing the CGI response the server expects. It is so useful, it is included in the standard Perl distribution as of the 5.004 release, along with helper modules like CGI::Carp and CGI::Fast. We show it off in <ACLASS="xref"HREF="ch19_02.htm"TITLE="Writing a CGI Script">Recipe 19.1</A>.</P><PCLASS="para">Some web servers come with a Perl interpreter embedded in them. This lets you use Perl to generate documents without starting a new process. The system overhead of reading an unchanging page isn't noticable on infrequently accessed pages, even when it's happening several times a second. CGI accesses, however, bog down the machine running the web server. <ACLASS="xref"HREF="ch19_06.htm"TITLE="Making CGI Scripts Efficient">Recipe 19.5</A> shows how to use <CODECLASS="literal">mod_perl</CODE>, the Perl interpreter embedded in the Apache web server, to get the benefits of CGI programs without the overhead.<ACLASS="indexterm"NAME="ch19-idx-1000005343-0"></A></P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch19-chap19_behind_0">Behind the Scenes</A></H3><PCLASS="para">CGI programs are called each time the web server needs a dynamic document generated. It is important to understand that your CGI program doesn't run continuously, with the browser calling different parts of the program. Each request for a partial URL corresponding to your program starts a new copy. Your program generates a page for that request, then quits.</P><PCLASS="para">A browser can request a document in a number of ways called <EMCLASS="emphasis">methods</EM>. (Don't confuse HTTP methods with the methods of object-orientation. They have nothing to do with each other). The <ACLASS="indexterm"NAME="ch19-idx-1000005352-0"></A><ACLASS="indexterm"NAME="ch19-idx-1000005352-1"></A><ACLASS="indexterm"NAME="ch19-idx-1000005352-2"></A><ACLASS="indexterm"NAME="ch19-idx-1000005352-3"></A>GET method is the most common, indicating a simple request for a document. The <ACLASS="indexterm"NAME="ch19-idx-1000005353-0"></A>HEAD method is used when the browser wants to know about the document without actually fetching it. The <ACLASS="indexterm"NAME="ch19-idx-1000005354-0"></A>POST method is used to submit form values.</P><PCLASS="para">Form values can be encoded in both GET and POST methods. With the GET method, values are encoded in the URL, leading to ugly URLs like this:</P><PRECLASS="programlisting">http://mox.perl.com/cgi-bin/program?name=Johann&amp;born=1685</PRE><PCLASS="para">With the POST method, values are in a different part of the HTTP request that the browser sends the server. If the form values in the example URL above were sent with a POST request, the user, server, and CGI script all see the URL:</P><PRE

⌨️ 快捷键说明

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