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

📄 ch22.htm

📁 《Perl 5 Unreleased》
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HTML>



<HEAD>

   <TITLE>Chapter 22 -- Using HTML FORMs

with Perl CGI Scripts</TITLE>

   <META NAME="GENERATOR" CONTENT="Mozilla/3.0b5aGold (WinNT; I) [Netscape]">

</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">

<H1><FONT COLOR=#FF0000>Chapter 22</FONT></H1>

<H1><B><FONT SIZE=5 COLOR=#FF0000>Using HTML </FONT></B><TT><B><FONT FACE="Courier" COLOR=#FF0000>FORMs</FONT></B></TT><B><FONT SIZE=5 COLOR=#FF0000> with Perl CGI Scripts</FONT></B></FONT></H1>

<P>

<HR WIDTH="100%"></P>

<P>

<H3 ALIGN=CENTER><FONT COLOR="#000000"><FONT SIZE=+2>CONTENTS<A NAME="CONTENTS"></A>

</FONT></FONT></H3>



<UL>

<LI><A HREF="#InputandOutputwithCGI" >Input and Output with CGI</A>

<LI><A HREF="#WhatAreGETandPOST" >What Are GET and POST?</A>

<LI><A HREF="#HandlingHTMLFORMswithGETMethods" >Handling HTML FORMs with GET Methods</A>

<LI><A HREF="#HandlinganHTMLFORMwithPOSTMethods" >Handling an HTML FORM with POST Methods</A>

<LI><A HREF="#ReturningHTMLPages" >Returning HTML Pages</A>

<LI><A HREF="#UsingtheCollectedData" >Using the Collected Data</A>

<UL>

<LI><A HREF="#ArchivingUserResponses" >Archiving User Responses</A>

<LI><A HREF="#ForwardingUserResponses" >Forwarding User Responses</A>

</UL>

<LI><A HREF="#Summary" >Summary</A>

</UL>

<HR>

<P>

This chapter covers the use of Perl with HTML forms. The topics

include collecting information from an HTML <TT><FONT FACE="Courier">FORM</FONT></TT>

and responding to the requested information. I cover two ways

of querying information from an HTML script: using the <TT><FONT FACE="Courier">GET</FONT></TT>

and <TT><FONT FACE="Courier">POST</FONT></TT> methods. I also

cover how to acquire and then parse data in the Common Gateway

Interface (CGI) script in order to get responses back to the browser.

The information presented in this chapter can easily be expanded

to cover a whole book. There are many different ways of handling

CGI scripts, <TT><FONT FACE="Courier">FORM</FONT></TT>s, and developing

client/server applications, and just as many texts to cover them.

A list of references is provided here if you want more information:

<P>

For more information via printed textbooks, you might want to

consult these titles:

<UL>

<LI><I>Teach Yourself Web Publishing with HTML 3.0 in a Week</I>,

Laura Lemay, Sams.net, 1-57521-064-9, 1996.

<LI><I>HTML &amp; CGI Unleashed</I>, John December and Mark Ginsberg,

Sams.net, 0-672-30745-6, 1995. 

<LI><I>Using HTML</I>, Neil Randall, Que, 0-7897-0622-9, 1995.

</UL>

<H2><A NAME="InputandOutputwithCGI"><B><FONT SIZE=5 COLOR=#FF0000>Input

and Output with CGI</FONT></B></A></H2>

<P>

If you have used a Web browser, then you have come across HTML

pages, which allow you to query databases for information. Click

a button and-voil&agrave;-you get the latest weather in Colorado.

Just enter a date and destination and you can click a button to

get the travel information you need. What's going on behind the

page? Well, the chances are very high that the information handler

behind the Web page is a Perl script. Perl's power and ease of

handling make it a good choice for setting up support code for

Web pages.

<P>

Before I begin, remember that a CGI script does not have to be

written in Perl, but the ease and convenience of handling strings

makes Perl a very comfortable choice. Because this book is about

Perl, it won't take a wild guess to figure out which language

I cover in this chapter. However, you certainly can write CGI

scripts in any language you like-tcl/Tk, C, C++, or (gasp) even

in Assembler.

<P>

I'll go over a few points about the terminology in this chapter

before I get into the code. An HTML page is picked up and displayed

by a browser on the request of a user using that browser. The

information handling scripts and executables for that page are

handled by the server to which the HTML page's Uniform Resource

Locator (URL) points. The server gets a request for action from

the browser when the user selects the URL. The request is processed

by the server using the CGI, and the results of the CGI executable

are sent back to the browser, which in turn displays them to the

user. When describing the code that handles the requests, it's

easy to use the word <I>user</I> instead of <I>browser</I>. However,

as far as the CGI script on the server is concerned, it's sending

results back to whoever or whatever called it. It's easy to get

the words mixed up, but the intent of both words is to imply the

entity that invoked the CGI script in the first place.

<P>

I introduced you briefly to CGI in <A HREF="ch20.htm" tppabs="http://www.mcp.com/815097600/0-672/0-672-30891-6/ch20.htm" >Chapter 20</A>,

&quot;An Introduction to Web Pages and CGI.&quot; In this chapter,

