Mike Gossland's Perl Tutorial Course for Windows

Intro to GET | CGI Module | Handling Forms | Form Errors | Fatal Errors

Chapter 6. The CGI Module

Introduction to GET and POST

I assume that readers are familiar with forms in HTML and would know how to build a form with either the GET or POST methods of form submission.

When a browser makes a simple URL request of a server, such as servername.com/dir/page.html, it sends this URL to the server and gets back a response. When information is provided to the server in this way, it is using the GET method of retrieval. It is the default method for all web requests.

With the GET method, all of the information required to retrieve the URL is in the address line. Sometimes you'll see additional fields trailing the URL, like servername.com/dir/page.html?term=first, etc. This is still a GET operation, with additional parameters provided in the URL. We will learn how to access these additional parameters inside our Perl scripts later in this chapter.

In contrast, the POST method is used when information is to be uploaded to the server. Because the POST method is intended for uploading much larger quantities of data than the GET method, none of the additional data appears in the URL. Instead it is passed in to the server through a different route, but it is still accessible by your Perl programs.

As examples of how the information is passed in and how it differs between GET and POST, consider the following script, env1.pl. This is a handy little script to have around because it reveals so many interesting details. In addition, it reveals the main structure of CGI input data.

Save the following script in your cgi-bin directory. A good name would be "environment.pl". Then run it in your browser.

use CGI;

$cgi = new CGI;

print qq{Content-type: text/html


#print every key in the environment
foreach $key (sort (keys %ENV)) {
  print $key, ' = ', $ENV{$key}, "<br>\n";

#print a couple of simple forms: a POST form and a GET form
print qq{<form method="POST" ><input type="submit" value="Post Request">
<input name="postfield"></form>};
print qq{<form method="GET" ><input type="submit" value="Get  Request ">
<input name="getfield" ></form>};

print qq{</body></html>};

Once you have run the script, you'll see all kinds of details: your IP address, the server's IP address, your browser version, referrer URL, details of the server's environment, etc. Did you know that all this stuff is available to every webserver you ever connect to?

When you are ready, try putting values into either the POST or GET fields. If you look carefully, you'll notice a couple of things change... "CONTENT_LENGTH", "REQUEST_METHOD" and "QUERY_STRING", and the URL itself.

During a GET method, the parameter appears in the URL of the response. QUERY_STRING shows the parameters, and CONTENT_LENGTH is 0.

During a POST method, the parameter does not appear in the URL of the response. QUERY_STRING is missing, and CONTENT_LENGTH is non-zero.

In both cases, the requested parameter has been supplied to the script. It is just accessed in a different way.

Fortunately, all the grunt work of getting the values out of the CGI input parameters is done for you by a very useful Perl module called CGI.pm which is already in this simple script. Let's learn more about using CGI.pm in the next page.