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

📄 ch03_02.htm

📁 用perl编写CGI的好书。本书从解释CGI和底层HTTP协议如何工作开始
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<tr><td><p>HTTP_FROM</p></td><td><p>The email address of the user making the request; most browsers donot pass this information, since it is considered an invasion of theuser's privacy.</p></td></tr><tr><td><p>HTTP_HOST</p></td><td><p>The hostname of the server from the requested URL (this correspondsto the HTTP 1.1 <em class="emphasis">Host</em> field).</p></td></tr><tr><td><p>HTTP_REFERER</p></td><td><p>The URL of the document that directed the user to this CGI program(e.g., via a hyperlink or via a form).</p></td></tr><tr><td><p>HTTP_USER_AGENT</p></td><td><p>The name and version of the client's browser.</p></td></tr></table><p>A secure server typically adds many more environment variables for<a name="INDEX-544" /> <a name="INDEX-545" /> <a name="INDEX-546" />secure connections. Much of thisinformation is based on X.509 and provides information about the<a name="INDEX-547" /><a name="INDEX-548" /> <a name="INDEX-549" />server's and possibly thebrowser's certificates. Because you really won't need tounderstand these details in order to write CGI scripts, wewon't get into X.509 or secure HTTP transactions in this book.For more information, refer to RFC 2511 or the public keyinfrastructure working group's web site at <a href="http://www.imc.org/ietf-pkix/">http://www.imc.org/ietf-pkix/</a>.</p><p>The names of the environment variables supplied to your script forsecure connections vary by server. The<a name="INDEX-550" />HTTPS environment variable (see<a href="ch03_02.htm#ch03-35636">Table 3-3</a>) is commonly supported, however, anduseful to test whether your connection is secure; unfortunately itsvalues vary between servers. Refer to your server'sdocumentation for more information or use <a href="ch03_02.htm#ch03-97970">Example 3-1</a> or <a href="ch03_02.htm#ch03-58305">Example 3-2</a> to generatedata for your server.</p><a name="ch03-35636" /><h4 class="objtitle">Table 3-3. Common Environment Variable for Secure Servers</h4><table border="1"><tr><th><p>Environment Variable</p></th><th><p>Description</p></th></tr><tr><td><p>HTTPS</p></td><td><p>This variable can be used as a flag to indicate whether theconnection is secure; its values vary by server (e.g.,"ON" or "on" when secure and blank or"OFF" when not).</p></td></tr></table><p>Finally, the web server may provide additional environment variablesbeyond those mentioned in this section. Most web servers also allowthe administrator to add <a name="INDEX-551" />environment variables viaa configuration file. You might take advantage of this feature if youhave several CGI scripts that all share common configurationinformation, such as the name of the database server to connect to.Having the variable defined once in the web server'sconfiguration file makes it easy to change later.</p></div><a name="ch03-6-fm2xml" /><div class="sect2"><h3 class="sect2">3.2.2. Examining Environment Variables</h3><p>Because <a name="INDEX-552" />browsers and web servers may provideadditional environment variables to your script, it's oftenhelpful to have a list of <a name="INDEX-553" />environment variables that is specific toyour web <a name="INDEX-554" />server. <a href="ch03_02.htm#ch03-97970">Example 3-1</a> shows a short <a name="INDEX-555" />script that is easy to remember and typein when you find yourself working on a new system. It generates ahandy list of environment variables specific to that web server.Remember that the browser may also affect this list. For example,<a name="INDEX-556" />HTTP_COOKIE will onlyappear if the browser supports cookies, if cookies have not beendisabled, and if the browser had received a previous request fromthis web server to set a cookie.</p><a name="ch03-97970" /><div class="example"><h4 class="objtitle">Example 3-1. env.cgi </h4><blockquote><pre class="code">#!/usr/bin/perl -wT# Print a formatted list of all the environment variablesuse strict;print "Content-type: text/html\n\n";my $var_name;foreach $var_name ( sort keys %ENV ) {    print "&lt;P&gt;&lt;B&gt;$var_name&lt;/B&gt;&lt;BR&gt;";    print $ENV{$var_name};}</pre></blockquote></div><p>This simply produces an alphabetic list of the environment variablenames and their values, shown in <a href="ch03_02.htm#ch03-14613">Figure 3-2</a>.</p><p>Because this is simply a quick-and-dirty script, we omitted somedetails that should be included in production CGI scripts, and whichare included in the other examples. For example, we did not print avalid HTML document (it is missing the enclosing HTML, HEADER, andBODY tags). This should certainly be added if the script were to growbeyond a few lines or if you intended for people other than yourselfto use it.</p><p><a href="ch03_02.htm#ch03-58305">Example 3-2</a> shows a more elaborate version thatdisplays all of the environment variables that CGI and your webserver define, along with a brief explanation of the standardvariables.</p><a name="ch03-58305" /><div class="example"><h4 class="objtitle">Example 3-2. env_info.cgi </h4><a name="INDEX-557" /><blockquote><pre class="code">#!/usr/bin/perl -wTuse strict;my %env_info = (    SERVER_SOFTWARE     =&gt; "the server software",    SERVER_NAME         =&gt; "the server hostname or IP address",    GATEWAY_INTERFACE   =&gt; "the CGI specification revision",       SERVER_PROTOCOL     =&gt; "the server protocol name",    SERVER_PORT         =&gt; "the port number for the server",    REQUEST_METHOD      =&gt; "the HTTP request method",    PATH_INFO           =&gt; "the extra path info",    PATH_TRANSLATED     =&gt; "the extra path info translated",    DOCUMENT_ROOT       =&gt; "the server document root directory",    SCRIPT_NAME         =&gt; "the script name",    QUERY_STRING        =&gt; "the query string",    REMOTE_HOST         =&gt; "the hostname of the client",    REMOTE_ADDR         =&gt; "the IP address of the client",    AUTH_TYPE           =&gt; "the authentication method",    REMOTE_USER         =&gt; "the authenticated username",    REMOTE_IDENT        =&gt; "the remote user is (RFC 931): ",    CONTENT_TYPE        =&gt; "the media type of the data",    CONTENT_LENGTH      =&gt; "the length of the request body",    HTTP_ACCEPT         =&gt; "the media types the client accepts",    HTTP_USER_AGENT     =&gt; "the browser the client is using",    HTTP_REFERER        =&gt; "the URL of the referring page",    HTTP_COOKIE         =&gt; "the cookie(s) the client sent");print "Content-type: text/html\n\n";print &lt;&lt;END_OF_HEADING;&lt;HTML&gt;&lt;HEAD&gt;    &lt;TITLE&gt;A List of Environment Variables&lt;/TITLE&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;H1&gt;CGI Environment Variables&lt;/H1&gt;&lt;TABLE BORDER=1&gt;  &lt;TR&gt;    &lt;TH&gt;Variable Name&lt;/TH&gt;    &lt;TH&gt;Description&lt;/TH&gt;    &lt;TH&gt;Value&lt;/TH&gt;  &lt;/TR&gt;END_OF_HEADINGmy $name;# Add additional variables defined by web server or browserforeach $name ( keys %ENV ) {    $env_info{$name} = "an extra variable provided by this server"        unless exists $env_info{$name};}foreach $name ( sort keys %env_info ) {    my $info = $env_info{$name};    my $value = $ENV{$name} || "&lt;I&gt;Not Defined&lt;/I&gt;";    print "&lt;TR&gt;&lt;TD&gt;&lt;B&gt;$name&lt;/B&gt;&lt;/TD&gt;&lt;TD&gt;$info&lt;/TD&gt;&lt;TD&gt;$value&lt;/TD&gt;&lt;/TR&gt;\n";}print "&lt;/TABLE&gt;\n";print "&lt;/BODY&gt;&lt;/HTML&gt;\n";</pre></blockquote></div><a name="ch03-14613" /><div class="figure"><img width="478" src="figs/cgi2.0302.gif" height="457" alt="Figure 3-2" /></div><h4 class="objtitle">Figure 3-2. Output of env.cgi</h4><p>The <tt class="literal">%env_info</tt><a name="INDEX-558" /> hash contains the standard environmentvariable names and their descriptions. The <tt class="function">while</tt>loop iterates over <tt class="literal">%ENV</tt> with the<tt class="function">each</tt> command to add any additional environmentvariables defined by the current web server. Then the<tt class="function">foreach</tt> loop iterates through the combined listand displays the name, description, and value of each environmentvariable. <a href="ch03_02.htm#ch03-87751">Figure 3-3</a> shows what the output willlook in a browser window.</p><a name="ch03-87751" /><div class="figure"><img width="478" src="figs/cgi2.0303.gif" height="457" alt="Figure 3-3" /></div><h4 class="objtitle">Figure 3-3. Output of env_info.cgi</h4><p>This covers most of CGI input, but we have not discussed how to readthe message body for POST requests. We will return to that topic whenwe discuss forms in the next chapter. Right now, let's look<a name="INDEX-560" /><a name="INDEX-561" /><a name="INDEX-562" />at CGIoutput.</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="ch03_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="ch03_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td width="172" valign="top" align="left">3. The Common Gateway Interface</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">3.3. CGI Output</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 + -