📄 ch10.htm
字号:
require("cgi-lib.pl");
print &Printheader;
print "<HTML>\n";
print "<HEAD><TITLE>Environmental Variables Available to the CGI</TITLE>
</HEAD>\n";
print "<BODY>\n";
print <<"eop";
<CENTER>
<TABLE border=1 cellpadding=12 cellspacing=12>
<TH align=left><H2>Environmental Variable</H2>
<TH align=left><H2>Contains</H2><TR>
eop
foreach $var (sort keys(%ENV)) {
print "<TD> $var <TD> $ENV{var}<TR>";
}
print <<"eop"
</TABLE>
</BODY>
</HTML>
eop
</PRE>
</BLOCKQUOTE>
<P>
This next script also can be used for determining what environmental
variables are on your server. Instead of just displaying them
on your browser, it can be more productive to have a text list
of them. You can do this by e-mailing this list to yourself using
a Perl script.
<P>
There are two environmental variables that are useful to collect
user input; QUERY_STRING and PATH_INFO. To get data into these
variables there are two methods available. Information can be
added directly to an HTML link by the programmer, as with
<BLOCKQUOTE>
<PRE>
<A HREF=http://www.my_server.com/cgi-bin/name.pl?data-request>Click here to read the member's names.</A>
</PRE>
</BLOCKQUOTE>
<P>
where all that follows the question mark is output into the QUERY_STRING
variable. This is true for all data that follows the first question
mark in an URL of an <A HREF> tag. Data can be input into
the PATH_INFO variable in a similar way:
<BLOCKQUOTE>
<PRE>
<A HREF=http://www.my_server.com/cards.pl/bet=1ØØ/cards=5>Click here to start your game with $1ØØ.ØØ</A>
</PRE>
</BLOCKQUOTE>
<P>
CGI will start up the program cards.pl and place everything after
that field into PATH_INFO.
<P>
Both of these variables can be modified by using different methods
inside a <FORM> tag. A form with METHOD=GET will place data
into the QUERY_STRING variable. An example of this might look
like this:
<BLOCKQUOTE>
<PRE>
<FORM METHOD=GET ACTION="http://www.my_server.com/cards.pl">
First Card<INPUT NUMBER = "First Card"><BR>
Second Card<INPUT NUMBER = "Second Card"><BR>
INPUT TYPE=SUBMIT VALUE="Submit"
</FORM>
</PRE>
</BLOCKQUOTE>
<P>
All the information the user inputs into the "First Card"
and "Second Card" prompts will be placed in the QUERY_STRING
variable. To double-check the entries, the CGI may echo back the
data with a script like this:
<BLOCKQUOTE>
<PRE>
#!/usr/bin/perl
# cards.pl
print "Content - type: text/html\n\n";
print "You picked \"$ENV{QUERY_STRING}\" as your cards. Good choice!\n\n";
exit;
</PRE>
</BLOCKQUOTE>
<P>
This script will not print plain converted numbers, because they
will still be encoded with spaces as "+", and so forth.
You might try a regular expression to carefully remove these extra
symbols.
<P>
In a script like this, the users' choices are shown back to them
on their Web browser. Their input has been appended to a new URL:
<BLOCKQUOTE>
<PRE>
http://www.my.machine/cards.pl?First+Card=TenofHearts&Second+Card=AceofSpades
</PRE>
</BLOCKQUOTE>
<P>
where the user had entered "Ten of Hearts" as her first
choice and "Ace of Spades" as her second. The CGI has
taken this input and appended it to a new Web page. Please note
that the input here has been encoded and decoded so that certain
characters, such as spaces, are translated before they proceed
to the gateway script.
<P>
"PATH" is another important environmental variable.
This is the variable that lets your CGI programs know how to find
the other programs and files it may need. When the Perl interpreter
looks for files referenced in a CGI program, it uses the PATH
environmental variable to define where it should search. PATH
is also used by your server's system to find files outside of
the CGI program it is running. Making sure that PATH is properly
defined is very important.
<P>
This is also a good place to check whether you are having problems
with CGI scripts that depend on other files for successful execution.
The different directories available to PATH are separated by a
colon. An example of a defined PATH environmental variable may
look like this:
<BLOCKQUOTE>
<PRE>
PATH=/usr/bin/:/cgi-bin/perl/:/usr/local/public/:/bin:/perl/usr/local:
</PRE>
</BLOCKQUOTE>
<P>
Whatever makes use of PATH starts on the left and looks in the
first directory listed, and then it proceeds down the list. To
speed up operations, list the directories judiciously and in the
order of most use to least. The period at the end of the PATH
values is not to terminate the list, but is a command to also
search the current directory where the CGI program is located.
<P>
The only real problem that exists with environmental variables
is that the gateway program could run extremely long strings through
a shell script that has built-in limitations for string lengths.
You might encounter this as the "running out of environment
space" error. To avoid this you can run your data through
standard input, or <STDIN>.
<H3><A NAME="StandardInputltSTDINgt">
Standard Input <STDIN></A></H3>
<P>
Remembering and using our METHOD=GET means of transferring data
to the gateway, standard input can be used to modify the Perl
script:
<BLOCKQUOTE>
<PRE>
#!/usr/bin/perl
# cards.pl
$user_input = read(STDIN, $_, $ENV{CONTENT_LENGTH});
print "Content - type: text/html\n\n";
print "You picked \"$user_input\" as your cards. Good choice!\n\n";
exit;
</PRE>
</BLOCKQUOTE>
<P>
where the output to the user would be the same as in the previous
example, except that the URL would not display the encoded QUERY_STRING
after the script name, as
<BLOCKQUOTE>
<PRE>
http://www.my_server.com/cards.pl
</PRE>
</BLOCKQUOTE>
<P>
Although the METHOD=GET tag is useful, the METHOD=POST tag is
even more so, because there is no restriction on the amount of
information that it can pass to the gateway program. An example
of using METHOD= POST follows:
<BLOCKQUOTE>
<PRE>
<FORM METHOD=POST ACTION=http://www.my.machine/cards.pl/screen=subscribe>
</PRE>
</BLOCKQUOTE>
<P>
where the user's information will go into both <STDIN> and
the PATH_INFO variable.
<P>
Overall there are several ways to get data to the gateway program,
which creates several software solving strategies to add to your
bag of tricks. To make the most of the CGI, a full understanding
of the set of available environmental variables is valuable. Environmental
variables fall into two distinct categories: server meta-information-where
the variable is independent of the client request and keeps the
identical value regardless of the client's request, and the other-which
is client-specific, and where the value is dependent on the client
request.
<P>
It should be noted that some client-specific environmental variables
can be defined by the server to which the client's request was
sent.
<H4>Server Meta-Information Environmental Variables</H4>
<P>
These environmental variables are set by the server itself, and
do not rely on the CGI to define them. They are always accessible
by the CGI. The list of meta-information environmental variables
is found in Table 10.4.<BR>
<P>
<CENTER><B>Table 10.4 Meta-Information Environmental Variables</B></CENTER>
<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=192><CENTER><B>Environmental Variable</B></CENTER>
</TD><TD WIDTH=384><B>Value</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>SERVER_ADMIN</TD><TD WIDTH=384>The e-mail address of the person responsible for all the Web-related concerns on your server, which is probably yourself.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>SERVER_SOFTWARE</TD><TD WIDTH=384>Identifies the name and version of the Web server. Its output comes in the form name/version.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>SERVER_NAME</TD><TD WIDTH=384>Signifies the server's hostname, DNS alias, or the IP address.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>GATEWAY_INTERFACE</TD><TD WIDTH=384>The server CGI type and the revision level. It is output as CGI/revision.
</TD></TR>
</TABLE></CENTER>
<P>
<H3><A NAME="ClientSpecificEnvironmentalVariables">
Client-Specific Environmental Variables</A></H3>
<P>
These environmental variables are also known as request-header
dependent, because they rely on the requests from the client to
give them a value. The client-specific environmental variables
are listed in Table 10.5.<BR>
<P>
<CENTER><B>Table 10.5 Client-Specific Environmental Variables</B></CENTER><P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR VALIGN=TOP><TD WIDTH=192><CENTER><B>Environmental Variable</B></CENTER>
</TD><TD WIDTH=384><B>Value</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>AUTH_TYPE</TD><TD WIDTH=384>Used to show the protocol-specific authentication method for validating user access. This is used only if the server supports user authentication.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>CONTENT_LENGTH</TD><TD WIDTH=384>The length of the content buffer as announced by the client in its request. It is used by the CGI to know when to cut off the data stream, which it does by reading an input buffer.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>CONTENT_TYPE</TD><TD WIDTH=384>The type of content the client has queried, like HTTP, POST, and PUT.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>HTTP_REFERER/REFERER_URL</TD><TD WIDTH=384>The URL from which the script was invoked.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>HTTP_REQUEST_METHOD</TD><TD WIDTH=384>Simply the HTTP methods request header remade into an environmental variable. The values here can range from the familiar GET and POST methods, to HEAD, PUT, DELETE, LINK, and UNLINK.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>HTTP_USER_AGENT</TD><TD WIDTH=384>Identifies the Web browser that the client uses to send its request. Its output is software/version library/version.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>METHOD=GET (POST) ACTION= http://machine/path/<BR>
programname/extra-path-info
</TD><TD WIDTH=384>This was explained earlier. The supplementary data is put into PATH_INFO.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>PATH_INFO</TD><TD WIDTH=384>Where data from the METHOD=GET(POST) winds up.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>PATH_TRANSLATED</TD><TD WIDTH=384>Where the server takes the virtual path found in PATH_INFO and translates it into a physical path.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>QUERY_STRING</TD><TD WIDTH=384>The client data that follows the ? in an URL that is sourced by this particular script.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>REMOTE_ADDR</TD><TD WIDTH=384>Identifies the IP address of the client.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>REMOTE_HOST</TD><TD WIDTH=384>Where server sets the client's hostname. If this data is not supplied the server should set REMOTE_ADDR instead because this variable holds the same value as REMOTE_ADDR.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>REMOTE_IDENT</TD><TD WIDTH=384>Used for logging. It holds the remote user's name.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>REMOTE_USER</TD><TD WIDTH=384>The user's authenticated user name.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>REQUEST_METHOD</TD><TD WIDTH=384>Where the METHOD=GET(POST) information is housed.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>SCRIPT_FILENAME</TD><TD WIDTH=384>The value of the full path to the CGI script.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>SCRIPT_NAME</TD><TD WIDTH=384>Used to reference the virtual path the executable script takes. Handy for self-referencing URLs like ISINDEX queries.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>SERVER_PORT</TD><TD WIDTH=384>Identifies the port to which the client request was sent.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>SERVER_PROTOCOL</TD><TD WIDTH=384>Takes the protocol that the client is using to make its request and outputs it as protocol/revision.
</TD></TR>
</TABLE></CENTER>
<P>
<H2><A NAME="Conclusion"><FONT SIZE=5 COLOR=#FF0000>
Conclusion</FONT></A></H2>
<P>
Looking into the CGI has lead us to how the CGI handles data from
your Web pages using the HTTP protocol, MIME headers, and Perl
scripts. These early explorations provoke even more questions
about the CGI and how it works, which are presented in Chapter
11.
<HR>
<CENTER><P><A HREF="ch9.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch9.htm"><IMG SRC="PC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/PC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="#CONTENTS"><IMG SRC="CC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/CC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="contents.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/contents.htm"><IMG SRC="HB.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/HB.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>
<A HREF="ch11.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch11.htm"><IMG SRC="NC.GIF" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/NC.GIF" BORDER=0 HEIGHT=88 WIDTH=140></A>
<HR WIDTH="100%"></P></CENTER>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -