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

📄 ch01_02.htm

📁 用perl编写CGI的好书。本书从解释CGI和底层HTTP协议如何工作开始
💻 HTM
📖 第 1 页 / 共 2 页
字号:
my $time        = localtime;my $remote_id   = $ENV{REMOTE_HOST} || $ENV{REMOTE_ADDR};my $admin_email = $ENV{SERVER_ADMIN};print "Content-type: text/html\n\n";print &lt;&lt;END_OF_PAGE;&lt;HTML&gt;&lt;HEAD&gt;  &lt;TITLE&gt;Welcome to Mike's Mechanics Database&lt;/TITLE&gt;&lt;/HEAD&gt;&lt;BODY BGCOLOR="#ffffff"&gt;  &lt;IMG SRC="/images/mike.jpg" ALT="Mike's Mechanics"&gt;  &lt;P&gt;Welcome from $remote_host! What will you find here? You'll    find a list of mechanics from around the country and the type of    service to expect -- based on user input and suggestions.&lt;/P&gt;  &lt;P&gt;What are you waiting for? Click &lt;A HREF="/cgi/list.cgi"&gt;here&lt;/A&gt;    to continue.&lt;/P&gt;  &lt;HR&gt;  &lt;P&gt;The current time on this server is: $time.&lt;/P&gt;  &lt;P&gt;If you find any problems with this site or have any suggestions,    please email &lt;A HREF="mailto:$admin_email"&gt;$admin_email&lt;/A&gt;.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;END_OF_PAGE</pre></blockquote></div><p>This program is quite simple. It contains only six<a name="INDEX-36" />commands, althoughthe last one is many lines long. Let's take a look at how itworks. Because this script is our first and is short, we'lllook at it line by line; but as mentioned in the Preface, this bookdoes assume that you are already familiar with Perl. So if you do notknow Perl well or if your Perl is a little rusty, you may want tohave a Perl reference available to consult as you read this book. Werecommend <em class="citetitle">Programming Perl, Third Edition,</em> byLarry Wall, Tom Christiansen, and Jon Orwant (O'Reilly &amp;Associates, Inc.); not only is it the standard Perl tome, but it alsohas a convenient alphabetical description of Perl's built-infunctions.</p><p>The first line of the program looks like the top of most Perlscripts. It tells the server to use the program at<em class="filename">/usr/bin/perl</em> to interpret and execute thisscript. You may not recognize the flags, however: the<tt class="command">-wT</tt><a name="INDEX-37" /> <a name="INDEX-38" /><a name="INDEX-39" /> <a name="INDEX-40" /><a name="INDEX-41" /> flags tell Perl to turn on warningsand taint checking. Warnings help locate subtle problems that may notgenerate syntax errors; enabling this is optional, but it is a veryhelpful feature. Taint checking should not be considered optional:unless you like living dangerously, you should enable this featurewith all of your CGI scripts. We will discuss taint checking more in<a href="ch08_01.htm">Chapter 8, "Security"</a>.</p><p>The command <a name="INDEX-42" /> <a name="INDEX-43" /><a name="INDEX-44" /><a name="INDEX-45" /><a name="INDEX-46" /><tt class="function">use strict</tt> tellsPerl to enable strict rules for variables, subroutines, andreferences. If you haven't used this command before, you shouldget into the habit of using it with your<a name="INDEX-47" />CGI scripts. Like warnings,it helps locate subtle mistakes, such as typos, that might nototherwise generate a syntax error. Furthermore, the<em class="emphasis">strict</em> pragma encourages good programmingpractices by forcing you to declare<a name="INDEX-48" /><a name="INDEX-49" />variables andreduce the number of global variables. This produces code that ismore maintainable. Finally, as we will see in <a href="ch17_01.htm">Chapter 17, "Efficiency and Optimization"</a>, the <em class="emphasis">strict</em> pragma isessentially required by<a name="INDEX-50" /><a name="INDEX-51" />FastCGI and<em class="emphasis">mod_perl</em>. If you think you might migrate toeither of these technologies in the future, you should begin using<em class="emphasis">strict</em> now.</p><p>Now we start the real work. First, we set three variables. The firstvariable, <tt class="literal">$time</tt>, is set to a string representingthe current date and time. The second variable,<tt class="literal">$remote_id</tt>, is set to the identity of the remotemachine requesting this page, and we get this information from the<a name="INDEX-52" /><a name="INDEX-53" />environmentvariables REMOTE_HOST or REMOTE_ADDR. As we mentioned earlier, CGIscripts get all of their information from the web server fromenvironment variables and STDIN. REMOTE_HOST contains the full<a name="INDEX-54" />domainname of the remote machine, but only if reverse domain name lookupshave been enabled for the web server -- otherwise, it is blank. Inthis case, we use REMOTE_ADDR instead, which contains the<a name="INDEX-55" />IPaddress of the remote machine. The final variable,<tt class="literal">$admin_email</tt>, is set to SERVER_ADMIN, whichcontains the email address of the server's administratoraccording to the server's configuration files. These are just afew environment variables available to CGI scripts. We'llreview these three in more detail along with the rest in <a href="ch03_01.htm">Chapter 3, "The Common Gateway Interface"</a>.</p><p>As we saw earlier, if a CGI script wants to return a new document, itmust first output an<a name="INDEX-56" />HTTPheader declaring the type of<a name="INDEX-57" />document it is returning. It does thisand prints an additional blank line to indicate that it has finishedsending headers. It then prints the body of the document.</p><p>Instead of using a<tt class="function">print</tt><a name="INDEX-58" /><a name="INDEX-59" /> statement to send each line tostandard output separately, we use a "here" document,which allows us to print a block of text at once. This is a standardPerl feature that's admittedly a little esoteric; you may notbe familiar with this if you have not done other forms of shellprogramming. This command tells Perl to print all of the followinglines until it encounters the <tt class="literal">END_OF_PAGE</tt> token onits own line. It treats the text as if it were enclosed in<a name="INDEX-60" /><a name="INDEX-61" />double quotes, sothe variables are evaluated, but double quotes do not need to beescaped. Not only do "here" documents save us from a lotof extra typing, but they also make the program easier to read.However, there are even better ways of outputting HTML, aswe'll see in <a href="ch05_01.htm">Chapter 5, "CGI.pm"</a>, and <a href="ch06_01.htm">Chapter 6, "HTML Templates"</a>.</p><p>That's all there is to our script, so at this point it exits;the web server adds additional HTTP headers and returns the responseto the client as we saw in <a href="ch01_02.htm#ch01-94693">Example 1-2</a>. This wasjust a simple example of a CGI script, and don't worry if youhave questions or are unsure about a particular detail. As ournumerous references to later chapters indicate, we'll spend therest of the book filling in the details.</p></div><a name="ch01-3-fm2xml" /><div class="sect2"><h3 class="sect2">1.2.2. Invoking CGI Scripts</h3><p><a name="INDEX-62" /> <a name="INDEX-63" /><a name="INDEX-64" /><a name="INDEX-65" />CGIscripts have their own URLs, just like HTML documents and otherresources on the Web. The server is typically<a name="INDEX-66" />configured to map a particularvirtual directory (a directory contained within a URL) to CGIscripts, such as <em class="filename">/cgi-bin</em>,<em class="filename">/cgi</em>, <em class="filename">/scripts</em>, etc.Generally, both the location for CGI scripts on the server'sfilesystem and the corresponding URL path can be overridden in theserver's configuration. We will see how to do this for theApache web server a little later in <a href="ch01_04.htm#ch01-95149">Section 1.4.1, "Configuring CGI Scripts"</a>.</p><p>On <a name="INDEX-67" /> <a name="INDEX-68" />Unix, thefile<a name="INDEX-69" />systemdifferentiates between files that are executable and those that arenot. CGI scripts must be executable. Assuming you have a Perl filethat you have named <em class="filename">my_script.cgi</em>, you wouldissue the following<a name="INDEX-70" /> <a name="INDEX-71" />command from the shell to make a fileexecutable:</p><blockquote><pre class="code">chmod 0755 my_script.cgi</pre></blockquote><p>Forgetting this step is a common problem. On other operating systems,you may have to enable other settings to enable scripts to run. Referto the documentation for your web server.</p></div><hr align="left" width="515" /><div class="navbar"><table border="0" width="515"><tr><td width="172" valign="top" align="left"><a href="ch01_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td width="171" valign="top" align="center"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td width="172" valign="top" align="right"><a href="ch01_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td width="172" valign="top" align="left">1. Getting Started </td><td width="171" valign="top" align="center"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td width="172" valign="top" align="right">1.3. Alternative Technologies</td></tr></table></div><hr align="left" width="515" /><img src="../gifs/navbar.gif" alt="Library Navigation Links" usemap="#library-map" border="0" /><p><font size="-1"><a href="copyrght.htm">Copyright &copy; 2001</a> O'Reilly &amp; Associates. All rights reserved.</font></p><map name="library-map"><area href="../index.htm" coords="1,1,83,102" shape="rect" /><area href="../lnut/index.htm" coords="81,0,152,95" shape="rect" /><area href="../run/index.htm" coords="172,2,252,105" shape="rect" /><area href="../apache/index.htm" coords="238,2,334,95" shape="rect" /><area href="../sql/index.htm" coords="336,0,412,104" shape="rect" /><area href="../dbi/index.htm" coords="415,0,507,101" shape="rect" /><area href="../cgi/index.htm" coords="511,0,601,99" shape="rect" /></map></body></html>

⌨️ 快捷键说明

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