Browse Sections

CGI Programming (Part 3)


Who is this for
This article is for those starting to learn CGI programming. It discusses the two ways to pass data to a CGI script.

What you Need to Have by Now
Skills: Perl programming, Basic HTML
Environment: Webserver setup to run you CGI scripts.

Review of Previous Article
In the previous article, we saw that your CGI script can use the %ENV hash to get a lot of general information. Your script can use the information in this hash during its processing. We also coded a basic CGI script that generates a HTML page.

We also coded a simple guest book with three fields. If you tried the provided sample, including putting data into the various fields and clicking the Send button, you will see the response from the CGI script when it received your data from the form. It displayed all the elements in the %ENV hash.

If you also note the $ENV{CONTENT_LENGTH} field as displayed, you will see that it has a non-zero value. We use this $ENV{CONTENT_LENGTH} to determine if there was in fact something passed to our script or not. If you read the code, the script will display a form if $ENV{CONTENT_LENGTH} is zero. Otherwise, it displays all the contents of the %ENV hash.

If you also looked carefully at the contents of the %ENV hash, you do not see the inputs from your form.

Reading Input from a Form

There are actually two methods to pass data to your CGI script: GET and POST. The difference between the two methods is the way you get the input.

  • GET uses the $ENV{QUERY_STRING} to pass data to the CGI script. The request is passed on to the server and is contained in the request string. This information is therefore written onto the server's log. DO NOT USE GET IF YOU ARE PASSING CONFIDENTIAL INFORMATION like: passwords, credit card numbers, account numbers, etc.

  • POST uses the STDIN to pass data to the CGI script. You will have to read the data from the STDIN as if you were reading it from an ordinary Perl program.

POST Method

Since in our sample script, we used the POST method, we have to read the data from STDIN. The sample code will have an additional statement, which are in bold and italicised.

The copyright of the article CGI Programming (Part 3) in Perl is owned by Philip Yuson. Permission to republish CGI Programming (Part 3) in print or online must be granted by the author in writing.

Go To Page: 1 2 3

Articles in this Topic    Discussions in this Topic