Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 543 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
543
Dung lượng
3,94 MB
Nội dung
www.it-ebooks.info
800 East 96th St., Indianapolis, Indiana, 46240 USA
Teach Yourself
in
24
Hours
Rafe Colburn
CGI
SECOND EDITION
00 0672324040 FM 8/28/03 9:41 AM Page i
www.it-ebooks.info
Sams TeachYourselfCGIin24 Hours,
Second Edition
Copyright 2003 by Sams Publishing
All rights reserved. No part of this book shall be reproduced, stored in a
retrieval system, or transmitted by any means, electronic, mechanical, photo-
copying, recording, or otherwise, without written permission from the pub-
lisher. No patent liability is assumed with respect to the use of the information
contained herein. Although every precaution has been taken in the preparation
of this book, the publisher and author assume no responsibility for errors or
omissions. Nor is any liability assumed for damages resulting from the use of
the information contained herein.
International Standard Book Number: 0-672-32404-0
Library of Congress Catalog Card Number: 2002107939
Printed in the United States of America
First Printing: September 2002
05 04 03 4 3 2
Trademarks
All terms mentioned in this book that are known to be trademarks or service
marks have been appropriately capitalized. Sams Publishing cannot attest to the
accuracy of this information. Use of a term in this book should not be regarded
as affecting the validity of any trademark or service mark.
Warning and Disclaimer
Every effort has been made to make this book as complete and as accurate as
possible, but no warranty or fitness is implied. The information provided is on
an “as is” basis. The author and the publisher shall have neither liability nor
responsibility to any person or entity with respect to any loss or damages aris-
ing from the information contained in this book.
Bulk Sales
Sams Publishing offers excellent discounts on this book when ordered in quan-
tity for bulk purchases or special sales. For more information, please contact
U.S. Corporate and Government Sales
1-800-382-3419
corpsales@pearsontechgroup.com
For sales outside of the U.S., please contact
International Sales
1-317-428-3341
international@pearsontechgroup.com
ACQUISITIONS EDITOR
Betsy Brown
DEVELOPMENT EDITOR
Scott Meyers
MANAGING EDITOR
Charlotte Clapp
INDEXER
Kelly Castell
PROOFREADER
Suzanne Thomas
TECHNICAL EDITORS
Greg Perry
J. Eric Sloan
TEAM COORDINATOR
Amy Patton
MULTIMEDIA DEVELOPER
Dan Scherf
INTERIOR DESIGNER
Gary Adair
COVER DESIGNER
Alan Clements
PAGE LAYOUT
Brad Lenser
00 0672324040 FM 8/28/03 9:41 AM Page ii
www.it-ebooks.info
Contents at a Glance
Introduction 1
Part I An Introduction to CGI 3
Hour 1 Overview of CGI Programming 5
2 Setting Up Your CGI Environment 19
3Downloading, Installing, and Debugging CGI Scripts 35
4Writing Your First CGI Program 53
Part II Capturing User Input 71
Hour 5 Creating HTML Forms 73
6Working with HTTP 91
7Validating User Input 111
8Creating an Email Feedback Form 131
Part III CGI Programming Languages and Tools 147
Hour 9 Web Application Architecture 149
10 Delving Further into Perl 169
11 Other Popular CGI Programming Languages 189
12 Pros and Cons of Alternate Technologies 211
Part IV Building Basic CGI Applications 227
Hour 13 Using Flat Files for Data Storage 229
14 Creating a CGI-Based Message Board 253
15 Session Management 275
16 Building a Simple Shopping Cart 299
17 Content Management with CGI 321
Part V Integrating Databases with CGI 339
Hour 18 Working with Relational Databases 341
19 How to Use the Structured Query Language 355
20 Creating an Online Store 373
00 0672324040 FM 8/26/02 11:04 AM Page iii
www.it-ebooks.info
Part VI Additional CGI Tips and Tricks 395
Hour 21 Handling Other Content Types 397
22 Securing CGI Scripts 411
23 Creating Custom Error Documents 425
24 Server Side Includes 443
Part VII Appendixes 461
Appendix A MIME Types 463
B Response Codes and Reason Phrases 467
CEnvironment Variables and Request Headers 471
D Summary of Regular Expressions 479
E ASCII Table 483
Index 489
00 0672324040 FM 8/26/02 11:04 AM Page iv
www.it-ebooks.info
Contents
Introduction 1
Part I An Introduction to CGI 3
Hour 1 Overview of CGI Programming 5
Types of Web Applications 6
A History of CGI 6
What Is a CGI Program? 7
How CGI Programs Work 8
How Resources Are Requested 8
Fulfilling the Request 9
Passing Data to a CGI Program 11
Pros and Cons of CGI 11
CGI Programming Languages 13
Perl 13
UNIX Shell 13
The C Language 14
Visual Basic 15
Python 15
Java 16
Summary 16
Q&A 16
Workshop 17
Quiz 17
Quiz Answers 17
2 Setting Up Your CGI Environment 19
The Web Server Itself 19
Hosting Your CGI Scripts 20
Running Your Own Web Server 20
Web Hosting 21
Web-Server Operating Systems 22
UNIX 22
Windows 23
The CGI Environment 23
Web Servers 24
Web-Server Directory Structure 24
How Scripts Are Executed 25
Setting Up Your CGI Development Environment 26
Step 1: Download a Web Server 26
Step 2: Install the Web Server 27
00 0672324040 FM 8/26/02 11:04 AM Page v
www.it-ebooks.info
Step 3: Download a Perl Interpreter 28
Step 4: Install the Perl Interpreter 28
Step 5: Get the Web Server Up and Running 29
Step 6: Test the Web Server 29
Step 7: Test a Perl CGI Script 30
What If Something Went Wrong? 32
Summary 33
Q&A 33
Workshop 34
Quiz 34
Exercises 34
Quiz Answers 34
3 Downloading, Installing, and Debugging CGI Scripts 35
Downloading Scripts from the Internet 36
Finding the Scripts You Need 36
What to Look for in Publicly-Available Scripts 37
Installing a Downloaded Script 38
Example: Downloading and Installing a Guestbook Script 38
Configuring the Script 39
Installing the Files and Setting Permissions 40
Testing the Script 41
Customizing the Look and Feel 41
Debugging CGI Scripts 42
Finding the Source of an Error 42
Fixing Setup Errors 44
Tools and Techniques for Debugging Your Program Code 47
Compiled Versus Interpreted Languages 48
Running CGI Scripts from the Command Line 49
Using Print Statements for Debugging 50
Summary 50
Q&A 51
Workshop 51
Quiz 51
Exercises 52
Quiz Answers 52
4Writing Your First CGI Program 53
Parts of CGI Programs 54
A Sample CGI Program 54
A URL-Redirection Program 57
How File Redirection Works 59
Pipes 60
vi SamsTeachYourselfCGIin24 Hours
00 0672324040 FM 8/26/02 11:04 AM Page vi
www.it-ebooks.info
Working with Files in Perl 61
Common Statements Used in Perl 63
The if Statement 65
Perl Expressions 66
Summary 68
Q&A 68
Workshop 69
Quiz 69
Exercises 69
Quiz Answers 69
Part II Capturing User Input 71
5Creating HTML Forms 73
The
<form> Tag 74
The
action Attribute 74
The
method Attribute 75
The
enctype Attribute 75
The
target Attribute 75
The
<input> Tag 76
Text Input Fields 76
Password Fields 77
Check Boxes 78
Radio Buttons 78
Hidden Fields 79
File Upload Fields 80
Reset Buttons 80
Submit Buttons 80
Using Images as Submit Buttons 81
Other Form Fields 82
Text Areas 83
Select Lists 84
Workshop: Building an Entire Form 85
Elements in the Survey Form 87
Summary 87
Q&A 88
Workshop 88
Quiz 88
Exercises 88
Quiz Answers 89
Contents vii
00 0672324040 FM 8/26/02 11:04 AM Page vii
www.it-ebooks.info
6Working with HTTP 91
HTTP Basics 92
What Takes Place During an HTTP Session 92
Step 1: Establish a TCP Connection 93
Step 2: The Web Browser Sends a Command to the Server 94
Step 3: The Web Browser Sends Request Headers 95
Step 4: The Web Server Responds 95
Step 5: The Web Server Sends Response Headers 96
Step 6: The Web Server Sends the Data to the Browser 96
Step 7: The Web Server Closes the TCP Connection 96
Request Methods 97
The
GET Method 97
The
POST Method 98
Choosing Between
GET and POST 98
Server Response Codes 99
Response Headers 101
Cache-control 101
Content-length 101
Content-type 101
Expires 101
Pragma 102
Server 102
Set-Cookie 102
NPH Scripts 102
Content Types 103
How Servers Use Content Types 104
Content-Type Categories 105
Nonstandard Types 106
Secure Connections 107
Summary 108
Q&A 109
Workshop 109
Quiz 109
Exercises 109
Quiz Answers 110
7Validating User Input 111
Using JavaScript for Form Validation 111
How JavaScript Works to Validate Forms 112
An Example of Form Validation 113
The Form Itself 113
The Event Handler 114
Designing Easily Validated Forms 114
viii SamsTeachYourselfCGIin24 Hours
00 0672324040 FM 8/26/02 11:04 AM Page viii
www.it-ebooks.info
Incorporating Validation into the Form-Processing Code 115
How a Form-Processing Program Works 115
An Example of Form Processing 116
The Main Script Logic 117
The Input Validation Subroutine 118
The Output Subroutine 119
The Form Creation Subroutine 120
The Full Source Code 121
Validating Values 123
Regular Expressions 124
An Example That Uses Regular Expressions 126
Summary 128
Q&A 128
Workshop 128
Quiz 128
Exercises 129
Quiz Answers 129
8Creating an Email Feedback Form 131
What Kinds of Applications Involve Sending Email? 132
How Email Works 132
Mail Message Composition 134
Using
Net::SMTP 135
Example: Sending Email from a CGI Script 136
Setting Things Up 138
The Application Logic 139
Validating the Form 140
Sending the Email Message 141
Using
sendmail 141
Using
sendmail with Perl 142
Summary 143
Q&A 144
Workshop 144
Quiz 145
Exercises 145
Quiz Answers 145
Part III CGI Programming Languages and Tools 147
9Web Application Architecture 149
Application Design 150
Round-trip Scripts 150
Figuring Out Whether a Form Was Submitted 151
Declarative Programming 152
Contents ix
00 0672324040 FM 8/26/02 11:04 AM Page ix
www.it-ebooks.info
[...]... Good luck! www.it-ebooks.info PART I An Introduction to CGI Hour 1 Overview of CGI Programming 2 Setting Up Your CGI Environment 3 Downloading, Installing, and Debugging CGI Scripts 4 Writing Your First CGI Program www.it-ebooks.info www.it-ebooks.info HOUR 1 Overview of CGI Programming Do you use the World Wide Web to look up the latest stock quotes? Do you purchase items from online stores? Have you... webdev@samspublishing.com Mail: Mark Taber Associate Publisher Sams Publishing 800 East 96th Street Indianapolis, IN 4 6240 USA Reader Services For more information about this book or others from Sams Publishing, visit our Web site at www.samspublishing.com Type the ISBN (excluding hyphens) or the title of the book in the Search box to find the book you’re looking for www.it-ebooks.info www.it-ebooks.info... concepts behind software design in order to write useful programs in Perl www.it-ebooks.info 2 Sams TeachYourself CGI in24 Hours My goal in this book is to explain the Perl concepts that I use in the example programs, and not to go beyond that I also used as few special Perl features as possible in order to apply the lessons in these examples to the programming language that you use to write CGI programs...x Sams TeachYourself CGI in24 Hours Handling Many Types of Requests 153 Sharing Code Among Scripts 156 Using CGI: :Application 158 Installing CGI: :Appliction 159 Creating CGI: :Application Applications 162 An Example That Uses CGI: :Application 162 Other CGI: :Application Notes 166 Summary ... 187 CGI. pm www.it-ebooks.info Contents 11 Other Popular CGI Programming Languages xi 189 Will My Favorite Language Work for CGI? 189 Writing CGI Programs Using the Bourne Shell 190 How Shell Scripts Work 191 Creating Gateways to UNIX Commands 192 Working with Query Strings 193 Writing a Program That Uses the Query String 194 Writing CGI Programs in C ... 221 The Apache mod_perl Module 222 Porting Your Scripts from CGI to mod_perl 223 Summary 224 Q&A 224 Workshop 225 Quiz 225 Exercises 225 Quiz Answers 225 www.it-ebooks.info xii Sams TeachYourself CGI in24 Hours Part IV Building Basic CGI Applications 227 13 Using Flat Files for Data Storage 229 What Is a Database?... also point out that most of the examples in this book are written in Perl Perl is a scripting language that’s available for most computing platforms, including Unix and Windows It has a number of features that make it well-suited to CGI programming, and in fact, it’s the most popular language for writing CGI programs The biggest advantage for readers of this book is that it’s easy to learn Perl in bits... Overview of CGI Programming CGI Programming Languages As I’ve already stated, almost any programming language can be used to write CGI programs Just because it isn’t mentioned here doesn’t mean that it’s unsuitable for CGI programming As long as programs written in the language can meet the criteria that I discussed earlier in this hour for CGI programs, it can be used for CGI programming In this section... 394 Quiz Answers 394 Part VI Additional CGI Tips and Tricks 395 21 Handling Other Content Types 397 Content Types 397 Handling Binary Content 398 An Authenticated Download Application 399 www.it-ebooks.info xvi Sams TeachYourself CGI in 24 Hours Creating Your Own Ad Server 403 Tracking User Activity 405 Summary 409 Q&A... application development platform In this book, you will learn how to build interactive Web applications using the Common Gateway Interface (CGI) —which is the predominant platform for deploying Web applications today www.it-ebooks.info 6 Hour 1 This hour will provide the introductory and background information you need before you can start writing CGI programs The following topics will be discussed: • . www.it-ebooks.info
800 East 96th St., Indianapolis, Indiana, 4 6240 USA
Teach Yourself
in
24
Hours
Rafe Colburn
CGI
SECOND EDITION
00 0672 3240 40 FM 8/28/03. 8/28/03 9:41 AM Page i
www.it-ebooks.info
Sams Teach Yourself CGI in 24 Hours,
Second Edition
Copyright 2003 by Sams Publishing
All rights reserved. No part