Teach Yourself CGI Programming with Perl 5 in a week Second Edit
Teach Yourself CGI Programming with PERL 5 in a Week, 2E
by Eric Herrmann
C O N T E N T S
Introduction
Chapter 1 An Introduction to CGI and Its Environment
- The Common Gateway Interface (CGI)
- HTML, HTTP, and Your CGI Program
- The Directories on Your Server
- File Privileges, Permissions, and Protection
- WWW Servers
- The CGI Programming Paradigm
- Preventing the Most Common CGI Bugs
- Learning Perl
- Summary
- Q&A
Chapter 2 Understanding How the Server and Browser Communicate
- Using the Uniform Resource Identifier
- Requesting Your Web Page with the Browser
- Using the Internet Connection
- Using the HTTP Headers
- Changing the Returned Web Page Based on the User-Agent Header
- Learning Perl
- Summary
- Q&A
Chapter 3 Using Server Side Include Commands
- Using SSI Negatives
- Understanding How SSIs Work
- Automatically Adding the Last Modification Date to Your Page
- Examining the Full Syntax of SSI Commands
- Using the SSI config Command
- Using the include Command
- Examining the flastmod Command
- Looking At Security Issues with SSIs
- Learning Perl
- Summary
- Q&A
Chapter 4 Using Forms to Gather and Send Data
- Understanding HTML Form Tags
- Using the HTML Form Method Attribute
- Generating Your First Web Page On-the-Fly
- Using the HTML Input Tag
- Making Your Text-Entry Form Fast and Professional Looking
- NPH-CGI Scripts
- Seeing What Happens to the Data Entered on Your Form
- Using URI Encoding
- Learning Perl
- Summary
- Q&A
Chapter 5 Decoding Data Sent to Your CGI Program
- Using the Post Method
- Using Radio Buttons in Your Web Page Forms and Scripts
-
Reading and Decoding Data in Your CGI Program
- Using the ReadParse Function
- Creating Name/Value Pairs from the Query String
- Decoding the Name/Value Pairs
- Using the Post Method
- Using the Perl read Function
- Including Other Files and Functions in Your CGI Programs
- Using the Data Passed with Radio Buttons
- Using Perl's If Elsif Block
- Using the HTML Checkbox
- Using a Database with Your CGI Program
- Using Pull-Down Menus in Your Web Page Forms and Scripts
- Using File Data in Your CGI Program
- Learning Perl
- Summary
- Q&A
Chapter 6 Using Environment Variables in Your Programs
- Understanding Environment Variables
- Printing Your Environment Variables
- Sending Environment Variables to Your E-Mail Address
- Using the Two Types of Environment Variables
- Finding Out Who Is Calling at YourWeb Page
- Getting the Username of Your Web Site Visitor
- Using the Cookie
- Returning the Cookie
- Learning Perl
- Summary
- Q&A
Chapter 7 Building an Online Catalog
- Using Forms, Headers, and Status Codes
- Registering Your Customer
- Setting Up Password Protection
- Dealing with Multiple Forms
- Learning Perl
- Summary
- Q&A
Chapter 8 Using Existing CGI Libraries
- Using the cgi-lib.pl Library
-
Using CGI.pm
for Creating and Reading Web Forms
- Installing CGI.pm
- Reading Input Data
- Saving Your Incoming Data
- Saving the Current State of a Form
- Creating the HTTP Headers
- Creating an HTML Header
- Ending an HTML Document
- Creating Forms
- Creating a Submit Button
- Creating a Reset Button
- Creating a Defaults Button
- Creating a Hidden Field
- Creating a Clickable Image Button
- Controlling HTML Autoescaping
- Using the CGI Library for C Programmers: cgic
- Summary
- Q&A
Chapter 9 Using Imagemaps on Your Web Page
- Defining an Imagemap
- Sending the x,y Coordinates of a Mouse Click to the Server
- Creating the Link to the Imagemap Program
- Using the imagemap.c Program
- Using the Mapfile
- Using Client-Side Imagemaps
- Summary
- Q&A
Chapter 10 Keeping Track of Your Web Page Visitors
- Defining an Access Counter
- Using the Existing Access Log File
- Using page-stats.pl to Build Log Statistics
- Getting Access Counts for Your Entire Server from wusage 3.2
- Examining Access Counter Graphics and Textual Basics
- Working with DBM Files
- Excluding Unwanted Domains from Your Counts
- Printing the Counter
-
Turning Your Counter into an Inline Image
- Generating Counters from a Bitmap
- Using the WWW Homepage Access Counter
- Using the gd 1.2 Library to Generate Counter Images On-the-Fly
- Using the gd 1.2 Library to Produce Images On-the-Fly
- Global Types
- Create, Destroy, and File Functions
- Drawing Functions
- Query Functions
- Font and Text-Handling Functions
- Color-Handling Functions
- Copying and Resizing Functions
- Summary
- Q&A
Chapter 11 Using Internet Mail with Your Web Page
- Looking At Existing Mail Programs
- Using Existing CGI E-Mail Programs
- Building Your Own E-Mail Tool
- Implementing E-Mail Security
- Defining a Regular Expression
- Summary
- Q&A
Chapter 12 Guarding Your Server Against Unwanted Guests
- Protecting Your CGI Program from User Input
- Protecting Your Directories with Access-Control Files
- Setting Up Password Protection
- Using the Authorization Directives
- Examining Security Odds and Ends
- Cleaning Up Cookie Crumbs
- Summary
- Q&A
Chapter 13 Debugging CGI Programs
- Determining Which Program Has a Problem
- Determining Whether the Program Is Being Executed
- Checking the Program's Syntax
- Viewing HTML Sources of Output
- Viewing the CGI Program's Environment
- Debugging at the Command Line
- Reading the Server Error Log
- Debugging with the Print Command
- Looking At Useful Code for Debugging
- A Final Word about Debugging
- Summary
- Q&A
Chapter 14 Tips, Tricks, and Future Directions
- Making Browser-Sensitive Pages
- Simplifying Perl Code
- Looking At the Future of Perl
- Examining Python: A New Language for CGI
- Examining Java and JavaScript: Bringing Life to HTML
- Finding Useful Internet Sites for CGI Programmers
- Summary
- Q&A
Appendix A MIME Types and File Extensions
Appendix B HTML Forms
- Form Attributes
- Input Fields
- Permitted Attributes for the Input Element
- Textarea
- Select Elements
- The Option Element
Appendix C Status Codes and Reason Phrases
Appendix D The ncSA imagemap.c Program
Appendix E The Perl Quick Reference Manual
- Perl Operators and Their Precedence
- Perl Operators and Their Meanings
- Special Variables
- Perl Commands
- Miscellaneous Perl Rules
- GNU License Information
Copyright 1997 by Sams.net Publishing
SECOND EDITION
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. For information, address Sams.net Publishing, 201 W. 103rd St., Indianapolis, IN 46290.
International Standard Book Number: 1-57521-196-3
HTML conversion by :
M/s. LeafWriters (India) Pvt. Ltd.
Website : http://leaf.stpn.soft.net
e-mail : leafwriters@leaf.stpn.soft.net
Acknowledgments
It's not possible to write a book without a lot of help from all kinds of places:
- Dad definitely hasn't been around very much in the last year, and hardly at all in the last 90 days. My oldest son, Scott, took over a lot of the work that Dad normally does, with very little complaint. Thanks, Scott.
- This book probably would not have happened without the initial encouragement to get into the Internet business, provided by my friend and mentor Mario V. Boykin. Thanks, Mario, for your business and personal support.
- Lorraine Bier is a dear friend who had the guts to tell me how awful the first couple of chapters were. Without Lori's honest early appraisal, I think my editor would have shot me. Thanks, Lori, for your editing help.
- James Martin, one of my partners and friends in this high-tech world, gave me the freedom and encouragement to spend the hours required to write a book. Thanks, James.
- A book on any subject on the Internet is always a collaborative
effort, with lots of cyberspace help. The newsgroup
comp.infosystems.www.authoring.cgi
was a big research tool for me. Thanks to everyone who answered all the myriad questions about CGI programming. Especially Thomas Boutell, Tom Christianson, Mark Hedlund, and Lincoln Stein. - Michael Moncur was a great help in getting this book done in a timely manner. When I was tired and didn't think I could write another word, Michael stepped in and wrote Chapters 13 and 14. Thanks, Mike, for the Great Work.
- It is amazing how much effort it is to write a book. My production editor, Fran Blauw, kept her sense of humor throughout the process of fixing my poor grammar and geeky English. Thanks a lot, Fran, for the hard work and keeping me smiling during the editing process.
About the Author
Eric Herrmann is the owner of Practical Internet, an online catalog and Web-page development company, and partner in Application Software Solutions Inc., a software development company focused on building intranets. Eric has a master's degree in Computer Science, 10 years of application programming experience in various asynchronous parallel processing environments, and is fluent in most of today's buzzwords: OOP, C++, UNIX, TCP/IP, Perl, and Java. Eric is happily settled on 10 acres of lovely Texas hill country in Dripping Springs, Texas, with his wife, Sherry, a riding instructor who speaks fluent horse; his three children, Scott, Jessica, and Steve; and 10 horses, 3 dogs, 4 cats, and 8 pet chickens :). When not playing at his computer, Eric helps with the horses, takes the kids fishing, or plays with model trains in the garage.
Tell Us What You Think!
As a reader, you are the most important critic and commentator of our books. We value your opinion and want to know what we're doing right, what we could do better, what areas you'd like to see us publish in, and any other words of wisdom you're willing to pass our way. You can help us make strong books that meet your needs and give you the computer guidance you require.
Do you have access to CompuServe or the World Wide Web? Then check out our CompuServe forum by typing GO SAMS at any prompt. If you prefer the World Wide Web, check out our site at http://www.mcp.com.
Note |
If you have a technical question about this book, call the technical support line at (800) 571-5840, ext. 3668. |
As the team leader of the group that created this book, I welcome your comments. You can fax, e-mail, or write me directly to let me know what you did or didn't like about this book-as well as what we can do to make our books stronger. Here's the information:
Fax: | 317/581-4669 |
E-mail: | newtech_mgr@sams.mcp.com |
Mail: | Mark Taber
Comments Department Sams.net Publishing 201 W. 103rd Street Indianapolis, IN 46290 |
Introduction
Teach Yourself CGI Programming with Perl 5 in a Week, 2E collects all the information you need to do Internet programming in one place.
In the first chapter, you will learn:
- The requirements needed to run CGI programs on your HTTP server
- How to set up the directories and configuration files on your server
- The common mistakes that keep your CGI programs from working
From there, you will learn about the basic client/server architecture of the server, and you will get a detailed description of the HTTP request/response headers. You will learn the client/server model in straightforward and simple terms, and throughout the book, you will learn about several methods for keeping track of the state of your client.
A full explanation of the unique environment of CGI programming is included in the chapters covering environment variables and server communications with the browser. The heart of CGI programming-understanding how data is managed between the client and the server-gets full coverage. Each step in data management-sending, receiving, and decoding data-is fully covered in its own chapter.
Each chapter of Teach Yourself CGI Programming with Perl 5 in a Week, 2E includes lots of programming and HTML examples. This book is an excellent resource for the novice Perl programmer; a detailed explanation of Perl is included with most programming examples. There is no assumption of the programming skills of the reader. Every programming example includes a detailed explanation of how the code works.
After teaching you the foundations of CGI programming, this book explores and explains the hottest topics of CGI programming. Make your Web page come alive with a clickable imagemap. Learn how to define the hotspots, where the existing tools are, and how to configure your server for imagemaps. Count the number of visitors to your Web page and learn about the pitfalls of getting their names. Learn how to create customizable mailing applications using the Internet sendmail format. And learn how to protect yourself from hackers, in a full chapter on Internet and CGI security.
You will find that this book is a great introduction and resource to the CGI programming environment on the Internet. Read on to begin understanding this fantastic programming environment, and good luck in all your programming endeavors. Have fun! It's more fun than not having fun.
What's New in This Edition
Teach Yourself CGI Programming with Perl 5 in a Week, 2E is a practical, hands-on guide to the world of CGI and Perl. This edition offers many new and revised examples and explanations. Furthermore, several readers suggested that more Perl foundation information would be helpful. The second edition includes new "Learning Perl" sections that focus exclusively on Perl concepts. This will give you, the reader, a much better understanding of both CGI and Perl as you work through each lesson in this book.
What This Book Is About
This book starts where most CGI tutorials leave off-just before you get into the really cool stuff! Fear not. If you are looking to take your Internet knowledge to the next level, you've made the right purchase. This book provides useful tips and hands-on examples for developing your own applications within the CGI programming environment using the Perl language. You get a complete understanding of the important CGI concepts, such as HTTP request/response headers, status codes, CGI/URI data encoding and decoding, and Server Side Include commands. You learn application development through examples in every chapter and with a complete application when you design an online catalog.
Specific features you'll see throughout the book follow:
Do &nb sp; Don't |
Do/Don't boxes: These give you specific guidance on what to do and what to avoid doing when programming in the CGI environment and Perl. |
Notes |
These provide essential background information so that you not only learn to do things within the CGI environment and Perl, but also have a good understanding of what you're doing and why. |
Tips |
It would be nice to remember everything you've previously learned, but that's just about impossible. If there is important CGI or Perl material that you have to know, these tips will remind you. |
Warnings |
Here's where the author shares his insight and experience as a professional programmer-common bugs he has faced, time-saving coding techniques he has used, and pitfalls he has fallen into. Learn from his experiences. |
What You Will Gain from This Book
Anyone who wants to know about programming on the Internet and in the CGI environment will benefit by reading this book. You spend several days covering advanced topics, yet a majority of this book is dedicated to helping you understand the CGI environment and Perl and then applying that knowledge to real applications. It is this hands-on approach to the CGI environment and the Perl language that sets this book apart from others. In addition to helping you develop an application, you learn the concepts involved in development.