Introducing "CGI Programming on the Web" by Andy Oram _________________________________________________________________ Give yourself a challenge. All you need is a Web server, a scripting language of your choice and a few lunch hours... The World Wide Web Web spent its early years as a publishing medium. You wrote what you want to say, you put it up and people read it. But along with the logarithmic growth of the Web, a lot of people realized that it could play a much more dynamic role. It could grow up to turn into a useful institutional asset, providing instant information within and between far-flung institutions. And the Common Gateway Interface (CGI) was the first technology to exploit this new use. In this article I'll introduce the new O'Reilly & Associates book on the subject, CGI Programming on the World Wide Web , by Shishir Gundavaram. One might say that dynamic information on the Web is just another step in fulfilling the promise of computers. Ever since batch jobs gave way to interactive programs, computers have acted as silent robots searching out the information people want. CGI lets a network offer the same dynamic, automated and customized access. Consider Dr. Georgiana Henk, a (fictitious) medical researcher with a busy schedule, a myriad of research projects, and an I-want-to-know-now attitude. Back in the 1980's, thanks to interactive spreadsheet-like software, she got used to pulling up a window on her computer and instantly viewing the results of today's study. Enter the Internet. Now that she maintains two offices in different cities and is using a network instead of a single computer, why can't she still query a database and see ever-changing information? If someone has written the proper CGI script, she can. Many people like to use this expanded Web capability for fun. They tell you "Four hundred and seventy-six people have logged into this site," or offer a guestbook to sign, or let you spin a virtual wheel and visit a random link on the Web. But as we saw above, CGI has its serious side, too. Of course, the person who maintains Dr. Henk's research or offers financial data to the organization's accountants could write a stand-alone program, sending forth socket calls (interfacing to the network) in its guts. For large amounts of data or routine, batch-like applications, that's still the way to go. But for small amounts of data where appearance is important, a stand-alone program is going to look old and worn the day it is released. Why not provide the convenience and attractive interface of a familiar Web browser? You get rich support for graphics, while links can take the users wherever they want to go through the data. People appreciate getting all their information through the same tool, and you can concentrate on the details of managing your information. What CGI Stands For Since you may find a name like Common Gateway Interface disorienting, let's take it apart and see what each word implies: Common - Assures you that CGI can be used by many languages and interact with many different types of systems. It doesn't tie you down to one way of doing what you want. Gateway - Suggests that CGI's strength lies not in what it does by itself, but in the potential access it offers to other systems such as databases and graphic generators. Interface - Simply means that CGI provides a well-defined way to call up its features, in other words, that you can write programs that use it. You can learn a lot from the CGI name once you look behind the austere exterior. What You Can Do With CGI Got your browser at hand? Try out some of the examples that we describe in our book. Quiz First, to exercise those memory neurons and show an application of a special quiz-generating program that Shishir wrote, take a quiz about the Web. The program takes quiz information from a file, generates forms that are shown to the user, accepts information from the forms, and grades the quiz. Try out this World Wide Web Quiz. Game Remember the game of Concentration, which little kids play? You turn up tiles and try to find pairs that match. A Web version of the game requires each tile to convey the state of the game so that the server can generate a new board after each click. Play with us. Database Queries Using SQL Looking up data and pretty-printing it for a browser to display is where CGI shines. Not many achievements will impress your colleagues as much as when you write a program that lets them access a large database at the click of a button and see dynamically generated output in their favorite browser. If you have an Oracle or Sybase database, free extensions to Perl and Tcl let you issue SQL to create and retrieve data. Our book shows you how to use some of these extensions in the context of a CGI program. For people who lack the commercial databases, Shishir has written a library that accepts SQL queries and creates or returns data from a simple flat file. To try it out, visit the Employee Database. Searching If you have a large Web site, your users probably wish they could search through files to find the ones that contain a particular string. Our book shows you how to provide this valuable feature. The program accepts a search string from the user and creates HTML output that shows this string in context within each file that contains it. The program could easily be extended to accept regular expressions, but Shishir sticks to fixed strings for security reasons. Take a look at this Search Gateway. Animation with CGI and Server Push I don't know if people will choose CGI over Java to do animation. The Java model is certainly appealing, because the server sends a program to the client and leaves it up to client power to execute it. But animation in CGI is quite easy. Here's an example that uses the Server Push Method. Dynamic Graphing and Plotting Another feature is determining how many people actually look at the examples in this article. Well, we can find out on an hourly basis. We just have to keep track of hits and plot the results on a timeline when each user clicks the URL. Try this Web Plotting Graph. Many more examples of dynamic image production are shown, in languages such as PostScript, gnuplot (a free language that's good for simple plotting), pgperl (a more powerful plotting library that is available in Perl), and the gd Graphics Library, which can be called from several languages including Perl and Tcl. Forms Simple forms are not hard to do in CGI. A typical book about the Web sticks to the simplest case, patiently taking you through each step of taking data from the form, converting characters, and so on. And you know what? There are freely-available tools to do just that. Decoding is the starting point for CGI, not the endpoint. We take you through all the basic steps of handling forms (in several different programming languages!) and on to more complicated applications such as the popular guestbook. Here's a survey you can try, Favorite Ice Cream Survey. Experienced CGI programmers know that form processing gets tough when you have multiple forms. This is because a separate run of the CGI program starts each time a form is submitted. How can you accumulate results from many different forms when the data is read by several different instances of your program? "Magic cookies"are one solution, shown by this TV and Movie Survey. Gateways and Remote Communications You're not going to create every Web application from scratch; you probably have legacy systems you want to hook up to. The databases mentioned earlier in this article are one example. Another is an Interface to Usenet News. This program uses a simple library based on sockets to communicate with an NNTP server. Mail Everybody has to offer a form that you can fill out in order to send mail to the Webmaster, even if the editor is primitive and the whole process is much more trouble than a real mailer. This application is just another gateway. You can let Shishir know what you think of his examples. That's not all, folks! That's it for the more spectacular displays. But our book discusses a number of introductory nuts-and-bolts issues too, such as configuring the system to run CGI scripts, generating HTTP headers, server-side includes, and debugging through a program that Shishir wrote, CGI Lint. The Language Issue You must learn a programming language to do anything non-trivial with CGI scripting. But you have your choice of languages: Perl, Tcl, Visual Basic, C, Applescript, and just about anything else. That's what the C in CGI stands for. Remember, it's a common interface. If you're not already wedded to one language for all time, this is a good point in your career to learn Perl. Most of the examples in the book are written in Perl, and when you see the marvelous things it can do to a Web page in two or three lines, you may well wonder why anyone would want to use any other language for CGI. But we recognize that people have pre-existing expertise in other languages, so we've provided some tips for getting started and doing common tasks in these languages. We also introduce some of the more obscure special-purpose languages that let you develop fancy pages quickly. Regardless of the language you ultimately choose, this book speaks valuable secrets to you. Give yourself a challenge. Find out what you can offer to your organization through dynamic Web pages. All you need is a Web server, a scripting language of your choice, and a few lunch hours. Rarely have so many applications become available using such simple tools. Eventually you may have the satisfaction of hearing from people all over the world who find your CGI scripts invaluable. Andy Oram is co-author of Managing Projects with Make and an editor at O'Reilly & Associates