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

📄 ch19.htm

📁 prrl 5 programs codes in the book
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HTML>

<HEAD>

<TITLE>Chapter 19  -- What Is CGI?</TITLE>



<META>

</HEAD>

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

<H1><FONT SIZE=6 COLOR=#FF0000>Chapter&nbsp;19</FONT></H1>

<H1><FONT SIZE=6 COLOR=#FF0000>What Is CGI?</FONT></H1>

<HR>

<P>

<CENTER><B><FONT SIZE=5>CONTENTS</FONT></B></CENTER>

<UL>

<LI><A HREF="#WhyUsePerlforCGI">

Why Use Perl for CGI?</A>

<LI><A HREF="#CGIAppsversusJavaApplets">

CGI Apps versus Java Applets</A>

<LI><A HREF="#ShouldYouUseCGIModules">

Should You Use CGI Modules?</A>

<LI><A HREF="#HowDoesCGIWork">

How Does CGI Work?</A>

<LI><A HREF="#CallingYourCGIProgram">

Calling Your CGI Program</A>

<LI><A HREF="#YourFirstCGIProgram">

Your First CGI Program</A>

<LI><A HREF="#WhyAreFilePermissionsImportantinUNIX">

Why Are File Permissions Important in UNIX?</A>

<LI><A HREF="#HTTPHeaders">

HTTP Headers</A>

<LI><A HREF="#CGIandEnvironmentVariables">

CGI and Environment Variables</A>

<LI><A HREF="#URLENCoding">

URL ENCoding</A>

<LI><A HREF="#Security">

Security</A>

<LI><A HREF="#CGIwrapandSecurity">

CGIwrap and Security</A>

<LI><A HREF="#Cookies">

Cookies</A>

<UL>

<LI><A HREF="#CookieSecurity">

Cookie Security</A>

<LI><A HREF="#HowAreCookiesCreatedandRead">

How Are Cookies Created and Read?</A>

<LI><A HREF="#CanaVisitorsBrowserSupportCookies">

Can a Visitor's Browser Support Cookies?</A>

</UL>

<LI><A HREF="#DebuggingCGIPrograms">

Debugging CGI Programs</A>

<UL>

<LI><A HREF="#SendingOutputtotheServersLogFile">

Sending Output to the Server's Log File</A>

<LI><A HREF="#SendingSTDERRtotheWebBrowser">

Sending STDERR to the Web Browser</A>

<LI><A HREF="#CGITap">

CGITap</A>

<LI><A HREF="#GeneratinganErrorHTMLPage">

Generating an Error HTML Page</A>

</UL>

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

Summary</A>

<LI><A HREF="#ReviewQuestions">

Review Questions</A>

<LI><A HREF="#ReviewExercises">

Review Exercises</A>

</UL>



<HR>

<P>

CGI, or <I>Common Gateway Interface</I>, is the standard programming

interface between Web servers and external programs. It is one

of the most exciting and fun areas of programming today. The CGI

standard lets Web browsers pass information to programs written

in any language. If you want to create a lightning-fast search

engine, then your CGI program will most likely be written in C

or C++. However, most other applications can use Perl.

<P>

The CGI standard does not exist in isolation; it is dependent

on the HTML and HTTP standards. HTML is the standard that lets

Web browsers understand document content. HTTP is the communications

protocol that, among other things, lets Web servers talk with

Web browsers.<BR>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD><B>Note</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

If you are unfamiliar with HTML, you might want to skip to the HTML introduction in <A HREF="ch20.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch20.htm" >Chapter 20</A>, &quot;Form Processing,&quot; before continuing. Otherwise, take the HTML refereNCes in this chapter at face value.</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<P>

Almost anyone can throw together some HTML and hang a &quot;home

page&quot; out on the Web. But most sites out there are, quite

frankly, boring. Why? The fact is that most sites are built as

a simple series of HTML documents that never change. The site

is completely static. No one is likely to visit a static page

more than oNCe or twice. Think about the sites you visit most

often. They probably have some interesting content, certainly,

but more importantly, they have dynamic content.

<P>

So what's a Webmaster to do? No Webmaster has the time to update

his or her Web site by hand every day. Fortunately, the people

who developed the Web protocol thought of this problem and gave

