Sams Teach Yourself CGI in 24 Hours, 2nd Edition
800 East 96th St., Indianapolis, Indiana, 46240 USA Teach Yourself in
24 Hours Rafe Colburn
CGI SECOND EDITION
A CQUISITIONS E DITOR Sams Teach Yourself CGI in 24 Hours, Betsy Brown
Second Edition D EVELOPMENT E DITOR Scott Meyers
Copyright 2003 by Sams Publishing M ANAGING E DITOR All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photo- Charlotte Clapp copying, recording, or otherwise, without written permission from the pub-
I NDEXER lisher. No patent liability is assumed with respect to the use of the information Kelly Castell contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or
P ROOFREADER omissions. Nor is any liability assumed for damages resulting from the use of Suzanne Thomas the information contained herein.
T ECHNICAL E DITORS International Standard Book Number: 0-672-32404-0 Greg Perry
J. Eric Sloan Library of Congress Catalog Card Number: 2002107939 T EAM C OORDINATOR
Printed in the United States of America Amy Patton First Printing: September 2002
M ULTIMEDIA D EVELOPER
05
04
03
4
3
2 Dan Scherf
I NTERIOR D ESIGNER Trademarks Gary Adair All terms mentioned in this book that are known to be trademarks or service
C OVER D ESIGNER marks have been appropriately capitalized. Sams Publishing cannot attest to the Alan Clements 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.
P AGE L AYOUT Brad Lenser
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
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
3 Downloading, Installing, and Debugging CGI Scripts
35
4 Writing Your First CGI Program
53 Part II Capturing User Input
71 Hour 5 Creating HTML Forms
73
6 Working with HTTP
91
7 Validating User Input 111
8 Creating an Email Feedback Form 131
Part III CGI Programming Languages and Tools 147
Hour 9 Web Application Architecture 14910 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 22914 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 34119 How to Use the Structured Query Language 355
20 Creating an Online Store 373
Part VI Additional CGI Tips and Tricks 395
Hour 21 Handling Other Content Types 39722 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 C Environment Variables and Request Headers 471 D Summary of Regular Expressions 479
E ASCII Table 483
Index 489
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
vi Sams Teach Yourself CGI in 24 Hours
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
4 Writing 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
Contents vii
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
5 Creating HTML Forms
73 <form>
The Tag ....................................................................................................74 action
The Attribute ......................................................................................74 method
The Attribute ......................................................................................75 enctype
The Attribute ....................................................................................75 target
The Attribute ......................................................................................75 <input>
The 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
viii Sams Teach Yourself CGI in 24 Hours
6 Working 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 GET
The Method ..............................................................................................97 POST
The Method ............................................................................................98 GET POST
Choosing Between and ....................................................................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
7 Validating 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
Contents ix
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
8 Creating an Email Feedback Form 131
What Kinds of Applications Involve Sending Email? ........................................132 How Email Works ..............................................................................................132Mail Message Composition ..........................................................................134 Net::SMTP
Using ............................................................................................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 sendmail
Using ............................................................................................141 sendmail
Using 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
9 Web Application Architecture 149
Application Design ............................................................................................150 Round-trip Scripts ..............................................................................................150Figuring Out Whether a Form Was Submitted ..............................................151 Declarative Programming ..................................................................................152
x Sams Teach Yourself CGI in 24 Hours
Handling Many Types of Requests ....................................................................153 Sharing Code Among Scripts ............................................................................156 CGI::Application
Using ....................................................................................158 CGI::Appliction
Installing ..........................................................................159 CGI::Application
Creating Applications ....................................................162 CGI::Application
An Example That Uses ..................................................162 CGI::Application
Other Notes ....................................................................166 Summary ............................................................................................................166 Workshop ............................................................................................................167 Q&A ..............................................................................................................167
Quiz ..............................................................................................................167 Exercises ........................................................................................................167 Quiz Answers ................................................................................................168
10 Delving Further into Perl 169 CGI.pm
................................................................................................................169 CGI.pm
Accessing from Your Program ..............................................................170 CGI.pm
Decoding Form Data via ........................................................................171 Named Image-Input Fields ............................................................................171 Multiple Select Lists ......................................................................................172 File Upload Fields ........................................................................................172
CGI.pm Generating HTTP Headers and HTML Tags via ..................................175 How to Generate HTTP Headers ..................................................................176
How to Generate HTML Tags ......................................................................177 Skipping the Object-Oriented Stuff ..............................................................179 cgi-lib.pl
..........................................................................................................180
cgi-lib.pl CGI.pm
Replacing with ..............................................................180
CGI::Carp
Handling Errors with ........................................................................181 Sending Fatal Errors to the Browser ............................................................182 Resources for Perl Programmers ........................................................................182 Perl Documentation ......................................................................................183
Perl Information on the Web ........................................................................183 Perl Books ....................................................................................................184 The Comprehensive Perl Archive Network ..................................................184 Summary ............................................................................................................186
Q&A ....................................................................................................................186 Workshop ............................................................................................................186 Quiz ..............................................................................................................186 Exercises ........................................................................................................186 Quiz Answers ................................................................................................187
11 Other Popular CGI Programming Languages 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 ................................................................................195 The cgic
Library ................................................................................................196
Printing Headers ............................................................................................196 Printing Output ..............................................................................................197 Handling Form Input ....................................................................................197A C Example ......................................................................................................201 Writing CGI Programs in Python ......................................................................205 A Python Example ..............................................................................................206 Summary ............................................................................................................208 Q&A ....................................................................................................................208 Workshop ............................................................................................................209
Quiz ..............................................................................................................209 Quiz Answers ................................................................................................209
12 Pros and Cons of Alternate Technologies 211 Looking Back ......................................................................................................212 Why CGI Alternatives Appeared ..................................................................212
Getting Past CGI’s Limitations ....................................................................212 J2EE ....................................................................................................................213 Servlets ..........................................................................................................214 JavaServer Pages ..........................................................................................214
PHP ....................................................................................................................216 Examples Using PHP ....................................................................................216 ASP.NET ............................................................................................................218 Business Objects ............................................................................................218
Code Blocks ..................................................................................................219 HTML Pages ................................................................................................221 Macromedia ColdFusion ....................................................................................221 ColdFusion Sample Code ..............................................................................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
Contents xi
xii Sams Teach Yourself CGI in 24 Hours
Part IV Building Basic CGI Applications 227
13 Using Flat Files for Data Storage 229 What Is a Database? ..........................................................................................230 Flat-File Databases ............................................................................................230
Delimiting Data Using Characters ................................................................231 Delimiting Data Using Field Widths ............................................................231 File Operations ....................................................................................................232 Retrieving Records from a Database ............................................................232
Inserting a Record into a Database ..............................................................232 Deleting Records from a Database ................................................................232 Modifying a Record in a Database ................................................................233 File Locking ..................................................................................................233
Building a Database Application ........................................................................234 The Sample Database ....................................................................................234 Retrieving Records from the Database ........................................................235 Inserting a Record into the Database ............................................................238 Deleting Records from the Database ............................................................243 Modifying a Record in the Database ............................................................245
Summary ............................................................................................................250 Q&A ....................................................................................................................250 Workshop ............................................................................................................250 Quiz ..............................................................................................................251
Exercises ........................................................................................................251 Quiz Answers ................................................................................................251
14 Creating a CGI-Based Message Board 253 The Structure of the Application ........................................................................254 The File Format ..................................................................................................254 The Display Script ..............................................................................................255 Utility Subroutines ........................................................................................260
Opening the Topic File ..................................................................................260 Parsing a Topic File ......................................................................................261 Printing the Topic List ..................................................................................263 Printing a Topic ............................................................................................264
The Posting Script ..............................................................................................265 Presenting the New Topic Form ....................................................................270 Processing a New Topic Submission ............................................................271 Adding a Response ........................................................................................272
Summary ............................................................................................................272 Q&A ....................................................................................................................273
Contents xiii
Workshop ............................................................................................................273 Quiz ..............................................................................................................273 Exercises ........................................................................................................273 Quiz Answers ................................................................................................273
15 Session Management 275 Why Use Session Management? ........................................................................276 Basic Authentication ..........................................................................................277 Hidden Fields in Forms ......................................................................................278 Hidden Form Fields Example ............................................................................279
Application Logic ..........................................................................................281 Printing the Hidden Fields ............................................................................281 Using Cookies ....................................................................................................282 How Cookies Work ......................................................................................283
Using Cookies to Save User Information ......................................................285 Using Cookies to Retrieve User Information ................................................287 Setting and Retrieving Cookies with JavaScript ..........................................289 Session Management with Cookies ....................................................................290 The Catalog Page ..........................................................................................291 The Checkout Form ......................................................................................293 Why CGI and Cookies Don’t Mix ................................................................295 Summary ............................................................................................................295
Q&A ....................................................................................................................296 Workshop ............................................................................................................296 Quiz ..............................................................................................................296 Exercises ........................................................................................................297 Quiz Answers ................................................................................................297
16 Building a Simple Shopping Cart 299 How the Sample Shopping Cart Works ..............................................................300 The Catalog ........................................................................................................301
Printing the Catalog ......................................................................................304 Adding Items to the Shopping Cart ..............................................................305 The Contents of the Shopping Cart ....................................................................306 Printing the User’s Cart ................................................................................309
Removing an Item from the Cart ..................................................................311 Checkout ............................................................................................................312 The Checkout Script for This Example ........................................................313 Summary ............................................................................................................318
Q&A ....................................................................................................................319 Workshop ............................................................................................................319 Quiz ..............................................................................................................320 Exercises ........................................................................................................320 Quiz Answers ................................................................................................320
xiv Sams Teach Yourself CGI in 24 Hours
17 Content Management with CGI 321 Why Content Publishing? ..................................................................................321 Separating Content and Presentation ............................................................322
Building in an Editorial Process ....................................................................322 Types of Content Publishing Systems ................................................................322 Data Storage for Content Publishing Systems ..................................................324 A Content Publishing Example ..........................................................................324
The Story-Input Program ..............................................................................325 The Story-Display Program ..........................................................................330 Free Content Management Systems ..................................................................335 Mason ............................................................................................................336
Zope ..............................................................................................................337 PostNuke ........................................................................................................337 Red Hat Content Management Solution ......................................................337 Summary ............................................................................................................337
Q&A ....................................................................................................................337 Workshop ............................................................................................................338 Quiz ..............................................................................................................338 Exercises ........................................................................................................338 Quiz Answers ................................................................................................338
Part V Integrating Databases with CGI 339
18 Working with Relational Databases 341 The Relational Database Model ........................................................................341 Structured Query Language ................................................................................343