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

📄 ch11.htm

📁 美国Macmillan出版社编写的Perl教程《Perl CGI Web Pages for WINNT》
💻 HTM
📖 第 1 页 / 共 4 页
字号:
#! /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 &quot;.pl.&quot;

You could use &quot;.cgi&quot; 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 &quot;#!&quot; 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 &quot;#!&quot; will be valid only in the first line

of the script, then only the &quot;#&quot; 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 &lt;&lt; command, which

tells Perl to print everything that follows the &lt;&lt; command,

that is, eop (which is short for &quot;end of perl&quot;) 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>

&lt;HTML&gt;

&lt;HEAD&gt;

&lt;TITLE&gt;Your Address Please&lt;/TITLE&gt;

&lt;BODY&gt;

&lt;CENTER&gt;&lt;H2&gt;Your Address, Please!&lt;/H2&gt;&lt;/CENTER&gt;

&lt;HR NOSHADE&gt;

&lt;FORM Method=GET Action=&quot;/cgi-bin/nph-address.pl&quot;&gt;

&lt;CENTER&gt;

&lt;TABLE Border=0 width=6&Oslash;%&gt;

&lt;CAPTION Align=top&gt;

&lt;H2&gt;What's Your Address?&lt;/H2&gt;&lt;/CAPTION&gt;

&lt;TH Align=left&gt; First Name

&lt;TH Align=left colsspan=2&gt; Last Name &lt;TR&gt;&lt;TD&gt;

&lt;INPUT type=text sixe=10 maxlength=2&Oslash; name=&quot;first&quot;&gt;

&lt;TD colspan=2&gt;

&lt;INPUT type=text size=32 maxlength=4&Oslash; name=&quot;last&quot;&gt;&lt;TR&gt;

&lt;TH Align=left colspan=3&gt; Street Address &lt;TD&gt;&lt;TD&gt;&lt;TR&gt;

&lt;TD colspan=3&gt;

&lt;INPUT type=text size=61 maxlength=61 name=&quot;street&quot;&gt;&lt;TR&gt;

&lt;TH Align=left&gt; City

&lt;TH Align=left&gt; State

&lt;TH Align=left&gt; Zip Code &lt;TR&gt;

&lt;TD&gt;&lt;INPUT type=text size=2&Oslash; maxlength=3&Oslash; name=&quot;city&quot;&gt;

&lt;TD&gt;&lt;INPUT type=text size=2&Oslash; maxlength=3&Oslash; name=&quot;state&quot;&gt;

&lt;TD&gt;&lt;INPUT type=text size=7 maxlength=1&Oslash; name=&quot;zip&quot;&gt;&lt;TR&gt;

&lt;TH Align=left colspan=3&gt; Telephone Number &lt;TR&gt;

&lt;TD colspan=3&gt;&lt;INPUT type=text size=15 maxlength=15 name=&quot;phone&quot; value=&quot;999.999.9999&quot;&gt; &lt;TR&gt;

&lt;TD width=50%&gt;&lt;INPUT type=&quot;submit&quot; name=&quot;address&quot; value=&quot;Send In Your Address&quot;&gt;

&lt;TD width=50%&gt;&lt;INPUT type=&quot;reset&quot; value=&quot;Reset this Form&quot;&gt;&lt;TR&gt;

&lt;/TABLE&gt;

&lt;/CENTER&gt;

&lt;/FORM&gt;

&lt;/BODY&gt;

&lt;/HTML&gt;

</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&amp;last=Hull&amp;street=1&Oslash;63+Golden+Jet+Lane&amp;city=

Pointe+Anne&amp;state=Ontario&amp;zip=CHI+BLA&amp;phone=61&Oslash;.555.117&Oslash;&amp;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&lt;&lt;&quot;eop&quot;

     HTTP/1.&Oslash; 2&Oslash;4 No Content

     eop

</PRE>

</BLOCKQUOTE>

<P>

The response header specification is given with the &quot;HTTP/1.0

204 No Content&quot; line, where the value &quot;204&quot; 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 &lt;CGI-L@VM.EGE.EDU.TR&gt;, which can be

subscribed to by sending the message &quot;subscribe&quot; 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 + -