📄 ch11.htm
字号:
#! /usr/bin/perl
# submit.pl
print "Content-type: text/html\n\n";
print <<'eop';
<HTML>
<HEAD>
<TITLE>Total Submission</TITLE>
</HEAD>
<BODY>
<H2>Thank-you for submitting!</H2>
We look forward to your future submissions.
</BODY>
</HTML>
eop
</PRE>
</BLOCKQUOTE>
<P>
which looks like Figure 11.4 when it reaches your browser.
<P>
<A HREF="f11-4.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f11-4.gif"><B>Figure 11.4 :</B> <I>A Web page created from a CGI script</I>.</A>
<P>
This is a good time to touch on some of the elements that you
see in submit.pl. The first is the name itself: In Perl, the files
are best named in lowercase, followed by the extension ".pl."
You could use ".cgi" here as well, but the consensus
among CGI programmers seems to be that if you're serious about
your CGI programming, which of course you are, then it is better
to signify what language you are using for your CGI script in
the name of the file.
<P>
The next thing to discuss is the first line of the Perl script
<BLOCKQUOTE>
<PRE>
-- #! /usr/bin/perl
</PRE>
</BLOCKQUOTE>
<P>
This tells the server reading the file that this is a Perl script
and where it can find Perl, so it can deal with this program.
The "#!" is a special use of the two characters in Perl
that are interpreted by the shell of a UNIX system as the executable
for the following script. In NT this is not the case, but the
convention is so deeply ingrained in Perl scripting convention
that you will most likely see all Perl scripts with this opening
line. The "#!" will be valid only in the first line
of the script, then only the "#" symbol is necessary
for marking comment lines in Perl, like the second line-# submit.pl.
This is the name of the file, and it is good programming technique
to always put the name of your file somewhere in here near the
top of the script.
<P>
The next line uses the Perl command print. This is the standard
tool for getting Perl to output data. The data that is output
is the MIME header information, which tells the server to create
the proper header for an HTML text file. The next line uses a
programming trick that makes use of Perl's << command, which
tells Perl to print everything that follows the << command,
that is, eop (which is short for "end of perl") until
it encounters the eop tag again. Perl makes a lot of sense, doesn't
it?
<P>
Before you get too excited, maybe we should try to create an HTML
form that actually takes user data and passes it to the CGI. The
example above might as well have been a static HTML page for all
the trouble it took.
<H3><A NAME="FormsandCGI">
Forms and CGI</A></H3>
<P>
One of the most common needs of a Web site is to have a form that
gathers mailing list information. If you wanted to, you could
create a form with a single textbox to get this information, or
even easier, put in an e-mail tag and a request for the user to
e-mail you his or her address. Although these are the easier ways
to gather the data, it limits you on your options on the back
end. It would be nice if this information could be sent into a
database where a mailing list can be created based on city, or
zip code. To do this, each piece of information needed must be
input in its own field. An HTML form that asks for this might
look like the following:
<BLOCKQUOTE>
<PRE>
<HTML>
<HEAD>
<TITLE>Your Address Please</TITLE>
<BODY>
<CENTER><H2>Your Address, Please!</H2></CENTER>
<HR NOSHADE>
<FORM Method=GET Action="/cgi-bin/nph-address.pl">
<CENTER>
<TABLE Border=0 width=6Ø%>
<CAPTION Align=top>
<H2>What's Your Address?</H2></CAPTION>
<TH Align=left> First Name
<TH Align=left colsspan=2> Last Name <TR><TD>
<INPUT type=text sixe=10 maxlength=2Ø name="first">
<TD colspan=2>
<INPUT type=text size=32 maxlength=4Ø name="last"><TR>
<TH Align=left colspan=3> Street Address <TD><TD><TR>
<TD colspan=3>
<INPUT type=text size=61 maxlength=61 name="street"><TR>
<TH Align=left> City
<TH Align=left> State
<TH Align=left> Zip Code <TR>
<TD><INPUT type=text size=2Ø maxlength=3Ø name="city">
<TD><INPUT type=text size=2Ø maxlength=3Ø name="state">
<TD><INPUT type=text size=7 maxlength=1Ø name="zip"><TR>
<TH Align=left colspan=3> Telephone Number <TR>
<TD colspan=3><INPUT type=text size=15 maxlength=15 name="phone" value="999.999.9999"> <TR>
<TD width=50%><INPUT type="submit" name="address" value="Send In Your Address">
<TD width=50%><INPUT type="reset" value="Reset this Form"><TR>
</TABLE>
</CENTER>
</FORM>
</BODY>
</HTML>
</PRE>
</BLOCKQUOTE>
<P>
This gives you a page like that shown in Figure 11.5.
<P>
<A HREF="f11-5.gif" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/f11-5.gif"><B>Figure 11.5 :</B> <I>An address form to gather user data for
the CGI</I>.</A>
<P>
All of the data from the form is URL encoded into name/value pairs
and attached to the end of the URL of that page, as is regular
procedure using the GET method. At the server end, this data is
put into QUERY_STRING, the environmental variable that handles
this kind of data. This data is also referred to as the query
string. A sample query string from this form might look like this:
<BLOCKQUOTE>
<PRE>
QUERY_STRING first=Bobby&last=Hull&street=1Ø63+Golden+Jet+Lane&city=
Pointe+Anne&state=Ontario&zip=CHI+BLA&phone=61Ø.555.117Ø&address=Send+In+Your+Address+
</PRE>
</BLOCKQUOTE>
<P>
This query string would appear right after the ? in the URL of
the form's action argument-/cgi-bin/nph-address.pl.
<H3><A NAME="NonParsedHeaders">
Non-Parsed Headers</A></H3>
<P>
As has been discussed previously, memory management is an ongoing
concern of any Web server administrator. Any opportunity to reduce
the work your Web server has to do to fulfill client requests
should be taken. With that in mind, non-parsed header (NPH) CGI
scripts can help lessen your server's work load.
<P>
NPH scripts are used to create headers that are not parsed by
the server, as their name would indicate. Remember the address
request HTML form? There was a program call through the CGI for
nph-address.pl.
<P>
When data is passed to the CGI, it creates a header that tells
the server the context of the data, and then sends the requested
data itself.
<P>
Next the server has to create a response header to send to the
browser. With this NPH, you can skip the stage between the CGI
and the server, because it designates what the client is to do
with the data within the data itself. Cleaning up the long string
that is sent back to the user attached to the URL (making it a
very ugly, long string) is a way to add some class to your Web
page and using an NPH script that could do that might look like
this:
<BLOCKQUOTE>
<PRE>
#!/usr/bin/perl
# nph-address.pl
print<<"eop"
HTTP/1.Ø 2Ø4 No Content
eop
</PRE>
</BLOCKQUOTE>
<P>
The response header specification is given with the "HTTP/1.0
204 No Content" line, where the value "204" informs
the browser that there isn't any data to load with this response
header. Sometimes, a confirmation HTML page is sent to the user,
but this small script will accomplish the same thing and save
time.
<P>
When the script is run, the browser is informed to let the current
HTML document stay displayed. It is important when working with
NPH that you use the nph- prefix in the file names, and no other
variations, because these will only cause you CGI grief.
<H2><A NAME="CGISummary"><FONT SIZE=5 COLOR=#FF0000>
CGI Summary</FONT></A></H2>
<P>
When dealing with the CGI, Perl is one of the premier languages
to run data to and from the Web pages on your server. The client/server
dynamic is moderated by MIME specifications that inform both client
and server what kind of data is being passed between them. Both
SSIs and NPH are features that can enhance the CGI, each with
different results.
<P>
There is much more to the CGI than is covered in this book. For
more information on the CGI, there is the e-mail mailing list
<BLOCKQUOTE>
<PRE>
CGI-L Common Gateway Interface list <CGI-L@VM.EGE.EDU.TR>, which can be
subscribed to by sending the message "subscribe" to listserv@VM.EGE.EDU.TR.
This list deals with the many issues involved with the CGI that are not
covered in this book. More information can be found at this URL: <A HREF="javascript:if(confirm('http://www.yahoo.com/Computers_and_Internet/Internet/World_Wide_Web/CGI_Common_Gateway_Interface/ \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.yahoo.com/Computers_and_Internet/Internet/World_Wide_Web/CGI_Common_Gateway_Interface/'" tppabs="http://www.yahoo.com/Computers_and_Internet/Internet/World_Wide_Web/CGI_Common_Gateway_Interface/">http://
www.yahoo.com/Computers_and_Internet/Internet/World_Wide_Web/CGI_Common_Gateway_Interface/
</A></PRE>
</BLOCKQUOTE>
<P>
or the NCSA site
<BLOCKQUOTE>
<PRE>
<A HREF="javascript:if(confirm('http://hoohoo.ncsa.uiuc.edu/cgi/ \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://hoohoo.ncsa.uiuc.edu/cgi/'" tppabs="http://hoohoo.ncsa.uiuc.edu/cgi/">http://hoohoo.ncsa.uiuc.edu/cgi/
</A></PRE>
</BLOCKQUOTE>
<P>
as well as the CGI library at
<BLOCKQUOTE>
<PRE>
<A HREF="javascript:if(confirm('http://www.bio.cam.ac.uk/cgi-lib/ \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.bio.cam.ac.uk/cgi-lib/'" tppabs="http://www.bio.cam.ac.uk/cgi-lib/">http://www.bio.cam.ac.uk/cgi-lib/
</A></PRE>
</BLOCKQUOTE>
<P>
which has many Perl scripts to work the CGI. There is also this
site
<BLOCKQUOTE>
<PRE>
<A HREF="javascript:if(confirm('http://www.city.net/win-httpd/httpddoc/wincgi.htm \n\nThis file was not retrieved by Teleport Pro, because it is addressed on a domain or path outside the boundaries set for its Starting Address. \n\nDo you want to open it from the server?'))window.location='http://www.city.net/win-httpd/httpddoc/wincgi.htm'" tppabs="http://www.city.net/win-httpd/httpddoc/wincgi.htm">http://www.city.net/win-httpd/httpddoc/wincgi.htm
</A></PRE>
</BLOCKQUOTE>
<P>
which specializes in Windows CGI concerns. Learning more about
how your server uses the client/server model and how each element
of the CGI is regulated on your server will help you to write
better CGI scripts, as well as providing better access, and service,
to the users of your Web sites.
<HR>
<CENTER><P><A HREF="ch10.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch10.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="ch12.htm" tppabs="http://210.32.137.15/ebook/PC%20Magazine%20Programming%20Perl%205.0%20CGI%20Web%20Pages%20for%20Microsoft%20Windows%20NT/ch12.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 + -