I cover how the methods for CGI are implemented in HTML forms.

I use the <TT><FONT FACE="Courier">test-cgi.pl</FONT></TT> shell

script (presented earlier) as the basis for setting up shell scripts

for returning data in response to a request. Listing 22.1 presents

a Perl script to echo CGI environment variables. 

<HR>

<BLOCKQUOTE>

<B>Listing 22.1. Perl script to echo CGI environment variables.

<BR>

</B>

</BLOCKQUOTE>

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&nbsp;1 #!/usr/bin/perl<BR>

&nbsp;2 #<BR>

&nbsp;3 # The sample script file to echo back ENV<BR>

&nbsp;4 # variables on call from an HTML document.<BR>

&nbsp;5 #<BR>

&nbsp;6 $|=1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#

Flush output immediately.<BR>

&nbsp;7 print &quot;Content-Type: text/plain\r\n&quot;;<BR>

&nbsp;8 print &quot;Yet Another CGI/1.0 Test Script\r\n&quot;;

<BR>

&nbsp;9 <BR>

10&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$count = ($#ARGV + 1);<BR>

11&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print &quot;Argument Count: $count&quot;;

<BR>

12&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;foreach $word (@ARGV) {<BR>

13&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print

&quot;\n $word&quot;;<BR>

14 <BR>

15 print &quot;\n&quot;;<BR>

16 #<BR>

17 print &quot;SERVER_SOFTWARE = $ENV{'SERVER_SOFTWARE'}\n&quot;;

<BR>

18 print &quot;SERVER_NAME = $ENV{'SERVER_NAME'}\n&quot;;<BR>

19 print &quot;GATEWAY_INTERFACE = $ENV{'GATEWAY_INTERFACE'}\n&quot;;

<BR>

20 print &quot;SERVER_PROTOCOL = $ENV{'SERVER_PROTOCOL'}\n&quot;;

<BR>

21 print &quot;SERVER_PORT = $ENV{'SERVER_PORT'}\n&quot;;<BR>

22 print &quot;SERVER_ROOT = $ENV{'SERVER_ROOT'}\n&quot;;<BR>

23 print &quot;REQUEST_METHOD = $ENV{'REQUEST_METHOD'}\n&quot;;

<BR>

24 print &quot;HTTP_AccEPT = $ENV{'HTTP_AccEPT'}\n&quot;;<BR>

25 print &quot;PATH_INFO = $ENV{'PATH_INFO'}\n&quot;;<BR>

26 print &quot;PATH = $ENV{'PATH'}\n&quot;;<BR>

27 print &quot;PATH_TRANSLATED = $ENV{'PATH_TRANSLATED'}\n&quot;;

<BR>

28 print &quot;SCRIPT_NAME = $ENV{'SCRIPT_NAME'}\n&quot;;<BR>

29 print &quot;QUERY_STRING = $ENV{'QUERY_STRING'}\n&quot;;<BR>

30 print &quot;QUERY_STRING_UNESCAPED = $ENV{'QUERY_STRING_UNESCAPED'}\n&quot;;

<BR>

31 print &quot;REMOTE_HOST = $ENV{'REMOTE_HOST'}\n&quot;;<BR>

32 print &quot;REMOTE_IDENT = $ENV{'REMOTE_IDENT'}\n&quot;;<BR>

33 print &quot;REMOTE_ADDR = $ENV{'REMOTE_ADDR'}\n&quot;;<BR>

34 print &quot;REMOTE_USER = $ENV{'REMOTE_USER'}\n&quot;;<BR>

35 print &quot;AUTH_TYPE = $ENV{'AUTH_TYPE'}\n&quot;;<BR>

36 print &quot;CONTENT_TYPE = $ENV{'CONTENT_TYPE'}\n&quot;;<BR>

37 print &quot;CONTENT_LENGTH = $ENV{'CONTENT_LENGTH'}\n&quot;;

<BR>

38 print &quot;DOCUMENT_ROOT = $ENV{'DOCUMENT_ROOT'}\n&quot;;

<BR>

39 print &quot;DOCUMENT_URI = $ENV{'DOCUMENT_URI'}\n&quot;;<BR>

40 print &quot;DOCUMENT_NAME = $ENV{'DOCUMENT_NAME'}\n&quot;;

<BR>

41 print &quot;DATE_LOCAL = $ENV{'DATE_LOCAL'}\n&quot;;<BR>

42 print &quot;DATE_GMT = $ENV{'DATE_GMT'}\n&quot;;<BR>

43 print &quot;LAST_MODIFIED = $ENV{'LAST_MODIFIED'}\n&quot;;</FONT></TT>

</BLOCKQUOTE>

<HR>

<P>

I'll examine only the Perl scripting features that apply to CGI.

Basically, CGI scripts are executed by the server in response

to a request or action by the URL referenced in the HTML document

being viewed. For example, a URL refers to this document as follows:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">&lt;A<BR>

HREF=&quot;http://ikra.com/cgi-bin/test-cgi?Its+de+a+vu+all+over+again&quot;

&gt;<BR>

Click me for an echo.<BR>

&lt;/A&gt;</FONT></TT>

</BLOCKQUOTE>

<P>

The output from this script is as follows. I have truncated it

to save space.

<BLOCKQUOTE>

<TT><FONT FACE="Courier">Yet Another CGI/1.0 Test Script<BR>

<BR>

Argument Count: 6<BR>

&nbsp;Its<BR>

&nbsp;deja<BR>

&nbsp;vu<BR>

&nbsp;all<BR>

&nbsp;over<BR>

&nbsp;again<BR>

SERVER_SOFTWARE = ncSA/1.4.2<BR>

SERVER_NAME = pop.ikra.com<BR>

GATEWAY_INTERFACE = CGI/1.1<BR>

SERVER_PROTOCOL = HTTP/1.0<BR>

SERVER_PORT = 80<BR>

SERVER_ROOT =<BR>

REQUEST_METHOD = GET<BR>

HTTP_AccEPT = */*, image/gif, image/x-xbitmap, image/jpeg<BR>

PATH_INFO =<BR>

PATH = /bin:/usr/bin:/usr/ucb:/usr/bsd:/usr/local/bin<BR>

PATH_TRANSLATED =<BR>

SCRIPT_NAME = /cgi-bin/test-cgi<BR>

QUERY_STRING = Its+deja+vu+all+over+again<BR>

QUERY_STRING_UNESCAPED =<BR>

REMOTE_HOST = pop.ikra.com<BR>

REMOTE_IDENT =</FONT></TT>

</BLOCKQUOTE>

<P>

The first action is to reply to the server that text is being

sent back. This is done with the following statement:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">print &quot;Content-Type: text/plain\n\n&quot;;</FONT></TT>

</BLOCKQUOTE>

<P>

Examine this <TT><FONT FACE="Courier">test-cgi.pl</FONT></TT>

Perl script and its associated URL in more detail. Notice how

the arguments are being passed into the Perl script. Okay, so

I said <I>Its</I> instead of <I>It's</I>, because I did not want

to escape the single quote (') between the <I>t</I> and <I>s</I>.

<BLOCKQUOTE>

<TT>HREF=&quot;http://ikra.com/cgi-bin/test-cgi?Its+de+a+vu+all+over+again&quot;</FONT></TT>

</BLOCKQUOTE>

<P>

The script being referred to in this URL is the <TT><FONT FACE="Courier">test-cgi</FONT></TT>

file on the node <TT><FONT FACE="Courier">ikra.com</FONT></TT>

in the subdirectory <TT><FONT FACE="Courier">cgi-bin</FONT></TT>

of the <TT><FONT FACE="Courier">http</FONT></TT> root directory.

The arguments being passed into this script appear after the question

mark (<TT><FONT FACE="Courier">?</FONT></TT>). Each argument is

separated by a plus sign (<TT><FONT FACE="Courier">+</FONT></TT>).

<P>

The number of arguments, therefore, is six. The string is the

now famous saying that is widely attributed to Yogi Berra, &quot;It's

d&eacute;j&agrave; vu all over again.&quot; Now let's see how

the shell script handles this quip.

<P>

The first line to look at is the one in which <TT><FONT FACE="Courier">$|</FONT></TT>

is set to <TT><FONT FACE="Courier">1</FONT></TT>. The <TT><FONT FACE="Courier">$|</FONT></TT>

variable is a special variable in Perl. When the <TT><FONT FACE="Courier">$|</FONT></TT>

variable is set to a non-zero value, Perl forces a flush to the

current output channel. When you are working with CGI applications,

it's important to keep in mind that a quick response will win

you praise. Don't wait for the channel to flush input back to

the caller because the buffering on your output might cause the

client's browser to wait for input for so long that a timeout

is triggered.

<P>

The next line is absolutely necessary and should be printed back

to the browser regardless of how the shell script runs. This line

tells the client what type of data you are sending back. In this

example, plain text is sent back; it's important to let the browser

know about it. This is done by sending back the MIME content identifier:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">print &quot;Content-Type: text/plain\n\n&quot;;</FONT></TT>

</BLOCKQUOTE>

<P>

It's nice to know what the returned output is; you can print it

out with this line:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">print &quot;Yet Another CGI/1.0 Test

Script\n\n&quot;;</FONT></TT>

</BLOCKQUOTE>

<P>

Next, all the arguments are printed out back to the browser with

the following lines:

<BLOCKQUOTE>

<TT><FONT FACE="Courier">$count = ($#ARGV + 1);<BR>

print &quot;Argument Count: $count&quot;;<BR>

foreach $word (@ARGV) {<BR>

&nbsp;&nbsp;&nbsp;&nbsp;print &quot;\n $word&quot;;<BR>

}</FONT></TT>

</BLOCKQUOTE>

<P>

The environment variable <TT><FONT FACE="Courier">QUERY_STRING</FONT></TT>

has the arguments to this shell script in the form of <TT><FONT FACE="Courier">Its+deja+vu+all+over+again</FONT></TT>.

In order to parse this string into individual arguments, you have

to split the array where there is a plus sign. This is easily

done with the following line (which is not in Listing 22.1):

<BLOCKQUOTE>

⌨️ 快捷键说明

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