us CGI. CGI gives you a way to make Web sites dynamic and interactive.

<P>

Each word in the acronym Common Gateway Interface helps you to

understand the interface:

<UL>

<LI><B>Common</B>-interacts with many different operating systems.

<LI><B>Gateway</B>-provides users with a way to gain access to

different programs, such as databases or picture generators.

<LI><B>Interface</B>-uses a well-defined method to interact with

a Web server.

</UL>

<P>

CGI applications can perform nearly any task that your imagination

can think up. For example, you can create Web pages on-the-fly,

access databases, hold telnet sessions, generate graphics, and

compile statistics.

<P>

The basic coNCept behind CGI is pretty simple; however, actually

creating CGI applications is not. That requires real programming

skills. You need to be able to debug programs and make logical

connections between one idea and another. You also need to have

the ability to visualize the application that you'd like to create.

This chapter and the next, &quot;Form Processing,&quot; will get

you started with CGI programming. If you plan to create large

applications, you might want to look at Que's <I>Special Edition

Using CGI</I>.

<H2><A NAME="WhyUsePerlforCGI"><FONT SIZE=5 COLOR=#FF0000>

Why Use Perl for CGI?</FONT></A></H2>

<P>

Perl is the de facto standard for CGI programming for a number

of reasons, but perhaps the most important are:

<UL>

<LI><B>Socket Support</B>-create programs that interface seamlessly

with Internet protocols. Your CGI program can send a Web page

in response to a transaction and send a series of e-mail messages

to inform interested people that the transaction happened.

<LI><B>Pattern Matching</B>-ideal for handling form data and searching

text.

<LI><B>Flexible Text Handling</B>-no details to worry. The way

that Perl handles strings, in terms of memory allocation and deallocation,

fades into the background as you program. You simply can ignore

the details of coNCatenating, copying, and creating new strings.

</UL>

<P>

The advantage of an interpreted language in CGI applications is

its simplicity in development, debugging, and revision. By removing

the compilation step, you and I can move more quickly from task

to task, without the frustration that can sometimes arise from

debugging compiled programs. Of course, not any interpreted language

will do. Perl has the distiNCt advantage of having an extremely

rich and capable fuNCtionality.

<P>

There are some times when a mature CGI application should be ported

to C or another compiled language. These are the Web applications

where speed is important. If you expect to have a very active

site, you probably want to move to a compiled language because

they run faster.

<H2><A NAME="CGIAppsversusJavaApplets"><FONT SIZE=5 COLOR=#FF0000>

CGI Apps versus Java Applets</FONT></A></H2>

<P>

CGI and Java are two totally different animals. CGI is a specification

that can be used by any programming language. CGI applications

are run on a Web server. Java is a programming language that is

run on the client side.

<P>

CGI applications should be designed to take advantage of the centralized

nature of a Web server. They are great for searching databases,

processing HTML form data, and other applications that require

limited interaction with a user.

<P>

Java applications are good when you need a high degree of interaction

with users: for example, games or animation.

<P>

Java programs need to be kept relatively small because they are

transmitted through the Internet to the client. CGI applications,

on the other hand, can be as large as needed because they reside

and are executed on the Web server.

<P>

You can design your Web site to use both Java and CGI applications.

For example, you might want to use Java on the client side to

do field validation when collecting information on a form. Then

oNCe the input has been validated, the Java application can send

the information to a CGI application on the Web server where the

database resides.

<H2><A NAME="ShouldYouUseCGIModules"><FONT SIZE=5 COLOR=#FF0000>

Should You Use CGI Modules?</FONT></A></H2>

<P>

I eNCourage you to use the CGI modules that are available on the

Internet. The most up-to-date module that I know about is called

<TT>cgi.pm</TT>-but you must be using

Perl v5.002 or an even newer version in order to use it. <TT>cgi.pm</TT>

is very comprehensive and covers many different protocols in addition

to the basic CGI standard.

<P>

You might find that <TT>cgi.pm</TT>

is overkill for simple CGI applications. If so, look at <TT>cgi-lite.pl</TT>.

This library doesn't do as much as <TT>cgi.pm</TT>

but you'll probably find that it is easier to use.

<P>

