This text is excerpted from CGI Programming 101 (2nd edition). Click here to order the book.
Text copyright © 2004 by Jacqueline Hamilton. All rights reserved. You may print one copy of this page for your personal use. You may not copy this material onto another website or into any other medium.
This book is intended for web designers, entrepreneurs, students, teachers, and anyone who is interested in learning CGI programming. You do not need any programming experience to get started; if you can write HTML, you can write CGI programs. If you have a website, and want to add guestbook forms, counters, shopping carts, or other interactive elements to your site, then this book is for you.
"CGI" stands for "Common Gateway Interface." CGI is one method by which a web server can obtain data from (or send data to) databases, documents, and other programs, and present that data to viewers via the web. More simply, a CGI is a program intended to be run on the web. A CGI program can be written in any programming language, but Perl is one of the most popular, and for this book, Perl is the language we'll be using.
If you're going to create web pages, then at some point you'll want to add a counter, a form to let visitors send you mail or place an order, or something similar. CGI enables you to do that and much more. From mail-forms and counter programs, to the most complex database programs that generate entire websites on-the-fly, CGI programs deliver a broad spectrum of content on the web today.
This book will get you up and running in as little as a day, teaching you the basics of CGI programs, the fundamentals of Perl, and the basics of processing forms and writing simple programs. Then we'll move on to advanced topics, such as reading and writing data files, searching for data in files, writing advanced, multi-part forms like order forms and shopping carts, using randomness to spice up your pages, using server-side includes, cookies, and other useful CGI tricks. Things that you've probably thought beyond your reach, things you thought you had to pay a programmer to do . . . all of these are things you can easily write yourself, and this book will show you how.
You can also try it out before buying the book; the first six chapters are available online, free of charge, at http://www.cgi101.com/book/.
You should already have some experience building web pages and writing HTML. You'll also need Perl and a web server (such as Apache) that is configured to allow you to run your own CGI programs.
The book is written towards CGI programming on Unix, but you can also set up Apache and Perl on Mac OS X and Windows. I've written several online tutorials that will show you how to get started:
If you need an ISP that offers CGI hosting, visit http://www.cgi101.com/hosting. CGI101 offers Unix shell access, CGI programming, a MySQL database, and all of the Perl modules used in this book. It's an easy, hassle-free way to get started writing your own CGI programs.
All of the code examples in this book are available on the web at http://www.cgi101.com/book/. You can download any or all of them from there, but do try writing the programs yourself first; you'll learn faster that way.
Perl code will be set apart from the text by indenting and use of a fixed-width font:
print "This is a print statement.\n";
Unix shell commands are shown in a bold font: chmod 755 filename
Each program in the book is followed by a link to its source code:
Source code: http://www.cgi101.com/book/chX/program-cgi.html
In most cases, a link to a working example is also included:
Working example: http://www.cgi101.com/book/chX/demo.html
Each chapter has its own web page at http://www.cgi101.com/book/chX, where X is the chapter number. The full text of chapters 1-6 are online; other chapters include an index of the CGI programs and HTML forms from that chapter, links to online resources mentioned in that chapter, questions and answers relating to the chapter material, plus any chapter errata.
The 2nd edition of CGI Programming 101 has been substantially revised from the first edition. You'll learn about Perl modules from the beginning, and work with modules (including the CGI.pm module, which offers many great features for writing CGI programs) throughout the book. You'll learn how to password protect an area on your website, how to build an online catalog with a shopping cart, how to work with cookies, how to protect your site from spammers, and much more.
So turn to Chapter 1, and let's get started.