Copyright ©1996, Que Corporation. All rights reserved. No part of this book may be used or reproduced in any form or by any means, or stored in a database or retrieval system without prior written permission of the publisher except in the case of brief quotations embodied in critical articles and reviews. Making copies of any part of this book for any purpose other than your own personal use is a violation of United States copyright laws. For information, address Que Corporation, 201 West 103rd Street, Indianapolis, IN 46290 or at support@mcp .com.

Notice: This material is excerpted from Special Edition Using CGI, ISBN: 0-7897-0740-3. The electronic version of this material has not been through the final proof reading stage that the book goes through before being published in printed form. Some errors may exist here that are corrected before the book is published. This material is provided "as is" without any warranty of any kind.

Introduction

The Common Gateway Interface, or CGI, is one of the most useful tools in a Webmaster's kit. Whether you're the lone maintainer of a single home page on someone else's machine or the Webmaster of a huge domain, you'll find that CGI is essential for anything beyond presenting static text and graphics.

CGI is the magic behind Web-based interactive games, page counters, order-entry systems, online shopping carts, SQL database interfaces, animations, and clickable images. In fact, you'll find that CGI, in one of its many forms, is what brings the World Wide Web to life.

How This Book Came About

The book you are reading is the result of group effort: The lead authors, Jeffry Dwight and Michael Erwin; the production team at Que Publishing; the contributing authors; and you.

You? Yes-in two ways. First, the growing number of CGI programmers, mixed with proliferating standards and new Web server capabilities, told us there was both interest and need. The second way you contributed to this book is more specific: We based much of the book's content on the types of questions you most frequently ask in public places-feedback to program authors, Usenet newsgroups, chat sessions, CompuServe forums, and similar areas.

So if there hadn't been an interest from you-as well as a good deal of confusion and misinformation in the community at large-this book would never have seen the light of day. We hope to satisfy most of your questions, and provide a reference you can keep around for project after project.

Intended Audience

We assume throughout the book that you have at least an intermediate understanding of programming in one or more languages. This book won't teach you how to program, but it will teach you how to use your existing programming skills to make CGI scripts work.

We didn't shy away from complicated topics, but we made sure to cover the fundamentals, too. In all, if you're comfortable with C, Perl, Visual Basic, or AppleScript, you should be able to glean a great deal of information from these pages.

We explain the basics of CGI programming fairly well, so even if you've never thought of writing CGI before, you'll do fine. If you're already an accomplished CGI programmer, you'll find hundreds of tips and tricks throughout the book to expand your repertoire.

Where possible, we used pseudocode or a textual description of the process under discussion. We did this for a couple of reasons: First, this book is intended to be platform-independent, meaning that you should be able to profit from it no matter what server you run and no matter what programming environment you use. Second, a textual description forces the reader (and the author) to focus on the process rather than the syntax. The goal is for you to understand how the magic works, not just what to type.

You'll also find a lot of actual code, both in the pages of the book and on the accompanying CD-ROM. We assume that programmers, once they understand the concepts, will want to go forth and create programs. What better way than to be armed with working samples?

How This Book Is Organized

This book is divided into eight major parts. These natural dividing lines let you hop around the book in hyperlink fashion, if that's how you like to read.

Throughout the book you'll find references to other chapters and sections where the material under discussion is either first introduced or discussed in more detail. This way, we can avoid recapitulating introductory material in an advanced section and can concentrate on the fundamentals without worrying about implementation in a tutorial section. In short, this scheme helps us keep from wasting your time. Feel free to jump straight in at the chapter that catches your interest, or read straight through from beginning to end. Either way, we have you covered.

The following sections discuss the eight major parts of the book.

Part I: CGI Fundamentals

The chapters in this section give you a complete introduction to CGI--everything from what it is and how it's used to what tools you'll need to start using it yourself. You'll also take a peek at where CGI is going, to help you prepare for the future.

Part II: CGI Application Development

