CGI Programming on the World Wide Web
Total Page:16
File Type:pdf, Size:1020Kb
CGI Programming on the World Wide Web CGI Programming on the World Wide Web By Shishir Gundavaram; ISBN: 1-56592-168-2, 433 pages. First Edition, March 1996. Table of Contents Preface Chapter 1: The Common Gateway Interface (CGI) Chapter 2: Input to the Common Gateway Interface Chapter 3: Output from the Common Gateway Interface Chapter 4: Forms and CGI Chapter 5: Server Side Includes Chapter 6: Hypermedia Documents Chapter 7: Advanced Form Applications Chapter 8: Multiple Form Interaction Chapter 9: Gateways, Databases, and Search/Index Utilities Chapter 10: Gateways to Internet Information Servers Chapter 11: Advanced and Creative CGI Applications Chapter 12: Debugging and Testing CGI Applications Appendix A: Perl CGI Programming FAQ Appendix B: Summary of Regular Expressions Appendix C: CGI Modules for Perl 5 Appendix D: CGI Lite Appendix E: Applications, Modules, Utilities, and Documentation Index Examples - Warning: this directory includes long filenames which may confuse some older operating systems (notably Windows 3.1). Search the text of CGI Programming on the World Wide Web. http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/web/cgi/ (1 of 2) [2/6/2001 11:48:37 PM] CGI Programming on the World Wide Web Copyright © 1996, 1997 O'Reilly & Associates. All Rights Reserved. http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/web/cgi/ (2 of 2) [2/6/2001 11:48:37 PM] Preface Preface Preface Contents: What's in the Book What You Are Expected to Know Before Reading Organization of This Book Conventions in This Book Acknowledgments The Common Gateway Interface (CGI) emerged as the first way to present dynamically generated information on the World Wide Web. CGI allows the computer to generate Web pages instantly at the user's request rather than being written by someone in advance. And at the time of this writing, it remains the only stable and well-understood method for creating such pages. Java presents problems that have not yet been solved. Other products are currently just in the announcement stage. CGI is fun. You can get a kick out of writing scripts that perform tricks for you, and the users enjoy the spice the scripts add to your Web pages. But CGI has a serious side too: It lets the Internet offer the kind of interactive, user-driven applications that modern computer users have come to expect. CGI opens up an entire class of modern applications to the Web. Today's computer users expect custom answers to particular questions. Gone are the days when people were satisfied by the computing center staff passing out a single, general report to all users. Instead, each salesperson, manager, and engineer wants to enter specific queries and get up-to-date responses. And if a single computer can do that, why not the Web? This is the promise of CGI. You can display sales figures for particular products month by month, as requested by your staff, using beautiful pie charts or plots. You can let customers enter keywords in order to find information on your products. You can also offer day-to-day conveniences, like collecting comments from users, offering them searches through your archives, and letting them sign your guestbook. http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/web/cgi/ch00_01.html (1 of 2) [2/6/2001 11:48:39 PM] Preface What's in the Book So, on to the book. What will you encounter here? A variety of powerful applications that you can use and that will serve as models for your own CGI scripts. Among the complete applications in the book are an animated clock, a search tool, a survey, a quiz program, a game, a gateway to Usenet News, and an appointment calendar based on a clickable imagemap. If you want to set up your own database and can't afford a commercial product like Oracle, you can use the Sprite extension to Perl that I wrote. Sprite offers a subset of SQL commands with a flat file as the database. I also offer a debugging program called CGI Lint, and a program that lets you write and parse extensions to HTML. I wrote the latter program to support my quiz application, but you can adapt it to other purposes without much trouble. Appendix E, Applications, Modules, Utilities, and Documentation, lists where you can get Sprite and CGI Lint. But the most important tool I hope to give you is not any particular program, but a thorough understanding of CGI's potential and how to invoke it. The ideas in these programs should become yours for any purpose you want, no matter what operating system or language you use. The old adage about "teaching someone how to fish" may no longer be politically correct, in a world of dangerously depleted fish stocks, but the metaphor describes what I want to do. The techniques I show in this book are fundamental CGI practices: passing information between client (browser) and server, interacting with databases through SQL, generating graphics, writing gateways to existing programs, and storing information while handling multiple forms. What You Are Expected to Know Before Reading http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/web/cgi/ch00_01.html (2 of 2) [2/6/2001 11:48:39 PM] [Chapter 1] The Common Gateway Interface (CGI) Chapter 1 1. The Common Gateway Interface (CGI) Contents: What Is CGI? CGI Applications Some Working CGI Applications Internal Workings of CGI Configuring the Server Programming in CGI CGI Considerations Overview of the Book 1.1 What Is CGI? As you traverse the vast frontier of the World Wide Web, you will come across documents that make you wonder, "How did they do this?" These documents could consist of, among other things, forms that ask for feedback or registration information, imagemaps that allow you to click on various parts of the image, counters that display the number of users that accessed the document, and utilities that allow you to search databases for particular information. In most cases, you'll find that these effects were achieved using the Common Gateway Interface, commonly known as CGI. One of the Internet's worst-kept secrets is that CGI is astoundingly simple. That is, it's trivial in design, and anyone with an iota of programming experience can write rudimentary scripts that work. It's only when your needs are more demanding that you have to master the more complex workings of the Web. In a way, CGI is easy the same way cooking is easy: anyone can toast a muffin or poach an egg. It's only when you want a Hollandaise sauce that things start to get complicated. CGI is the part of the Web server that can communicate with other programs running on the server. With CGI, the Web server can call up a program, while passing user-specific data to the program (such as what host the user is connecting from, or input the user has supplied using HTML form syntax). The program then processes that data and the server passes the program's response back to the Web browser. CGI isn't magic; it's just programming with some special types of input and a few strict rules on program output. Everything in between is just programming. Of course, there are special techniques that are http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/web/cgi/ch01_01.html (1 of 2) [2/6/2001 11:48:41 PM] [Chapter 1] The Common Gateway Interface (CGI) particular to CGI, and that's what this book is mostly about. But underlying it all is the simple model shown in Figure 1.1. Figure 1.1: Simple diagram of CGI Acknowledgments CGI Applications http://www.crypto.nc1uw1aoi420d85w1sos.de/documents/oreilly/web/cgi/ch01_01.html (2 of 2) [2/6/2001 11:48:41 PM] [Preface] Acknowledgments Preface Acknowledgments I remember early last year when there was so much demand for a CGI book, and yet there was not even one book to help all the eager users put up counters or guestbooks. My boss at that time, Dyung Le, suggested that we write a book on CGI. Of course, we laughed it off several minutes later. But, the idea never left either one of us. Several days later, we started writing up a contract, and the rest is history-thanks to Adrian Nye, an editor at O'Reilly, who helped us put the contract together. It was a dream come true: writing a technical book for O'Reilly & Associates. First and foremost, I'd like to thank Mr. Le for not only suggesting the idea for the book, but giving me an opportunity to develop software straight out of high school. In addition, I'd like to thank Rita Horsey, my other boss, who also taught me quite a bit, and provided me with an Internet connection in the early days of the book. Of course, I'd also like to thank my family for not only putting up with my bizarre work hours during the entire writing period, but also coming to my assistance whenever I needed it. There's no way I could have finished this book without their support. Thanks to all the reviewers and everyone who provided suggestions: Jeffrey Friedl (the king of regular expressions), Andreas Koenig (the father of MakeMaker), Marc Hedlund (the originator of the CGI FAQ), Tom Christiansen (the UNIX wizard), Jon Backstrom, Joseph Radin, Paul DuBois, and from ORA, Norman Walsh, Paula Ferguson, Ellie Cutler, Tanya Herlick, Frank Willison, Andy Oram, Linda Mui (more on these guys in a minute), and Tim O'Reilly (the godfather of the Nutshell). I had the privilege of working with two really excellent teachers, Andy and Linda, whom I also consider my friends.