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

📄 ch03_02.htm

📁 用perl编写CGI的好书。本书从解释CGI和底层HTTP协议如何工作开始
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<?label 3.2. Environment Variables?><html><head><title>Environment Variables (CGI Programming with Perl)</title><link href="../style/style1.css" type="text/css" rel="stylesheet" /><meta name="DC.Creator" content="Scott Guelich, Gunther Birznieks and Shishir Gundavaram" /><meta scheme="MIME" content="text/xml" name="DC.Format" /><meta content="en-US" name="DC.Language" /><meta content="O'Reilly & Associates, Inc." name="DC.Publisher" /><meta scheme="ISBN" name="DC.Source" content="1565924193L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="CGI Programming with Perl" /><meta content="Text.Monograph" name="DC.Type" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" alt="Book Home" usemap="#banner-map" border="0" /><map name="banner-map"><area alt="CGI Programming with Perl" href="index.htm" coords="0,0,466,65" shape="rect" /><area alt="Search this book" href="jobjects/fsearch.htm" coords="467,0,514,18" shape="rect" /></map><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">CGI Programming with Perl</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></table></div><hr align="left" width="515" /><h2 class="sect1">3.2. Environment Variables</h2><p>CGI scripts are given predefined <a name="INDEX-534" /><a name="INDEX-535" /> <a name="INDEX-536" />environment variables that provideinformation about the web server as well as the client. Much of thisinformation is drawn from the<a name="INDEX-537" />headers of the HTTP request. In Perl,environment variables are available to your script via the globalhash <tt class="literal">%ENV</tt><a name="INDEX-538" />.</p><p>You are free to add, delete, or change any of the values of<tt class="literal">%ENV</tt>. Subprocesses created by your script willalso inherit these environment variables, along with any changesyou've made to them.</p><a name="ch03-5-fm2xml" /><div class="sect2"><h3 class="sect2">3.2.1. CGI Environment Variables</h3><p>The <a name="INDEX-539" /> <a name="INDEX-540" />standard CGI environment variableslisted in <a href="ch03_02.htm#ch03-72429">Table 3-1</a> should be available on anyserver supporting CGI. Nonetheless, if you loop through all the keysin <tt class="literal">%ENV</tt>, you will probably not see all thevariables listed here. If you recall, some HTTP request headers areused only with certain requests. For example, the<em class="emphasis">Content-length</em> header is sent only with POSTrequests. The environment variables that map to these HTTP requestheaders will thus be missing when its corresponding header field ismissing. In other words, <tt class="literal">$ENV{CONTENT_LENGTH}</tt> willonly exist for POST requests.</p><a name="ch03-72429" /><h4 class="objtitle">Table 3-1. Standard CGI Environment Variables </h4><table border="1"><tr><th><p>Environment Variable</p></th><th><p>Description</p></th></tr><tr><td><p>AUTH_TYPE</p></td><td><p>The authentication method used to validate a user. This is blank ifthe request did not require authentication.</p></td></tr><tr><td><p>CONTENT_LENGTH</p></td><td><p>The length of the data (in bytes) passed to the CGI program viastandard input.</p></td></tr><tr><td><p>CONTENT_TYPE</p></td><td><p>The media type of the request body, such as"<em class="emphasis">application/x-www-form-urlencoded</em>".</p></td></tr><tr><td><p>DOCUMENT_ROOT</p></td><td><p>The directory from which static 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>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 from requested URL (i.e., the data following"?").</p></td></tr><tr><td><p>REMOTE_ADDR</p></td><td><p>The remote IP address of the client making the request; this could bethe address of an HTTP proxy between the server and the user.</p></td></tr><tr><td><p>REMOTE_HOST</p></td><td><p>The remote hostname of the client making the request; this could alsobe the name of an HTTP proxy between the server and the user.</p></td></tr><tr><td><p>REMOTE_IDENT</p></td><td><p>The user making the request, as reported by their ident daemon. Onlysome Unix and IRC users are likely to have this running.</p></td></tr><tr><td><p>REMOTE_USER</p></td><td><p>The user's login, authenticated by the web server.</p></td></tr><tr><td><p>REQUEST_METHOD</p></td><td><p>The HTTP request method used for this request.</p></td></tr><tr><td><p>SCRIPT_NAME</p></td><td><p>The URL path (e.g., <em class="filename">/cgi/program.cgi</em>) of thescript 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 listening.</p></td></tr><tr><td><p>SERVER_PROTOCOL</p></td><td><p>The name and revision of the request protocol, e.g.,"HTTP/1.1".</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>Any HTTP <a name="INDEX-541" />headers that the web server doesnot recognize as <a name="INDEX-542" /> <a name="INDEX-543" />standard headers, as well as a fewother common headers, are also available to your script. The webserver follows these rules for creating the name of the environmentvariable:</p><ul><li><p>The field name is capitalized.</p></li><li><p>All dashes are converted to underscores.</p></li><li><p>The prefix <em class="emphasis">HTTP_</em> is added to the name.</p></li></ul><p><a href="ch03_02.htm#ch03-91440">Table 3-2</a> provides a list of some of the morecommon of these environment variables.</p><a name="ch03-91440" /><h4 class="objtitle">Table 3-2. Additional CGI Environment Variables </h4><table border="1"><tr><th><p>Environment Variable</p></th><th><p>Description</p></th></tr><tr><td><p>HTTP_ACCEPT</p></td><td><p>A list of the media types the client can accept.</p></td></tr><tr><td><p>HTTP_ACCEPT_CHARSET</p></td><td><p>A list of the character sets the client can accept.</p></td></tr><tr><td><p>HTTP_ACCEPT_ENCODING</p></td><td><p>A list of the encodings the client can accept.</p></td></tr><tr><td><p>HTTP_ACCEPT_LANGUAGE</p></td><td><p>A list of the languages the client can accept.</p></td></tr><tr><td><p>HTTP_COOKIE</p></td><td><p>A name-value pair previously set by the server.</p></td></tr>

⌨️ 快捷键说明

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