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 also write CGI programs. If you have a web page, and want to write CGIs, then this book is for you.
What is CGI?
"CGI" stands for "Common Gateway Interface." CGI is the 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, CGI is programming for the web. A CGI can be written in any programming language, but Perl is the most popular, and for the course of this book, Perl is the language we'll be using.
Why learn CGI?
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 scripts that generate entire websites on-the-fly, CGI programs deliver a broad spectrum of content on the web today. If you've ever looked at a site such as Amazon.com, DejaNews, or Yahoo, and wondered how they did it... the answer is CGI. CGI experience is also in high demand from employers now; you could substantially improve your salary and job prospects by learning CGI.
Why use this book?
This book will get you up and running in as little as a day, teaching you the basics of CGI scripts, the fundamentals of Perl, and the basics of processing forms and writing simple CGIs. Then we'll move on to advanced topics, such as reading and writing data to and from files, searching for data in a file, writing advanced multi-part forms like order forms and storefronts, using randomness to spice up your pages, using server-side includes, cookies, and other useful CGI tricks. Things you probably have 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.
Also included are several appendices that will be invaluable references, including a list of other online Perl resources, CGI job search sites, and tutorials for Unix and password-protection.
You can also try this course before buying the book; the first six chapters are available online, free of charge, at http://www.cgi101.com/class/.
What do you need to get started?
This book is written for the programmer using Perl on a Unix system. Don't worry if you're not familiar with Unix; this book will teach you all you need to know. Each chapter will show you the Unix commands you need to use with your CGIs, and there's also a Unix command reference and tutorial in Appendix B. If you don't already have access to a Unix account, you can get one from a number of ISPs, including cgi101.com, which offers telnet-only shell accounts and virtual hosting with access to CGIs, CGI helper modules, and a library of ready-to-use scripts. Visit http://www.cgi101.com/ for more information.
If you are using Windows NT instead of Unix, you can still use most of the programs in this book, and learn Perl just as easily. Most NT machines run the same Perl code that Unix machines will. But some of the examples will not work for you, since they are intended for Unix. The Perl Reference for Windows (http://www.perl.com/reference/query.cgi?windows) has some links to websites that can help you get started using CGIs on a Windows system.
You will need a Telnet client to connect to your Unix host of choice. If you are using a Windows PC, you should already have Telnet installed; just go under "Start"->"Run" and type "C:\WINDOWS\telnet.exe". A new telnet window will open. To connect to the Unix host, just pull down the"Remote" menu and select "Connect". Type the host name you want to connect to (such as cgi101.com), press "Connect", and you'll be in. Here's what a typical login window looks like once you connect:
If you are using a Mac, there are a number of free or shareware Telnet programs available online, including BetterTelnet (http://www.cstone.net/%7Erbraun/mac/telnet/), dataComet (http://www.databeast.com/), and NiftyTelnet (http://andrew2.andrew.cmu.edu/dist/niftytelnet.html).
If you still need help or info on how to connect or upload your CGIs to the Unix host, visit our website at http://www.cgi101.com/class/connect.html.
All of the code examples in this book are available on the web at http://www.cgi101.com/class/. You can download any or all of them from there, but do try writing the scripts yourself first; you'll learn faster that way. The website also includes some related utilities and a library of ready-to-use CGI programs, plus a mailing list you can join to get updates of new material.
Conventions Used in this Book
All Perl and HTML code will be set apart from the text by indenting and use of a fixed-width font, like:
All programs printed in the book are followed by a link to their respective source code:
print "This is a print statement.\n";
In most cases, a link to a working example is also included:
Source code: http://www.cgi101.com/class/chX/script.txt
Each chapter also has its own web page at http://www.cgi101.com/class/chX, where X is the chapter number. The full texts of chapters 1-6 are online; other chapters include only an index to the CGI scripts and HTML forms from that chapter.
Working example: http://www.cgi101.com/class/chX/demo.html
So, turn to Chapter 1, and let's get started writing CGIs!