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

📄 ch3.htm

📁 CGI programming is the hottest stuff to look out for in this book
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<P>
<CENTER><B>Table 3.1. Server-specific environment variables.</B></CENTER>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><I>Variable</I></TD><TD WIDTH=404><I>Purpose</I>
</TD></TR>
<TR><TD WIDTH=186><TT><FONT FACE="Courier">GATEWAY_INTERFACE</FONT></TT>
</TD><TD WIDTH=404>CGI version the server complies with.</TD>
</TR>
<TR><TD WIDTH=186>&nbsp;</TD><TD WIDTH=404>Example: CGI/1.1</TD>
</TR>
<TR><TD WIDTH=186><TT><FONT FACE="Courier">SERVER_NAME</FONT></TT>
</TD><TD WIDTH=404>Server's IP address or host name. Example: <TT><FONT FACE="Courier">www.yahoo.com</FONT></TT>
</TD></TR>
<TR><TD WIDTH=186><TT><FONT FACE="Courier">SERVER_PORT</FONT></TT>
</TD><TD WIDTH=404>Port on the server that received the HTTP request. Usually 80 on most servers. 
</TD></TR>
<TR><TD WIDTH=186>&nbsp;</TD><TD WIDTH=404>Example: 80</TD></TR>
<TR><TD WIDTH=186><TT><FONT FACE="Courier">SERVER_PROTOCOL</FONT></TT>
</TD><TD WIDTH=404>Name and version of the protocol being used by the server to process requests.
</TD></TR>
<TR><TD WIDTH=186>&nbsp;</TD><TD WIDTH=404>Example: HTTP/1.0</TD>
</TR>
<TR><TD WIDTH=186><TT><FONT FACE="Courier">SERVER_SOFTWARE</FONT></TT>
</TD><TD WIDTH=404>Name (and normally, version and platform) of the server software.
</TD></TR>
<TR><TD WIDTH=186>&nbsp;</TD><TD WIDTH=404>Example: Purveyor/v1.2 Windows NT
</TD></TR>
</TABLE></CENTER>
<P>
<H3><A NAME="ClientSpecificEnvironmentVariables">Client-Specific
Environment Variables</A></H3>
<P>
Your server knows your CGI program, but it doesn't normally know
the user on the other end or the program that's being used to
contact it with the request. Because the server's going to give
you information about the user and what the user wants, it figures
it might as well give you some information about what the user's
HTML browser, in some cases called the <I>client</I>, is. Some
of the most useful pieces of information that can be obtained
from this data are what the specific program is that the user
has (Is it Netscape? Internet Explorer? Mosaic?) and what page
led them here (Was it a search engine? The front page of your
site?). All of the client environment variables begin with the
prefix <TT><FONT FACE="Courier">HTTP_</FONT></TT> because the
client is responsible for helping with the <I>HyperText Transfer
Protocol (HTTP)</I>, the method Web browsers and servers use to
communicate with one another. Table 3.2 shows a variety of the
more common <TT><FONT FACE="Courier">HTTP_</FONT></TT>environment
variables that you may want to take advantage of.<BR>
<P>
<CENTER><B>Table 3.2. Client-specific (<FONT SIZE=2 FACE="MCPdigital-B">HTTP_</FONT>)
environment variables.</B></CENTER>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><TT><I><FONT FACE="Courier">HTTP_</FONT></I></TT><I> Variable</I>
</TD><TD WIDTH=404><I>Purpose</I></TD></TR>
<TR><TD WIDTH=186><TT><FONT FACE="Courier">AccEPT</FONT></TT>
</TD><TD WIDTH=404>Lists what kind of response schemes are accepted by this request.
</TD></TR>
<TR><TD WIDTH=186><TT><FONT FACE="Courier">AccEPT_EncODING</FONT></TT>
</TD><TD WIDTH=404>Lists what types of encoding schemes are supported by the client.
</TD></TR>
<TR><TD WIDTH=186><TT><FONT FACE="Courier">AccEPT_LANGUAGE</FONT></TT>
</TD><TD WIDTH=404>Identifies the ISO code for the language that the client is looking to receive.
</TD></TR>
<TR><TD WIDTH=186><TT><FONT FACE="Courier">AUTHORIZATION</FONT></TT>
</TD><TD WIDTH=404>Identifies verified users.</TD></TR>
<TR><TD WIDTH=186><TT><FONT FACE="Courier">chARGE_TO</FONT></TT>
</TD><TD WIDTH=404>Sets up automatic billing (for future use).
</TD></TR>
<TR><TD WIDTH=186><TT><FONT FACE="Courier">FROM</FONT></TT></TD>
<TD WIDTH=404>Lists the client's e-mail address.</TD></TR>
<TR><TD WIDTH=186><TT><FONT FACE="Courier">IF_MODIFIED_SIncE</FONT></TT>
</TD><TD WIDTH=404>Accompanies the <TT><FONT FACE="Courier">GET</FONT></TT> request to return data only if the document is newer than the date specified.
</TD></TR>
<TR><TD WIDTH=186><TT><FONT FACE="Courier">PRAGMA</FONT></TT>
</TD><TD WIDTH=404>Sets up server directives or proxies for future use.
</TD></TR>
<TR><TD WIDTH=186><TT><FONT FACE="Courier">REFERER</FONT></TT>
</TD><TD WIDTH=404>Identifies the URL of the document that gave the link to the current document.
</TD></TR>
<TR><TD WIDTH=186><TT><FONT FACE="Courier">USER_AGENT</FONT></TT>
</TD><TD WIDTH=404>Identifies the client software, normally including version information.
</TD></TR>
</TABLE></CENTER>
<P>
<P>
How, and if, you use client-specific headers is up to you. Remember,
though, that there's a reason they're called client-specific:
Not everyone's client is going to fill out all of this information.
Depending on receiving a value from <TT><FONT FACE="Courier">HTTP_FROM</FONT></TT>
isn't going to work more than half the time because very few browsers
currently support the capability. How do you find out for certain
if something's supported or not? If you're really curious, because
you think some of those bits of information look too good to pass
up, you can find the latest information on browsers at <TT><FONT FACE="Courier"><A HREF="http://www.webcompare.com">http://www.webcompare.com</A></FONT></TT>.
This list is updated quite frequently and provides a good, impartial
viewpoint on what's supported and what's not.
<H3><A NAME="RequestSpecificEnvironmentVariables">Request-Specific
Environment Variables</A></H3>
<P>
Every time the server receives a request to do something, it's
different. This keeps life interesting. It also means that there
are a lot of pieces of information that may really matter to your
CGI program that it has to keep track of. These request-specific
environment variables include everything from where the user is
calling from, to how the user has sent the request, to how much
(and what) information they've sent along as part of the request.
This is where the real goldmine of information lies for your program,
so we'll take time out to cover a few of these environment variables
in detail, which you'll find listed in Table 3.3. Several of the
most important request-specific environment variables are on the
list but won't be discussed just yet. Those important variables
deserve very special attention, which we'll give them in the next
section &quot;Taking It All In.&quot;<BR>
<P>
<CENTER><B>Table 3.3. Request-specific environment variables.</B></CENTER>
<CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><I>Variable</I></TD><TD WIDTH=423><I>Purpose</I>
</TD></TR>
<TR><TD WIDTH=167><TT><FONT FACE="Courier">AUTH_TYPE</FONT></TT>
</TD><TD WIDTH=423>Authentication scheme used by the server.</TD>
</TR>
<TR><TD WIDTH=167>&nbsp;</TD><TD WIDTH=423>Example: basic</TD>
</TR>
<TR><TD WIDTH=167><TT><FONT FACE="Courier">CONTENT_FILE</FONT></TT>
</TD><TD WIDTH=423>File that contains data for the CGI program.
</TD></TR>
<TR><TD WIDTH=167>&nbsp;</TD><TD WIDTH=423>Example: <TT><FONT FACE="Courier">c:\windows\mine.dat</FONT></TT>
</TD></TR>
<TR><TD WIDTH=167>&nbsp;</TD><TD WIDTH=423>Note: For WinCGI/Windows HTTPd only.
</TD></TR>
<TR><TD WIDTH=167><TT><FONT FACE="Courier">CONTENT_LENGTH</FONT></TT>
</TD><TD WIDTH=423>Number of bytes sent to Standard Input (<TT><FONT FACE="Courier">STDIN</FONT></TT>) due to a <TT><FONT FACE="Courier">POST</FONT></TT> request.
</TD></TR>
<TR><TD WIDTH=167>&nbsp;</TD><TD WIDTH=423>Example: 342</TD></TR>
<TR><TD WIDTH=167><TT><FONT FACE="Courier">CONTENT_TYPE</FONT></TT>
</TD><TD WIDTH=423>The MIME type of data being sent.</TD></TR>
<TR><TD WIDTH=167>&nbsp;</TD><TD WIDTH=423>Example: <TT><FONT FACE="Courier">text/html</FONT></TT>
</TD></TR>
<TR><TD WIDTH=167><TT><FONT FACE="Courier">OUTPUT_FILE</FONT></TT>
</TD><TD WIDTH=423>File that the CGI program should place data into.
</TD></TR>
<TR><TD WIDTH=167>&nbsp;</TD><TD WIDTH=423>Example: <TT><FONT FACE="Courier">c:\windows\outgoing.dat</FONT></TT>
</TD></TR>
<TR><TD WIDTH=167>&nbsp;</TD><TD WIDTH=423>Note: For WinCGI/Windows HTTPd only.
</TD></TR>
<TR><TD WIDTH=167><TT><FONT FACE="Courier">PATH_INFO</FONT></TT>
</TD><TD WIDTH=423>Additional path information for the CGI program, passed as part of the URL after the program name.
</TD></TR>
<TR><TD WIDTH=167>&nbsp;</TD><TD WIDTH=423>Example: <TT><FONT FACE="Courier">/docs/january</FONT></TT>
</TD></TR>
<TR><TD WIDTH=167><TT><FONT FACE="Courier">PATH_TRANSLATED</FONT></TT>
</TD><TD WIDTH=423>The translated version of <TT><FONT FACE="Courier">PATH_INFO</FONT></TT>, which points to the absolute directory.
</TD></TR>
<TR><TD WIDTH=167>&nbsp;</TD><TD WIDTH=423>Example: <TT><FONT FACE="Courier">/usr/stuff/docs/january</FONT></TT>
</TD></TR>
<TR><TD WIDTH=167><TT><FONT FACE="Courier">QUERY_STRING</FONT></TT>
</TD><TD WIDTH=423>Data passed to the CGI program as part of the URL, consisting of anything after the question mark (<TT><FONT FACE="Courier">?</FONT></TT>). In the example, <TT><FONT FACE="Courier">&quot;name1=value1&quot;</FONT></TT> is the <TT><FONT 
FACE="Courier">QUERY_STRING</FONT></TT>.
</TD></TR>
<TR><TD WIDTH=167>&nbsp;</TD><TD WIDTH=423>Example: <TT><FONT FACE="Courier">http://abc.com/hi.pl?name1=value1</FONT></TT>
</TD></TR>
<TR><TD WIDTH=167><TT><FONT FACE="Courier">REMOTE_ADDR</FONT></TT>
</TD><TD WIDTH=423>The end user's IP address or host name.</TD>
</TR>
<TR><TD WIDTH=167>&nbsp;</TD><TD WIDTH=423>Example: <TT><FONT FACE="Courier">123.231.111.1</FONT></TT>
</TD></TR>
<TR><TD WIDTH=167><TT><FONT FACE="Courier">REMOTE_USER</FONT></TT>
</TD><TD WIDTH=423>User name, if the user has been authenticated.
</TD></TR>
<TR><TD WIDTH=167>&nbsp;</TD><TD WIDTH=423>Example: <TT><FONT FACE="Courier">rocko</FONT></TT>
</TD></TR>
<TR><TD WIDTH=167><TT><FONT FACE="Courier">REQUEST_LINE</FONT></TT>
</TD><TD WIDTH=423>The complete HTTP request line, sent to the server.
</TD></TR>
<TR><TD WIDTH=167>&nbsp;</TD><TD WIDTH=423>Example: <TT><FONT FACE="Courier">GET /scripts/mine.pl?hi HTTP/1.0</FONT></TT>
</TD></TR>
<TR><TD WIDTH=167><TT><FONT FACE="Courier">REQUEST_METHOD</FONT></TT>
</TD><TD WIDTH=423>The method used to pass data as part of the HTTP request.
</TD></TR>
<TR><TD WIDTH=167>&nbsp;</TD><TD WIDTH=423>Example: <TT><FONT FACE="Courier">GET</FONT></TT>
</TD></TR>
<TR><TD WIDTH=167><TT><FONT FACE="Courier">SCRIPT_NAME</FONT></TT>
</TD><TD WIDTH=423>CGI script being run.</TD></TR>
<TR><TD WIDTH=167>&nbsp;</TD><TD WIDTH=423>Example: <TT><FONT FACE="Courier">mine.pl</FONT></TT>
</TD></TR>
</TABLE></CENTER>
<P>
<P>
The three most important and most frequently used environment
variables are
<UL>
<LI><TT><FONT FACE="Courier">REQUEST_METHOD</FONT></TT>
<LI><TT><FONT FACE="Courier">QUERY_STRING</FONT></TT>
<LI><TT><FONT FACE="Courier">CONTENT_LENGTH</FONT></TT>
</UL>
<P>
The reason these are the &quot;big three&quot; you need to familiarize
yourself with is that this combination tells you how data got
to the CGI program; after you know that, all you have to do is
get it. We'll cover these three, as mentioned earlier, in much
more detail in the next section.
<P>
What use are the other environment variables? Plenty. You can
find out if people from your competition are accessing your programs,
you can see if they're registered users, and you can set up links
to your CGI programs so that extra path information gets included
in the request-and you don't have to figure out what directory
they were really looking for.
<P>
<TT><FONT FACE="Courier">CONTENT_FILE</FONT></TT> and <TT><FONT FACE="Courier">OUTPUT_FILE</FONT></TT>
deserve special mention because not everyone uses them. Because
Windows 3.1 (and DOS) don't have too many programming languages
that let you read and write to Standard Input (<TT><FONT FACE="Courier">STDIN</FONT></TT>)
and Standard Output (<TT><FONT FACE="Courier">STDOUT</FONT></TT>),
substitutes were needed.
<H2><A NAME="TakingItAllIn"><FONT SIZE=5 COLOR=#FF0000>Taking
It All In</FONT></A></H2>
<P>
It's good to know that all the information needed by the CGI program
is stored, and knowing the kinds of information stored is even
better. But how does any of it get to the CGI program? Well, let's
find out.
<H3><A NAME="REQUEST_METHOD"><TT><FONT SIZE=4 FACE="Courier">REQUEST_METHOD</FONT></TT></A>
</H3>
<P>
When the user makes a request for the server to execute a CGI
program, additional data the user may want to send along gets
stored by the server. The problem is that it can be stored in

⌨️ 快捷键说明

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