You can find both of these scripts at one of the CPAN Web sites

that are mentioned in <A HREF="ch22.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch22.htm" >Chapter 22</A>, &quot;Internet Resources.&quot;

<P>

However, in this book, I have purposely not used these scripts.

I feel it is important for you to understand the mechanisms behind

the protocols. This will make debugging your applications easier

because you'll have a better idea what the modules are doing behind

the scenes. You will also be able to make better use of pre-existing

modules if you can make educated guesses about what a poorly documented

fuNCtion does.

<H2><A NAME="HowDoesCGIWork"><FONT SIZE=5 COLOR=#FF0000>

How Does CGI Work?</FONT></A></H2>

<P>

CGI programs are always placed on a disk that the Web server has

access to. This means that if you are using a dial-up account

to maintain your Web site, you need to upload your CGI programs

to the server before they can be run.<BR>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD><B>Tip</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

You can test your scripts locally as long as you can use Perl on your local machine. See the &quot;Debugging&quot; section later in this chapter.</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<P>

Web servers are generally configured so that all CGI applications

are placed into a <TT>cgi-bin</TT>

directory. However, the Web server may have aliases so that &quot;virtual

directories&quot; exist. Each user might have his or her own <TT>cgi-bin</TT>

directory. The directory location is totally under the control

of your Web site administrator.<BR>

<p>

<CENTER>

<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>

<TR><TD><B>Tip</B></TD></TR>

<TR><TD>

<BLOCKQUOTE>

Finding out which directory your scripts need to be placed in is the first step in creating CGI programs. Because you need to get this information from your Web site administrator, send an e-mail message right now requesting this information. Also ask if 
there are any CGI restrictions or guidelines that you need to follow.</BLOCKQUOTE>



</TD></TR>

</TABLE>

</CENTER>

<P>

<H2><A NAME="CallingYourCGIProgram"><FONT SIZE=5 COLOR=#FF0000>

Calling Your CGI Program</FONT></A></H2>

<P>

The easiest way to run a CGI program is to type in the URL of

the program into your Web browser. The Web server should recognize

that you are requesting a CGI program and execute it. For example,

if you already had a CGI program called <TT>test.pl</TT>

running on a local Web server, you could start it by entering

the following URL into your Web browser:

<BLOCKQUOTE>

<PRE>

http://localhost/cgi-bin/test.pl

</PRE>

</BLOCKQUOTE>

<P>

The Web server will execute your CGI script and any output is

displayed by your Web browser.

<P>

The URL for your CGI program is a <I>virtual</I> path. The actual

location of the script on the Web server depends on the configuration

of the server software and the type of computer being used. For

example, if your computer is running the Linux operating system

and the NCSA Web server in a &quot;standard&quot; configuration,

then the above virtual would translate into <TT>/usr/local/etc/httpd/cgi-bin/test.pl</TT>.

If you were running the Website server under Windows 95, the translated

path might be <TT>/website/cgi-shl/test.pl</TT>.

<P>

If you have installed and are administering the Web server yourself,

you probably know where to place your scripts. If you are using

a service provider's Web server, ask the server's administrator

where to put your scripts and how to refereNCe them from your

documents.

<P>

There are other ways to invoke CGI programs besides using a Web

browser to visit the URL. You can also start CGI programs from:

<UL>

<LI>a hypertext link. For example:

</UL>

<BLOCKQUOTE>

<PRE>

&lt;A HREF=&quot;cgi-bin/test.pl&quot;&gt;Click here to run a CGI program&lt;/A&gt;

</PRE>

</BLOCKQUOTE>

<UL>

<LI>a button on an HTML form. You can read more about HTML forms

in <A HREF="ch20.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch20.htm" >Chapter 20</A>, &quot;Form Processing.&quot;

<LI>a server-side iNClude. You can read more about server-side

iNCludes in <A HREF="ch20.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch20.htm" >Chapter 20</A>, &quot;Form Processing.&quot;

</UL>

<P>

Interestingly enough, you can pass information to your CGI program

by adding extra information to the standard URL. If your CGI program

is used for searching your site, for example, you can pass some

information to specify which directory to search. The following

HTML hyperlink will invoke a search script and tell it to search

⌨️ 快捷键说明

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