If you are ready to build your own customized interactive documents, forms, graphics, and other full-feature CGI applica
629 50 20MB
English Pages 320 [324] Year 1996
DEVELOP
I
NG
CGI
Applications
~*^i
Perl
with
Covers the
and
CGI
HTTP Protocol Specifications
Create Forms, Ii i teractive
Graph ics,
Database Queries,
and Dynamic Documents
John Deep and
Peter Holfelder
Developing
CGI
Applications
with Perl
John Deep Peter Holf elder
Developing
CGI
Applications
with Perl
John Deep Peter Holfelder
Wiley Computer Publishing
John Wiley
New York
•
&
Sons, Inc.
Chichester
•
Brisbane
•
Toronto
•
Singapore
Publisher: Katherine Schowalter Editor:
Tim Ryan
Assistant Editor: Allison Roarty
Managing
Susan Curtin Composition: Benchmark Productions,
Editor:
Text Design
&
Inc.
Designations used by companies to distinguish their products are often claimed instances where John Wiley & Sons, Inc. is aware of a names appear in initial capital or all capital letters. Readers, however, should contact the appropriate companies for more complete information regarding trademarks and registration. as trademarks. In
all
claim, the product
This text
is
Copyright
printed on acid-free paper.
© 1996 by John Wiley & Sons,
Inc.
All rights reserved. Published simultaneously in Canada.
designed to provide accurate and authoritative information It is sold with the understanding that the publisher is not engaged in rendering legal, accounting, or other professional service. If legal advice or other expert assistance is required, the services of a competent professional person should be sought. This publication
is
in regard to the subject matter covered.
Reproduction or translation of any part of this work beyond that permitted by section 107 or 108 of the 1976 United States Copyright Act without the permission of the copyright owner is unlawful. Requests for permission of further information should be addressed to the Permissions Department, John Wiley & Sons, Inc.
Library of Congress Cataloging-in-Pnblication Data:
Deep, John. Developing applications with Perl cm. p.
/
Includes index.
ISBN 0-471-14158-5 Printed in the United States of America 10
987654321
John Deep, Peter Holfelder.
1
Contents Acknowledgments
xi
Contributors
xii
Chapter 1 The World Wide Web What Is the World Wide Web? History- of the World Wide Web
Web
1 1
2
Browsers
Future of the
3
Web
4
Web Concepts
5
Home
Pages and
Pages
5
Hypertext/Hyperlinks
5
URLs
5
Client-Server Systems
and Protocols: Buying
a Sack of Potatoes
7
Chapter 2 HTML
9
Elements and Tags
9
Document an HTML Document: The Prologue
Identifying Your as
The Basic
HTML
Elements
HTML Comments Non-Standard Elements and Attributes: Netscape Extensions
HTML Versions Chapter 3 HTML Forms Introduction
HTML FORM Elements The FORM Element The INPUT Element SELECT
TEXTAREA Queries as Hypertext Anchors
Chapter 4 The CGI
How Do How Do
I I
Execute a CGI Application? Write a CGI Application?
10
10 2 2
43
45 45 46
46 47 55
59 59
61 62 62
)
vi
Contents
CGI Methods The GET Method The HEAD Method The POST Method The CGI Command Line Sending Input to a CGI Application from the Client CGI Environment Variables Server-Oriented CGI Environment Variables Sending Output from a CGI Application to the Client Sending a Document to the Client Sending a Redirection Header to the Client
URL Encoding A Sample ISINDEX Query The HTML Form The HTTP Request The Server Output / CGI Input A Sample GET Query The HTML Form The HTTP Request The Server Output / CGI Input A Sample POST Query The HTML Form The HTTP Request The Server Output / CGI Input CGI and Security Security and popen(
),system(
Security and eval Statements Security and Server-Side Includes
Web
72 72 74 74 75 75 76 77 78 78
79 79 79 80
81
Perl
Sites
Usenet Newsgroup comp.lang.perl Mailing List
Gopher
71
81
Chapter 5 Perl What Is Perl? The History of Perl An Overview of Perl Learning More About FTP Sites
63 63 63 63 64 64 64 66 67 67 68 68 70
Site
Other Material Personal Experience: Pros and Cons of Perl
82 82 83 83 84 85 85 85 86 86
Contents
Chapter 6 The Hypertext Transfer Protocol
HTTP
Properties
89 90
A Comprehensive Addressing Scheme
91
Client-Server Architecture
91
The HTTP Protocol The HTTP Protocol
An
Is
Connectionless
91
Is
Stateless
92
Open
Extensible and
Representation for Data Types
HTTP Header Fields
92 93 94
Content-Type Date
94 94 95 95 95 95 96 96 97 97 97 97 98
Expires
From If-Modified-Since
Last-Modified
Location Referer
Server
User-Agent
HTTP Methods The GET method The HEAD method The POST method
HTTP Response
99
The HyperText Transfer Protocol
— Next Generation
Chapter 7 FormMail
100
101
Introduction
101
System Requirements
102
HTML Basics
102
Perl
and
Configuring and Running the Script Creating the
HTML Form
A Sample HTML Form
102 103 104
Perl Source for the Application
106
Extended Description of Usage of FormMail
108
Chapter 8 Free For
All
Introduction Perl
Concepts
Perl Source
112
Links
113 113
114
Scalar Variables
114
Associative Arrays
114
vii
viii
Contents
Subroutines
115
Configuring and Running the Script
116
HTML Concepts Perl and HTML Source
117 for the Free For All Links Script
HTML Source: Perl
121
links.html Perl
and
HTML Source
125 125
Source Annotations
HTML Source Annotations
29 130 1
Modifying the Free For All Links Script
Adding
New Categories
Removing Adding
a
1
Category
a Description Field to Your
Form
132 133
135
Chapter 9 Countdown
135
Introduction
Perl
and and
HTML HTML
Basic
136
Basics
Source for Countdown
Countdown
Examples of
Description of
136 1 36
Script
HTML
Use of Countdown Extended Description of
142
Source
143
and HTML Source Source
143
Perl
HTML
1
Chapter 10 Random Image Displayer
Perl
and and
149
HTML Basics HTML Source
150
for Random Image Displayer Random Image Displayer Source (rand_image.pl) Server Side Includes Random Image
Basic
Examples of HTML Source Extended Description of Perl and HTML Source Basic Random Image Displayer
Introduction Perl
and
HTML Basics
152
1
54
154 154
Random Image
The HTML Source Usage of Random Image Displayer Scripts
Chapter 11 Guestbook
151
152
Displayer (ssi_rand_image.pl)
Server Side Includes Version of
48
149
Introduction Perl
30
131
Usage of the Free For All Links Script
Perl
18
118
Perl Source: links.pl
Extended Description of
1
Displayer
1
57
160 160
163 163
164
Contents
Perl
and
HTML
Source for Guestbook
164
Perl Source (guestbook.pl)
165
HTML Sources
172
Extended Description of Guestbook
Perl
and
HTML Source
175
HTML
180
Source Annotations
Usage of Guestbook
182
Chapter 12 Server Push and Client Pull
How
Client Pull Works
Document
Blink
Drawbacks Server Push Works Multipart
183 183
Client Pull Example: Full
How
175
Perl Annotations (guestbook.pl)
184
186 1
MIME Messages
Server Push Example:
87
188
The Random Quote Server
The Quotes Database The Script Drawbacks
189 189 190 194
Chapter 13 rand*.cgi:
A Random
Anything- You-Want Generator
Introduction
195
randval.cgi:
A Generic
to Generate a
randdice.cgi:
A
Generate a randurl.cgi:
Generate a
Perl Script
Random Value
196
Perl Script to
Random Toss
A Perl
"uniquelndex
Some
195
of the Dice
196
Script to
Random URL A Feature to
":
Solve the Caching Problem
Suggestions for Enhancing randdice.cgi
and Functions You Should The Source Code and Annotations Perl Operators
Know
197 198
199 200 201
randval.cgi
201
randdice.cgi
202
randurl.cgi
205 208
Resources to Learn More
Chapter 14 A
WWW-Based
Information
Management System
209
Introduction
209
What You Should Know
21
Background
210
ix
X
Contents
212
PIMS System Requirements The PIMS System PIMS Phase I Approach
213 215 215 216
Operational Scenario
217 226 226 227 227 258 259
Design Details
PIMS Phase II Approach Scenario
Design Details
PIMS System Limitations
Summary
261
Chapter 15 ORG: ORacle Gateway HTML Form Fields
ORG
Future Plans
Objects in Perl 5 Perl
and C++ General Comparison
Why
How Objects? ORG
Objects?
Objects and
271
Chapter 16 Creating Dynamic Documents C vs. PERL in In-line Animation The Example Program— A Simple Clock Code and Annotation Using Server-Push in the Real World The Future of In-line Animation on the
WWW
Chapter 17 Inexpensive Internet Services Centralized Processing of On-line Transactions Millicent:
An
Efficient Protocol for Inexpensive Purchases
The Risk of Fraud The Millicent Protocol
264 266 266 266 269
275 278 278 278 284 285
287 288 289 289 290
Validating the Scrip
291
Detecting Double Spending
291
Checking the Request Signature
291
A Millicent Transaction on the WWW
292
Conclusion
292
References
293
Index
295
Acknowledgments Matt would like to thank Patrick Michael Kane for his assistance in pointing out flaws and inefficiencies in early versions of FFAL. His contri-
butions have greatly increased the efficiency and quality of the script.
xi
Contributors Chapters 5 and 6 Alan Richmond maintains The Web Developer's Virtual Library, with searchable database of of The Lycos 250
Web.
It
—
it is
some 1,400 annotated URLs, which
a
member
among the 250 most often cited resources on the month from 100,000 unique hosts. It has
receives a million hits a
been rated by Point
as being
among
the top 5 percent of all
Suns'n'Roses:TheWeb of Time, Visions Metaphysics, sites
is
a
is
in Astronomy,
web sites.
Cyberspace, and
rated by Point as being in the top 5 percent of all Internet
surveyed, for content, presentation, and experience.
He presented advanced
tutorials at
WWW95 in Darmstadt
WebWorlds, and was the technical editor
by John December
et
al,
(Sams. Net).
He was,
Systems Engineer and Group Leader Center. Alan created NASA's
first
for
at
and DO's
HTML and CGI Unleashed until recently, a principal
NASA's Goddard Space Flight
searchable
web
interface to astro-
physics satellite data through StarTrax (nominee. The Best of the Web
He is now a consultant and CyberWeb Software. '94).
He has
lecturer
under the company name of
built software for several international scientific research
NASA (GSFC); the European Synchrotron Radiation Facility (ESRF); the NASA/ESA Hubble Space Telescope (HST); and the Joint European Torus GET). He has over 16 years of software development experience, has been a member of several major computer societies, has projects, e.g.
published several papers on software development, and
member of the
xii
Internet Society and the Web Society.
is
currently a
Contributors
Chapters
through 11
7
who works for Hewlett-Packard while attending the 1 1th grade at is well-known in the web community for providing CGI scripts through his web site, Matt's Script Archive (http://
Matt Wright,
Fort Collins High School,
many
free
worldwidemart.com/scripts/).
He
also very
is
proud of his High School On-
Line (http://alpha.prl.kl2.co.us/Schools/schools/fchs/fchs.html) and of the
web page he
creates for his church, Heart of the Rockies Christian
(http://worldwidemart.com/hrcc/), updating
it
Church
monthly with the church's
newsletter.
Chapter 13 Cecilia Farell
consulting
on
is
the proprietor of Hippermedia, a World Wide Web and Internet
company based
in Toronto,
Canada. She
HTML 3.0 for Sams.Net Publishing, due
Cecilia Alicia
based
can be reached via e-mail
da Conceicao Perl, C,
is
currently writing a
book
March or April of 1996.
at [email protected].
a professional
and C++. She
is
for release in
CGI programmer who specializes in UNLX-
lives in Toronto,
Canada, and in her spare time
is
the Web co-ordinator for the Toronto Internet Users Group. She can be reached via e-mail at
[email protected].
Chapter 14 Hughes STX
Aileen Barry previously worked
at
Goddard Space
There she served
"
Flight Center.
as a contractor to the as
Web
NASA
developer and
WebMistress" for several years. She currently serves as a researcher for a large
publishing
company evaluating current Web technologies
oping and investigating
in addition to devel-
new ones.
Chapter 15 John Lewis has been working with the University
Libraries at Virginia Tech
since 1991- Starting in the Interlibrary Loan department, he planned and imple-
mented
a software tracking
program
(called the
"Borrowing Update System ")
xiv
Contributors
which
tracks
all
ILL requests.
where he administered computers took a job
web
in a
He
transferred to the
a site of
New Media Center,
25 networked Apple Macintosh
multimedia development environment. Recently, John
in the Library
Automation department where he works on
servers covering a variety of environments, including
Microsoft
Windows
UNIX (OSF/1).
(3.1, 95,
For fun, John
all
varieties of
and NT), Apple Macintosh, but mostly
tries to
teach himself ancient languages.
Chapter 16 Dan Austin
is
the founder of Invented Worlds, a small consulting firm
specializing in state of the art services for the
company provides advanced
World Wide Web. The
WWW solutions including custom CGI
programming, dazzling artwork, long-term maintenance, and hosting services.
Chapter 17 member of Digital Equipment Corporation's System Research Center in Palo Alto, CA. He is currently working on topics Steve Glassman
is
a
related to the Internet
moment
in the area of
and World Wide
Web — concentrating
low cost protocols
for electronic .
the
commerce,
(http ://www. research digital. com/SRC/staff/steveg/bio html) .
at
The World Wide What
Is
Web?
the World Wide
The World Wide Web those
Web
who
computers
use all
it) is
(affectionately called "the
Web" by
a collection of information stored
on
over the world that are connected to the
Internet. Each piece of information
on the Web, known
as a
resource, can be associated with other resources via hyperlinks,
so that you can
move from
topic to topic
easily,
according to your interest. The links between resources do not have to follow any specific pattern; as pieces of
mation are linked together, the system
starts to
infor-
resemble a
dense mesh, or a web. Any type of information can be a Web resource, including text, graphics, audio, and video.
The Web
is
the largest, most comprehensive, and most widely
used electronic information system It is
easy to use; you
or clicking a
mouse
move
to a
button.
in the world.
new
It is
It is
popular.
topic by pressing a key
organic; there
is
no
central
1
Chapter
I
authority that decides
what
will or will
or anyone, can add information to the
Web server, which
information on a
is
and software to respond to requests provide
is
particularly useful, other
Web — all you need
for
Web
resources.
place your
is
the information you
If
will link their information to yours,
to publish
Web
in short,
is
the
your information.
Web
European Center
(CERN), developed the World Wide
do
your information. The Web,
History of the World Wide In 1990, Tim Berners-Lee of the
to
computer with the necessary hardware
people
increasing the potential audience for
most convenient and powerful way
a
not be on the Web. This means that you,
for Nuclear Physics Research
hypertext system. His goal was to
improve the flow of information between members of the high-energy physics research community. At the time, a large in electronic form, but obstacles
Information was distributed cient
means
amount of information was
prevented
among many
it
from being used to
available
its fullest.
individual machines, without suffi-
to tie related materials together. Furthermore, each type of
resource was associated with a different protocol and could be retrieved only
by
a
program
had to learn
that "spoke" that protocol. If a file transfer
program;
newsgroup, you had to learn
a
if
you wanted
The URL,
a
naming system
download
you wanted to participate
a
in a
HTML,
that
compactly describes the location of
a language for "marking" hypertext links
features of a •
HTTP,
it
and other important
document
a protocol for transferring hypertext
documents and other
resources •
Web
clients or
Web browsers, programs
information retrieval retrieve •
Web
abilities
under one
that
combine
interface,
a variety of
making
it
easier to
and use resources
servers,
you
Usenet
to solve these problems:
every resource on the Internet and the protocol used to retrieve •
file,
newsreader program, and so on.
The World Wide Web uses the following components •
to
programs that send resources to Web
clients
on request
The World Wide Web
The system Berners-Lee created grew
Web
50
worldwide, mainly
sites
was about
to take off,
due
at
slowly; in
May
of 1993, there were only
physics research institutions. But the
made
to a decision
Web
the previous year.
Web Browsers CERN released much of its WWW technology into the public domain; this spurred development of Web servers and browsers at other organizations. A team In 1992,
led
by Marc Andreessen
(NCSA)
at
at
the National Center for Supercomputing Applications
the University of
Illinois
technology to create Mosaic, the in 1993. least
— Urbana/Champaign took advantage of this
first
graphical Web browser,
Mosaic has thousands of users and has spawned
of which
is
graphical browsers,
Web
NCSA
which have changed the
browsers make
with a single mouse
describe the use of the priate images;
it
face (and the size) of the Web.
easier than ever before to retrieve Internet
download
click. Surfing,
Web
the
a
file,
or read a Usenet
word used by Web
fans to
with a graphical browser gives you the
on top of a wave of
information.
Another key feature of Mosaic, Netscape, and other graphical browsers they are designed to
work
tive all
computer sessions.
in partnership
Web
of information can be a
It
of these resources by
news
with a graphical browser, conjures the appro-
Web
browsing the
feeling of gliding
mem-
Mosaic development group. Millions of people use
resources. You can activate a hyperlink, article
a legion of imitators, not
Netscape's Navigator, also created by Andreessen and other
bers of the original
Graphical
which was released
is
that
with helper applications. Any type
resource: sound, graphics, animation, even interac-
would be
itself;
difficult to
write a browser that could handle
most browsers can only understand
and plain ASCII formats, and graphics
in
text in
HTML
GIF and JPEG formats. Helper applica-
tions are separate programs that handle data the browser cannot understand.
When a Web
server responds to a browser's request for a resource,
transmits
it
the Multipurpose Internet Mail Extensions (MIME) type of the data to the
browser.
If
the browser
is
unable to interpret data of that
the data to a helper application that can interpret interprets the data
and opens
its
it.
MIME
type,
it
passes
The helper application
own program window
to display
it.
Helper
application support has encouraged information providers to add graphics, ani-
mation, and sound to their Web
sites,
making the Web
a true
multimedia system.
Chapter
1
In the time since
NCSA
released Mosaic, the growth of Web traffic has been
phenomenal, as reflected
in
the
number of Web
•
In
June 1993, there were 130
•
In
December 1993,
•
In June 1994, there
•
In
December 1994,
sites.
there were 623
were 1,265 there
servers worldwide:
sites.
sites.
were 11,576
There are currently more than 15,000
Web
sites.
50 to 100 more con-
servers, with
necting every day.
Future of the
Web
At present, two major trends are shaping the future of the Web. The major
commercial on-line services are making the Web available to their subscribers, and corporations are
installing
Web
servers and creating an on-line presence.
These two trends, which reinforce each
Web from medium
a repository of scientific
that
other, are
changing the nature of the
and technical information
combines information, entertainment, and
to an interactive
advertising.
Web access via CompuServe, Prodigy, and America Online (AOL) is bringing the Web to a different — and much larger — audience than ever before. In the beginning, the Web was the exclusive province of scientists and engineers, and
its
content reflected the needs of that technically proficient audience. As
an ever greater percentage of the population gains access to the Web, tent
is
Web
shifting to serve a
sites are related to
sites
a site that
was devoted
find this site at
—
I
appeal of the Internet
but
recently
came
across
entirely to information about llamas! (Llama fans
can
http://www.webcom.com/~degraham/.)
The promise of an inexpensive mass-market presence
rally,
con-
music, photography, gardening, movies, and games.
covering almost any topic imaginable
There are
its
more general audience. Some of the most popular
— has drawn corporations of
computer and technology companies
many companies
all
— and
the high-tech
sizes to the
Web. Natu-
are at the forefront of this trend,
in other industries are staking their claim
on the Web
as
well. Although most of these companies are placing product information, mar-
keting materials, and technical support information
on the Web,
a
growing
The World Wide Web
number of them
Web
are using the
transactions are of
two
types:
one
to is
a
conduct electronic transactions. These
Web-based on-line
scribers pay for the privilege of access to a
ordering,
where people can transmit an
Web
site;
service,
where
the other
is
sub-
on-line
electronic order for a product.
Web Concepts To understand the Web, you
need to know
will
how
its
resources are
organized, linked together, and identified, and you will need to
know
a bit
about the underlying design of the system. The following sections, which cover these topics, will give you the background you need.
Pages and Home Pages Web documents are written in HTML and are known as pages. Unlike paper documents, a Web page has no size; it is a unit of information, and it can Most
correspond to any number of paper pages.
When
placing a collection of pages
one page page
is
that
known
is
on
a
Web
site,
an author usually creates
a starting point for exploring the other pages; this type of
as a
homepage. Home pages
are often a combination of a
title
page and a table of contents. There are some standard methods of writing the
home page that make the URLs section.
address of a
cussed
in
it
easier to find them; these
methods
are dis-
Hyper text/ Hyper inks I
The
links
between resources
that contain the links are
are
known
known
as hyperlinks,
as hypertext
and the documents
documents. The author of
a
hypertext document marks the words of the document that refer to related topics and
embeds the
locations of the related materials in the document.
The
hypertext viewing program sets the marked words apart from the rest of the text,
using underlining, different colors, or reverse video (white on black, for
instance).
When you
activate the hyperlink with a keystroke or
the hypertext viewing program displays the material to
mouse
which the
click,
link refers.
URLs URLs, or Uniform Resource Locators, have three parts: transfer
names, domain names, and path names.
mechanism
Chapter
•
1
Transfer
mechanism names,
scheme names,
or
are abbreviations of
the transfer protocol used to retrieve the resource. For instance, http is
the
name
for the
transfer Web pages.
Hypertext Transfer Protocol, which
The most common
transfer
the transfer mechanisms they correspond •
Domain names
refer to the
stored. Optionally, a user
port •
used to
are
shown
in Table 1.1
computer on which the resource
name
number may be added
to,
is
mechanism names, and
/
is
password combination and an IP
to the
domain name.
Path names refer to the location of the resource within the computer's storage system. The path
name
computer where the resource
The URLs
for
is is
written from the perspective of the stored;
it is
usually a full path
most organizations' home pages follow
this
name. convention:
http://wssrw.organization_donuiin_name, where organization_domain_
Table 1.1
The World Wide Web
name is the domain name the organization uses. For instance, the domain name of the World Wide Web Consortium, a group that helps define future development of the Web,
is
w3.org, and the URL
http://www.w3.org. There
home page on
is
home page
their
for
also a convention for the
URL
is
of an individual's
UNIX system. It is: htXp://www.organization_domain_ name/~user_name. As an alumnus of Rensselaer Polytechnic Institute, I keep several pages on their system, whose domain name is rpi.edu. The URL for my
home page
a
http://www.rpi.edu/~holfep.
is
Client-Server Systems and Protocols:
Buying a Sack of Potatoes The Web
on
built
is
general store;
it
a client-server
A
client
is like
and asks for one or more items on
like a collection of
is
tomers agree to speak
announce
The the
a
A
server
in
who
just as a
grocer
walks into the store
list.
a
common protocol. A
sentences the general store owner and his cus-
when conducting business.
presence to the server;
its
the proprietor of a
with a request,
customer
a grocery
is like
and servers communicate with each other using
protocol
store
come
waits for a client to
waits behind a counter.
Clients
model.
and greeting the owner, saying
For instance, the client
may
customer walking into
this is a like a
"Hello, Mrs. Hatfield.
a
How are you today?"
server then acknowledges the client and asks for the request; in the store,
owner might
then gives
say "I'm fine, Mr. McCoy.
What can
request, "I'd like a sack of potatoes,"
its
resource to the
client;
I
get for you?"
and the server
The
client
transfers the
"Here are your potatoes, Mr. McCoy. Have a nice day"
course, programs don't converse in
human
Of
language; most protocols are codes
that are designed to handle a very small set of situations efficiently in a standard It's
time Mr.
McCoy bought
The Web
is
as
if
earlier.
requests you made, even
Each transaction you make
ones. This has
means
that the server
is
every
unusu-
Unlike Mrs. Hatfield in the general store, the server will not
remember any previous
its
is
if
you made them only
discrete, unrelated to
sec-
any previous
advantages and disadvantages. Even though stateless systems
can't always give like,
supplies,
a stateless system; in essence, this
ally forgetful.
onds
McCoy used the same sentences except for the name of the supplies.
Mrs. Hatfield and Mr.
manner.
you the good, old-fashioned customer service you would
they are relatively easy to write.
HTML Most of the information available on the matted
in
language
today
Hypertext Markup Language (HTML). is
HTML
is for-
A markup
a set of symbols that specify the structure or for-
matting of a document, as opposed to eral,
Web
its
contents. In gen-
defines the structure of the document, but
it
does not define the format of a document. The format of an
HTML document is defined by the reader of the document, who configures a browser program to associate different typographical attributes to different
document elements.
For instance, headings are often in boldface and in a larger type size than body
text.
Elements and Tags HTML
defines different
document elements, such
headings, body text, ordered (numbered) (bulleted)
lists,
lists,
as titles,
unordered
and hyperlinks. The elements of an
HTML 9
10
Chapter
2
document have marked by
a hierarchical structure, like
a start tag at
end of the element.
is
characters; or
the beginning of the element and by an
Start tags are
an example of a
is
an outline. Each element
end tag
at
is
the
enclosed within the < and > characters;
End
start tag.
tags are enclosed within the
Tags are not case sensitive:
will work as well as .
Document as an HTML Document: The Prologue Identifying Your
To
identify your
document
HTML document, you place
as an
beginning of the document. The prologue consists of the
HTML PUBLIC Other
'-//W30//DTD
HTML
a
prologue
line
at
the
valid prologues are as follows:
•
•
•
•
\n"
nput type=reset>\n"
print "\n": links";
print "Back to the free for all print "page\n"
;
print "\n" exit: 1
sub html_check
[12]
I
$F0RM( 'title'l
—
s/] \n)*>//g: |
I
Figure 8.1
shows how the "Error-No Title page" appears on the screen.
HTML Source: Here
is
links.html
the complete
HTML
source for Free For All Links. The Perl script uses
the and
Computers
\n\n";
}
else
(
print GUEST )
167
'
168
Chapter
J
1
Example 11.1 continued. Log The
it
if
Entry
(luselog eq
&log(entry)
'
1'
{
)
;
)
mmmm Options
it
[8]
Mail
it
if
Option
($mail
eq
T)
{
"|$mailprog $recipient")
open (MAIL.
print MAIL "Reply-to: print MAIL "From:
||
die "Can't open $mailprog!\n"
$F0RM( 'username'}
$F0RM{ username'
}
($F0RM( 'realname'
print MAIL "Subject:
Entry to Guestbook\n\n":
print MAIL "You have
a
print MAIL
}
)\n":
($FORM{'realname'} )\n";
new entry in your guestbook:\n\n";
"
\n";
print MAIL "$F0RM('comments' )\n"; print MAIL "$FORM{'realname' 1":
if
(
$FORM{'username') "
print MAIL
){
";
print MAIL "\n";
if
(
$F0RM{'city'}
){
print MAIL "$F0RM( 'city'
)
.";
}
if
(
$FORM('state'} "
print MAIL
if
(
){
$F0RM{ 'state' }";
$FORMCcountry'}
){
print MAIL
"
$F0RMf country'}":
print MAIL
"
•
$date\n";
print MAIL
"
}
\n"
close (MAIL); }
[9]
if
($remote_mail open
(MAIL,
eq
'1'
&& $F0RM( 'username'}
"|$mailprog -t")
||
)
{
die "Can't open $mailprog!\n";
::
Guestbook
print MAIL "To:
$F0RM{ 'username' )\n";
print MAIL "From:
$recipient\n";
print MAIL "Subject:
Entry to Guestbook\n\n";
print MAIL "Thank you for adding to my guestbook. \n\n";
print MAIL
"
\n"
print MAIL "$FORM{'comments' }\n"; print MAIL "$F0RMC realname'
if
(
$FORM('username'}
){
\n"; print "Comments :
\n"; print "\n":
print " * \n"; print "Return to the Guestbook."
print "\n\n":
# Log The Error if
($uselog eq
'1')
&log(no_comments)
{
;
}
exit; }
[12]
sub no_name
{
print "Content-type:
text/html\n\n";
print "No Name\n"; print "Your Name appears to be blank\n"; print "The Name Section in the guestbook fillout form appears to\n"; print "be blank and therefore your entry to the guestbook was not\n"
Please add your name in the blank below.
\n";
print "added.
print "\n": print "Your Name:\n";
print "E-Mail: print
"
. State: Country:
print "size=15Xp>\n"; print "Comments have been retained.
\n"; print "\n";
print " * (input type-reset>\n":
":
'
:
;
Guestbook
print "Return to the Guestbook. print "\n\n";
# Log The Error if ($uselog eq
'1')
{
&log(no_name) }
exit: )
[13]
# Log the Entry or Error
sub log
{
$log_type = $^[0]; open
(LOG,
">>$guestlog")
if ($log_type eq
'entry')
(
print LOG "$ENV{'REM0TE_H0ST'}
-
[$shortdate]\n";
-
[$shortdate]
-
ERR:
No Name\n"
[Ishortdate]
-
ERR:
No ":
}
elsif ($log_type eq 'no_name')
{
print LOG "$ENV{'REM0TE_H0ST'} }
elsif ($log_type eq
'no_comments'
print LOG "$ENV{ REM0TE_H0ST' '
}
{
)
-
print LOG "Comments \n"; } }
[14]
# Redirection Option sub no_redi rection
(
# Print Beginning of HTML print "Content-Type:
text/html\n\n":
print "Thank You\n": print "Thank You For Signing The Guestbook\n";
# Print Response print "Thank you for filling in the guestbook.
Your entry has\n";
print "been added to the guestbook. \n"; print "Here is what you submitted:
\n"; print "$F0RM{ 'comments }
\n"; '
if
($F0RM('url'})
(
print "$FORM{'realname' }"; }
else
{
print "$F0RM{'realname' )": }
171
172
Chapter
1
1
Example 11.1 continued. if
(
if
$FORM( 'user-name') (
$1
i
nkmai
print
1
){
'1')
eq
{
";
"
)
}
print "
\n";
if
(
$FORM('city'}
){
print "$FORM('city'}.":
if
(
$FORM{'state'}
print
){
SFORMCstate'}";
"
}
if
(
$FORM{'country')
print
){
$FORM('country'}":
"
}
print
"
-
$date
\n";
# Print End of HTML
print "\n":
print "Back to the Guestbook\n"; print "- You may need to reload it when you get there \n";
print "to see your entry. \n"; print "\n"; exit; }
HTML Sources The HTML source
for
this
example consists of three documents.
guestbook.html contains the guest information that the Perl script writes; contains a
comment, ,
that the script uses to place the
information.
guestbook.html Example 11.2 HTML source
for
Guestbook.
new
it
guest
Guestbook
Title of Guestbook
[A]
Your Header
We would love
Thank you for visiting our pages.
it
if you would
href="addguest.html")Add to this guestbook we are keeping!