Share E-Book

AuthorRafe Colburn

Sams Teach Yourself CGI in 24 Hours teaches the reader the next step beyond simple HTML Web pages. This new edition covers implementing CGI with both C and Perl, and it discusses CGI's relative strengths and weaknesses in comparison with other Web programming technologies like JavaScript, Java, and PHP. It also covers recent developments in templating systems and mod_perl server add-ons.The book takes the reader from the basics of CGI—learning, for example, how to implement and customize existing CGI programs that have been written by others—to the point where he can use CGI to create his own programs from scratch.The book also provides numerous real-life examples of CGI scripts—database search tools, survey forms, interactive games, order forms, guest books, and more.

AI Reading Assistant

Summary and highlights from this book's index; jump to passages in the text

Passage locations
Tags
No tags
ISBN: 0672324040
Publisher: Sams Publishing
Publish Year: 2002
Language: 英文
Pages: 534
File Format: PDF
File Size: 3.3 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
800 East 96th St., Indianapolis, Indiana, 46240 USA Teach Yourself in 24HoursRafe Colburn CGI SECOND EDITION 00 0672324040 FM 8/28/03 9:41 AM Page i
Sams Teach Yourself CGI in 24 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
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 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
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 C Environment 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
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
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 vi Sams Teach Yourself CGI in 24 Hours 00 0672324040 FM 8/26/02 11:04 AM Page vi
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 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
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 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 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 viii Sams Teach Yourself CGI in 24 Hours 00 0672324040 FM 8/26/02 11:04 AM Page viii
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 ..............................................................................................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 9 Web 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
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 ............................................................................................................166 Workshop ............................................................................................................167 Q&A ..............................................................................................................167 Quiz ..............................................................................................................167 Exercises ........................................................................................................167 Quiz Answers ................................................................................................168 10 Delving Further into Perl 169 CGI.pm ................................................................................................................169 Accessing CGI.pm from Your Program ..............................................................170 Decoding Form Data via CGI.pm ........................................................................171 Named Image-Input Fields ............................................................................171 Multiple Select Lists ......................................................................................172 File Upload Fields ........................................................................................172 Generating HTTP Headers and HTML Tags via CGI.pm ..................................175 How to Generate HTTP Headers ..................................................................176 How to Generate HTML Tags ......................................................................177 Skipping the Object-Oriented Stuff ..............................................................179 cgi-lib.pl ..........................................................................................................180 Replacing cgi-lib.pl with CGI.pm ..............................................................180 Handling Errors with CGI::Carp ........................................................................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 x Sams Teach Yourself CGI in 24 Hours 00 0672324040 FM 8/26/02 11:04 AM Page x
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 ....................................................................................197 A 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 00 0672324040 FM 8/26/02 11:04 AM Page xi
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 xii Sams Teach Yourself CGI in 24 Hours 00 0672324040 FM 8/26/02 11:04 AM Page xii
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 Contents xiii 00 0672324040 FM 8/26/02 11:04 AM Page xiii
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 Statements for Data Manipulation ................................................................343 Statements for Data Definition ......................................................................344 Statements for Database Administration ......................................................345 Database Design ................................................................................................345 Characteristics of Good Databases ................................................................346 Symptoms of Bad Databases ........................................................................347 The Design Process ......................................................................................347 Creating a Database ......................................................................................348 Choosing Which Database to Access ............................................................349 Creating a Table ............................................................................................349 Relational Data Types ........................................................................................350 String Data ....................................................................................................351 Numeric Data ................................................................................................352 Temporal Data ..............................................................................................352 Summary ............................................................................................................353 Q&A ....................................................................................................................353 xiv Sams Teach Yourself CGI in 24 Hours 00 0672324040 FM 8/26/02 11:04 AM Page xiv
Workshop ............................................................................................................353 Quiz ..............................................................................................................353 Exercises ........................................................................................................354 Quiz Answers ................................................................................................354 19 How to Use the Structured Query Language 355 Structured Query Language ................................................................................356 The SELECT Statement ..................................................................................357 Adding, Deleting, and Modifying Records ..................................................363 Database Interfaces ............................................................................................365 ODBC ................................................................................................................366 DBI and DBD ....................................................................................................366 A Sample Program Using DBI and DBD ....................................................367 Summary ............................................................................................................370 Q&A ....................................................................................................................370 Workshop ......................................................................................................371 Quiz ..............................................................................................................371 Exercises ........................................................................................................371 Quiz Answers ................................................................................................371 20 Creating an Online Store 373 The Database Design ..........................................................................................373 The Catalog Script ..............................................................................................377 Opening and Closing Database Connections ................................................381 Displaying the Product List ..........................................................................381 Adding Items to the Shopping Cart ..............................................................382 The Shopping Cart Script ..................................................................................382 Displaying the Shopping Cart ......................................................................385 Removing Items from the Shopping Cart ....................................................386 The Checkout Script ..........................................................................................387 Storing Orders in the Database ....................................................................392 Summary ............................................................................................................393 Q&A ....................................................................................................................393 Workshop ............................................................................................................394 Quiz ..............................................................................................................394 Exercises ........................................................................................................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 Contents xv 00 0672324040 FM 8/26/02 11:04 AM Page xv
Creating Your Own Ad Server ............................................................................403 Tracking User Activity ........................................................................................405 Summary ............................................................................................................409 Q&A ....................................................................................................................409 Workshop ............................................................................................................409 Quiz ..............................................................................................................409 Exercises ........................................................................................................410 Quiz Answers ................................................................................................410 22 Securing CGI Scripts 411 Why Security? ....................................................................................................412 The Crack-a-Mac Contest ............................................................................412 Risk Assessment ............................................................................................413 Securing Your Web Server ..................................................................................414 Keep Your Software Up-to-Date ..................................................................415 Store Your CGI Scripts Together ..................................................................415 File Permissions ............................................................................................416 Server Options That Are Bad for Security ....................................................417 Common CGI Security Holes ............................................................................417 A Note on How CGI Works ..........................................................................417 The Buffer Overflow Problem ......................................................................418 Don’t Send Raw Input to Shell Commands ..................................................419 Using File Paths Is Risky ..............................................................................420 Don’t Place the Perl Interpreter in cgi-bin ....................................................421 Security Hole with DOS Batch Files ............................................................421 Keep Your Server Information Private ..........................................................421 Safe Programming ..............................................................................................422 Running Shell Commands Without Using the Shell ....................................422 Summary ............................................................................................................423 Q&A ....................................................................................................................423 Workshop ............................................................................................................424 Quiz ..............................................................................................................424 Exercises ........................................................................................................424 Quiz Answers ................................................................................................424 23 Creating Custom Error Documents 425 What Is an Error Document? ..............................................................................425 Configuring Your Web Server for Custom Error Documents ............................427 The Apache Web Server ................................................................................428 Using HTML to Create a Basic Error Document ..............................................429 xvi Sams Teach Yourself CGI in 24 Hours 00 0672324040 FM 8/26/02 11:04 AM Page xvi
Using CGI to Create an Advanced Error Document ..........................................430 Environment Variables for Error Documents ................................................430 Linking Back from the Error Document to the Referring Page ..................431 Creating Custom Links from the Error Document ......................................432 Handling a “Not Found” Error ......................................................................435 Handling an “Unauthorized” Error ....................................................................439 Summary ............................................................................................................440 Q&A ....................................................................................................................440 Workshop ............................................................................................................441 Quiz ..............................................................................................................441 Exercises ........................................................................................................441 Quiz Answers ................................................................................................441 24 Server Side Includes 443 How Server Side Includes Work ........................................................................444 Setting Up Your Web Server for SSI ..................................................................444 Apache ..........................................................................................................445 Microsoft Internet Information Server ..........................................................446 Using SSI Directives ..........................................................................................446 flastmod ........................................................................................................446 SSI Directives ................................................................................................448 #echo ..............................................................................................................448 #include ........................................................................................................448 #fsize ............................................................................................................449 #exec ..............................................................................................................449 #config ..........................................................................................................450 Designing Pages Using SSI ................................................................................453 Using the #include Directive ........................................................................453 Last Modified Dates ......................................................................................455 Using the #exec Directive ............................................................................455 XSSI ....................................................................................................................457 printenv ........................................................................................................457 set ................................................................................................................457 if Directives ................................................................................................457 Summary ............................................................................................................459 Q&A ....................................................................................................................459 Workshop ............................................................................................................459 Quiz ..............................................................................................................460 Exercises ........................................................................................................460 Quiz Answers ................................................................................................460 Contents xvii 00 0672324040 FM 8/26/02 11:04 AM Page xvii
Part VII Appendixes 461 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 Tables 483 Index 489 00 0672324040 FM 8/26/02 11:04 AM Page xviii
About the Author Rafe Colburn is a software developer who works at TogetherSoft in Raleigh, North Carolina. He’s also the author of Special Edition Using SQL for Que Publishing and has contributed to a number of other computer books. His personal site, rc3.org Daily, can be found at http://rc3.org, and he can be reached via email at rafecolburn@rafe.us. 00 0672324040 FM 8/26/02 11:04 AM Page xix