The chapters in this section dive right into the meat of programming CGI. We'll teach you how to design your programs, how to document them, and how to get them to work across platforms where possible. We'll also explain some of the limitations to CGI, and teach you ways of getting around them.

Part III: CGI Programming Examples

These chapters start off by presenting some sample CGI scripts, and then show you how to modify them for your own use. You'll also learn about secure HTTP and how to take advantage of it.

Part IV: CGI Search Engines

The chapters in this section provide a basic overview of site indexing and database use, both from the user's point of view (finding information) and the Webmaster's point of view (providing information). You'll see why and how indexing is used, and you'll find out all about tying your back-end SQL engine into your Web site.

Part V: Creating Interactive HTML Documents

Interactivity is king on the Web, and the chapters in this section will show you how to make your site come alive with personalized, up-to-the-minute information. You'll also find a lot of ready-to-run sample programs that are both useful and fun.

Part VI: CGI Tips & Techniques

Here's where we let you in on the secrets of the experts. For each of the five most popular scripting languages, we show you the low-down, nitty-gritty details to let you get the most from the environment.

Part VII: Polishing CGI Scripts

The CGI operating environment places some special considerations on testing and debugging. In these chapters, we'll show you how to make your scripts as close to bulletproof as possible. We'll also talk about CGI security in detail.

Part VIII: Learning from the Pros

In this short section, we'll take you on a whirlwind tour of the Internet to show you places where you can see how the best and brightest minds have implemented CGI. We'll also point you to resources you can snatch and use for your own programs.

Conventions Used in This Book

Que has more than a decade of experience writing and developing the most successful computer books available. With the experience, we've learned what special features help readers the most. Look for these special features throughout the book to enhance your learning experience.

Several typeface and font conventions are used to help make reading the text easier:

print qq|<h1>This is a test, only a test.</h1>\n|;
This book also uses a couple of icons to alert you to important information. The icon in the margin next to this paragraph tells you that the program, routine, library, or document under discussion can be found on the accompanying CD-ROM. We've included the things you'll find most useful and indexed the entire CD-ROM so you can read it using your Web browser. This icon alerts you to important security information in the text. You'll see it used to warn you of programmer mistakes that lead to insecure applications, common hacker methods and practices, and known security holes. Armed with these tips, you can make your programs secure and robust.
Tips suggest advice on easier or alternative methods, to help you program more efficiently.
Notes either point out information often overlooked in the documentation, or help you solve or avoid problems.
Cautions alert you to potentially negative consequences of an operation or action, especially if the latter could result in serious or even disastrous results, such as loss or corruption of data.
What is the purpose of a troubleshooting section?
Troubleshooting sections provide you with advice on how to avoid or solve problems. Troubleshooting information is presented in the format of a question/problem followed by a solution.
To find troubleshooting tips more quickly, refer to the special Index of Common Problems near the end of the book.

If a paragraph mentions features described elsewhere in the book, a special cross-reference box will follow the paragraph to refer you to a related section in another chapter in the book. For example,

See  "Section in Another Chapter" for more information on the subject at hand, p. xxx.

These cross-references function like hypertext links and allow you to navigate through the text.

Sidebars Provide Deeper Insight
This paragraph format provides technical, ancillary, or non-essential information that you may find interesting or useful. Sidebars are like extended notes, but you can skip over them without missing something necessary to the topic at hand.

A Word from the Authors

Michael Erwin and I have enjoyed putting this book together. Our hope is that you'll find both instruction and inspiration in its pages and that, armed with the information we provide, you'll be able to write your own programs.

There are a thousand things we didn't cover, and if we had more space or more time, we'd love to include them. Our aim, though, was to get all the essentials-to give you enough to get started. In the winnowing process, we had to make choices...this bit stays, that bit goes. We hope our choices make sense to you, and that you find this book both useful and fun. If you have suggestions for future editions, we'd like to hear from you.

QUE Home Page

For technical support for our books and software contact support@mcp.com

Copyright ©1996, Que Corporation