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

📄 ch09_04.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>CGI Environment Variables (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch09_03.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch10_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">9.4. CGI Environment Variables</h2><p><a name="INDEX-1636" /><a name="INDEX-1637" />Most of the information needed by CGIprograms is made available via Unix environment variables. Programscan access this information as they would any environment variable(via the <tt class="literal">%ENV</tt> hash in Perl). The table below listsenvironment variables commonly available through CGI. However, sinceservers occasionally vary on the names of environment variables theyassign, check with your own server documentation for moreinformation.</p><a name="ch09-2-fm2xml" /><table border="1" cellpadding="3"><tr><th><p>Environment variable</p></th><th><p>Content returned</p></th></tr><tr><td><p>AUTH_TYPE</p></td><td><p>The authentication method used to validate a user. See REMOTE_IDENTand REMOTE_USER.</p></td></tr><tr><td><p>CONTENT_LENGTH</p></td><td><p>The length of the query data (in bytes or the number ofcharacters)passed to the CGI program through standard input.</p></td></tr><tr><td><p>CONTENT_TYPE</p></td><td><p>The media type of the query data, such as<tt class="literal">text/html</tt>.</p></td></tr><tr><td><p>DOCUMENT_ROOT</p></td><td><p>The directory from which web documents are served.</p></td></tr><tr><td><p>GATEWAY_INTERFACE</p></td><td><p>The revision of the Common Gateway Interface that the server uses.</p></td></tr><tr><td><p>HTTP_ACCEPT</p></td><td><p>A list of the media types that the client can accept.</p></td></tr><tr><td><p>HTTP_COOKIE</p></td><td><p>A list of cookies defined for that URL.</p></td></tr><tr><td><p>HTTP_FROM</p></td><td><p>The email address of the user making the query (many browsers do notsupport this variable).</p></td></tr><tr><td><p>HTTP_REFERER</p></td><td><p>The URL of the document the client read before accessing the CGIprogram.</p></td></tr><tr><td><p>HTTP_USER_AGENT</p></td><td><p>The browser the client is using to issue the request.</p></td></tr><tr><td><p>PATH_INFO</p></td><td><p>Extra path information passed to a CGI program.</p></td></tr><tr><td><p>PATH_TRANSLATED</p></td><td><p>The translated version of the path given by the variable PATH_INFO.</p></td></tr><tr><td><p>QUERY_STRING</p></td><td><p>The query information passed to the program. It is appended to theURL following a question mark (<tt class="literal">?</tt>).</p></td></tr><tr><td><p>REMOTE_ADDR</p></td><td><p>The remote IP address from which the user is making the request.</p></td></tr><tr><td><p>REMOTE_HOST</p></td><td><p>The remote hostname from which the user is making the request.</p></td></tr><tr><td><p>REMOTE_IDENT</p></td><td><p>The user making the request.</p></td></tr><tr><td><p>REMOTE_USER</p></td><td><p>The authenticated name of the user making the query.</p></td></tr><tr><td><p>REQUEST_METHOD</p></td><td><p>The method with which the information request was issued (e.g., GET,POST, HEAD).</p></td></tr><tr><td><p>SCRIPT_NAME</p></td><td><p>The virtual path (e.g., <em class="emphasis">/cgi-bin/program.pl</em>) ofthe script being executed.</p></td></tr><tr><td><p>SERVER_NAME</p></td><td><p>The server's hostname or IP address.</p></td></tr><tr><td><p>SERVER_PORT</p></td><td><p>The port number of the host on which the server is running.</p></td></tr><tr><td><p>SERVER_PROTOCOL</p></td><td><p>The name and revision number of the server protocol.</p></td></tr><tr><td><p>SERVER_SOFTWARE</p></td><td><p>The name and version of the server software that is answering theclient request.</p></td></tr></table><p><p>Here's a simple Perl CGI script that usesenvironment variables to display various information about theserver:</p><blockquote><pre class="code">#!/usr/local/bin/perlprint &lt;&lt; EOFContent-type: text/html&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;About this Server&lt;/TITLE&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;H1&gt;About this Server&lt;/H1&gt;&lt;HR&gt;&lt;PRE&gt;Server Name:        $ENV{'SERVER_NAME'}&lt;BR&gt;Running on Port:    $ENV{'SERVER_PORT'}&lt;BR&gt;Server Software:    $ENV{'SERVER_SOFTWARE'}&lt;BR&gt;Server Protocol:    $ENV{'SERVER_PROTOCOL'}&lt;BR&gt;CGI Revision:       $ENV{'GATEWAY_INTERFACE'}&lt;BR&gt;&lt;HR&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</pre></blockquote><p>The preceding program outputs five environments as an HTML document.In Perl, you can access the environment variables with the<tt class="literal">%ENV</tt> hash. Here's typical outputof the program:</p><a name="INDEX-1638" /><a name="INDEX-1639" /><a name="INDEX-1640" /><a name="INDEX-1641" /><blockquote><pre class="code">&lt;HTML&gt;&lt;HEAD&gt;&lt;TITLE&gt;About this Server&lt;/TITLE&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;H1&gt;About this Server&lt;/H1&gt;&lt;HR&gt;&lt;PRE&gt;Server Name:          www.whatever.comRunning on Port:      80Server Software:      NCSA/1.4.2Server Protocol:      HTTP/1.0CGI Revision:         CGI/1.1&lt;HR&gt;&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</pre></blockquote><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch09_03.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch10_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">9.3. Extra Path Information</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">10. The CGI.pm Module</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><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 + -