| A Taste of CGI
by Danny Swarzman of Stow Lake Software Thanks to Victoria Leonard for preparing Sample1.html |
||
|---|---|---|
|
preface This presents the basics of CGI following an example. purpose and scope When a form is displayed on a web page the user enters data to be processed by a server. The form itself is coded as an HTML document. The script which runs on the server and handles the data and returns information to the user is Common Gateway Interface (GCI). The example is written in perl. Any of several languages could have been used. files in the example The example consists of these files:
Take a look at the source code as you read further. The script which runs on the server and handles the data and returns information to the user is Common Gateway Interface (GCI). information flow
the FORM tag
A form on the web has text fields, check boxes, radio buttons, etc. When all the necessary information has been entered, the user clicks on the submit button to send the data to the server.
Consider the sample form:
This form contains two text fields, a submit button and a reset button.
Specifies a URL of a the CGI script to run on our server.
The method can be POST or GET. Determines how the data are passed to the CGI. See methods of delivery below.
For every text field, button, checkbox, etc. in the form, there is an INPUT tag. Note that the INPUT tags appear between the <FORM...> and the </FORM> the structure of query strings The information the browser sends over the network consists of an address of a script followed by '?' followed by a name/value list. A name/value list consists of a series of name/value pairs separated by an ampersand, '&'. Each pair consists of the name of an INPUT in the form together with the value entered by the user.
http://www/stowlake.com/sample1.cgi?Name=Danny&Answer=3 methods of delivery The method attribute of the FORM tag specifies how the server is to present the user's data to the CGI. When the method is POST, the data are passed to the CGI as the standard input. When the method is GET, the data are passed via the environment variable, QUERY_STRING. creating mime for client's browser The CGI script writes to standard output a file that the server is to send to the browser. The information is passed in a file with a header that signals the type of information that is included. The format of the file is Multipurpose Internet Mail Extensions (mime). The mime spec provides for several header fields. The examples only use the Content-type field to specify that the contents are HTML. tourist introduction to perl This section contains a summary of some of the more common perlisms |
||
| Perlism | Example | Explanation |
| $ | $rawQuery | Indicates a string variable. |
| =~ | $cleanQuery =~ s/\+/\x20/g | Means modify $cleanQuery
|
| % | %dictionary | Indicates associative array
Each element is a pair of strings. The first in the pair serves a a key. The second in the pair is the value which is associated with the first. Thus, $dictionary{"Answer"} represents the second string of an element of %dictionary. The first string in that element is "Answer". |
| @ | @brokenPair | Indicates an array
Indices are numeric starting at 0 In this example, $brokenPair[0] would refer to the first element |
| << | print <<EndOfGood; | Indicates a here-document.
Everything that appears after this line until the terminator is copied to the output file. In the example, the interpreter will copy until it encounters a line whose sole contents is EndOfGood |
|
Bibliography Programming Perl by Larry Wall and Randal L. Schwartz published by O'Reilly - camel book CGI Programming on the World Wide Web by Shishir Gundavaram published by O'Reilly - mouse book |
||