Chapter 5 using Perl for CGI
Programming
• This chapter introduces the common
Gateway Interface (CGI) and discusses
How Perl can be used as a CGI
Programming Language. It begins with
Overview of CGI, how CGI programs are
Liked to web clients, and how results
Are returned to clients from CGI programs.
• You'll also learn in this chapter about
CGI. PM module, which provides more
Efficient Ways To Using CGI. PM.
5.1 The Common Gateway
Interface
• HTML is a markup language and, as such,
Cannot by itself describe computations,
Allow interaction with the user, or provide
Access to a database.
• The interface between a browser and
Software on the server that was developed
Is called the Common Gateway Interface.
• When a server has es a request for
CGI program, it does not return the file-it
Executes it.
5.1 Common Gateway
Interface
Client
Internet
Web
Server CGI app
5.1 Common Gateway
Interface
• When a new document is generated, it
Consists of two parts, an HTTP header and
A body. The header must be complete if
The response is going directly to the client.
• The contents of the form are encoded and
Transmitted to the server. The server must
Be some program to decode
Transmitted form contents.
5.2 CGI Linkage
• In this section, we describe how
Connection between an HTML document
Displayed by a browser and a CGI
Program on the server is established.
5.2 CGI Linkage
• If a CGI program has been compiled and
Is in machine code, the server can invoke
It directly. However, the compiled versions
Of Perl programs are not saved and are
Not in machine code anyway, so the perl
System must be invoked on every perl cgi
Program.
5.2 CGI Linkage
•#! /Usr/local/bin/perl-W
• #! Specifies that the program whose
Location follows must be executed on the rest
The file.
• The-W flag on this line specifies that the perl
Compiler shoshould produce warning messages
When it finds thins that cocould potentially be errors.
We recommend that it always be include,
Whether a program is run using the perl
Command or by including the line described
Above.
5.2 CGI Linkage
• An HTML document specifies a call to
CGI program using an anchor tag (<A> ),
Which must include hypertext reference
Attribute (href ).
• Examples page 134 sample reply.html
5.2 CGI Linkage
• In Perl, this means that the print function is
Way to generate a response to be sent to
Client.
• This first line of the header specifies the form
The response, as a mime (Multipurpose Internet
Mail Extension protocol) content type.
• The line following the one-line header must
Always be blank. The blank line indicates
End of the HTTP header.
Print "Content-Type: text/html/n ";
5.3 HTML for forms
• The most common way for a user
Communicate information from a Web
Browser to the server is through a form.
• Forms, which are modeled on the paper
Forms that people continually are required
To fill out, are described in HTML and
Objects on a screen form.
• These objects are called widgets.
5.3 HTML for forms
• There are widgets:
-Single-line text collection
-Multiple_line text collection
-Checkbox
-Radio button
-Meanu
-Submit
-Reset
5.3 HTML for forms
• Most widgets are used to gather information
From the user, in the form of either text or button
Selections.
• Each widget has a value, either by default or
From user input.
• Together, the values of all of the widgets in
Form are called the form data.
• Each form requires a submit button. When
User presses the submit button, the form data is
Do the processing. The rest button resets all
Values of the widgets of the form.
5.3.1 The <form> tag
• All of the components of a form appear
The content of a <form> tag. <form> can
Have several different attributes:
-Action: Specifies the URL of the application
That is to be call when the user presses
Submit buttons.
Action = "localhost/cgi/reply. pl"
-Method: specifes one of the two techniques,
Get or post, used to pass the form data
The server.
5.3.1 The <form> tag
• Get is the default, so if no Method
Attribute is given in the <form> tag, get
Will be used. The alternative technique is
Post.
• In both techniques, the form data is coded
Into a text string when the user presses
The submit button. This text string is called
A query string.
5.3.1 The <form> tag
• When the get method is used, the browser attaches
Query string to the URL of the CGI program, so the data
Is transmitted to the server with the URL. The Browser
Inserts a question mark at the end of the actual URL just
Before the first character of the query string so that
Server can easily find the beginning of the query string.
The server removes the query string from the URL and
Places it in the environment variable QUERY_STRING,
Where it can be accessed by the CGI program.
Http://replay.pl? Name = Jack & sex = male
• The get method can also be used to pass Parameters
To the server when forms are not involved (this is cannot
Be done with post ).
5.3.1 The <form> tag
• The main disadvantage of the get
Method is that some serves place a limit
On the length of the URL string and
Truncate any characters past the limit.
• Another potential problem with get is that
The query string is vulnerable to illegal or
Inappropriate access because of its
Appearance with the URL, because URLs
Are easy to find by network sniffers.
5.3.1 The <form> tag
• When the POST method is used,
Query string is passed through standard
Input of the CGI program, so the CGI
Program can simply read the string.
Length of the query string is passed
Through the environment variable
Content_length. There is no length
Limitation for the query string with
POST method, so it is obviusly
Choice when there are more than a few
Widgets in the form.
5.3.2 wiiot
• This section briefly describes the widgets
That can be placed in a form and how they
Are specified in HTML.
• Attributes of the popular widgets are specified
With the <input> tag. These are for text,
Checkboxes, radio buttons, and the special
Buttons, submit and rest.
5.3.2 wiiot
• The one attribute of <input> that is required
All of the widgets discussed in this section is
Type, which specifies the specified kind of widget.
• All widgets counter t reset and submit also
Require a name attribute, which becomes
Name of the value of the widget within the query
String.
• The widgets for check boxes and radio buttons
Require the value attribute, which initializes
Value of the widget.
5.3.2 wiiot
• Text widget
-The default size of the box created by a text
Widget is 20 characters.
-The size attribute can be used to give
Different size.
-The maxlength attribute to specify
Maximum number of characters that
Browser will accept in the box.
• Example page 137
Sample widget.html
5.3.2 wiiot
• Check box
-Are used to collect multiple-choice input form the user.
-Every checkbox button requires a Value Attribute in its
<Input> tag.
-The attribute checked, which is assigned the value
Checked, indicates that the checkbox button is initially
On.
-In bytes cases, checkboxes appear in lists, with every
One having the same name. The content of <input>
Tag is displayed next to the check box button,
Providing a label.
Example page 139
Sample check.html
5.3.2 wiiot
• Radio box
-Are closely related to checkbox buttons.
Difference between a group of radio buttons and
Group of checkboxes is that only one radio button can
Be on or pressed at any time.
-All radio buttons in a group must have the name
Attribute Set in the <input> tag. And all radio buttons
In a group have the same name.
-The checked attribute, set to the value checked in
<Input> tag of the button's definition.
Example page 141
Sample radio.html
5.3.2 wiiot
• Check box and radio buttons are valid
Methods for collection multiple-choice data
From a user. However, if the number
Possible choices is large, the displayed
Form becomes too complex and long. In
These cases, a menu shocould be used.
5.3.2 wiiot
• Menu
• A menu is described in HTML using the <SELECT>
Tag.
• There are two kinds of menus:
-Only one menu item can be selected at a time
-Multiple menu items can be selected
• The default option is the one related to radio
Buttons. The other option can be specified
Adding the multiple attribute, which gets
Value multiple, to the <SELECT> tag.
Sample menu.html
5.3.2 wiiot
• Menu
-When multiple menu items have been selected,
Value for the menu in the query string between des all
Selected menu items.
-The size attribute can be include in the <SELECT> tag.
Size specifies the number of menu items that are
Be displayed for the user.
-Each of the item in a menu is specified with
<Option> tag. The content of an <option> tag is
Value of the menu item, which is just text.
-The <option> tag can include the select attribute,
Which specifies that the item is preselected.
5.3.2 wiiot
• Textarea
• The text typed into the area created
<Textarea> tag is used to create such
Widgets.
• The default size of the visible part of
Text in a text area is often quite small, so
The Rows and Cols attributes shoshould usually
Be writable ded and set to reasonable sizes.
Sample texarea.html
5.3.2 wiiot
• Reset button
Clears all of the widgets in the form to their
Initial states.
• Submit button
-The form data is coded into a query string,
Which is sent to the server.
-The server is requested to execute the CGI
Program specified in the Action attribute of
<Form> tag. Sample rest.html
5.3.3 a complete Form
Example
• Page 145-147
• You now have the basis for a discussion
The coding of form data (into a query
String) and a CGI-perl program that
Decodes that data, processes it, and
Sends a response back to the user
Concerning the order.
Popcorn. hmtl
5.4 query string format
• This section describes the Encoding
Format a query string. When the submit
Button in a form is clicked, the form's data
Is coded and sent to the server.
• For each widget in the form that has
Value, the widget's name and that value
Are coded as an assignment statement
And sorted ded in the query string.
• It is coded in the query string as follows:
Payment = discover
5.4 query string format
• If the form has more than one widget,
Assignments that code their values in the query
String are separated by ampersands (&):
Caramel = 7 & payment = discover
• If there are special characters in the value of
Widget, they are coded as a percent sign (%)
Followed by a two-character hexadecimal
Number that is the ASCII code for the character.
Payment = visa & saying = eat % 20 your % 20 fruit % 21
5.4 query string format
• A cgi program that processes a query string
Must first get the string into a scalar variable.
CGI programs shocould be able to handle the get
And the post methods, so the Web document
Designer can change his or her mind about
Which one to use at any time in the life time
The CGI program, without needing to change that
Program.
• The environment variable request_method
Has either get or post as its value. So,
CGI program checks this variable and decides
How to get the query string.
5.5 decoding the query
String
• If get was used, the server removes
Query string from the URL and places it in
The Environment Variable
QUERY_STRING.
• If post was used, the READ function is
Used to read the query string from stdin,
Using the environment variable
Content_length as the number
Bytes to be read.
5.5 decoding the query
String
$ Request_method = $ ENV {'request _ method '};
If ($ request_method EQ "get "){
$ QUERY_STRING = $ ENV {'query _ string '};
}
Elsif ($ request_method EQ "Post "){
Read (stdin, $ QUERY_STRING,
$ ENV {'content _ length '});
}
Else {
Print "error-the request method is illegal/N ";
Exit (1 );
}
1st step
5.5 decoding the query
String
@ Name_value_pairs = Split (//, $ QUERY_STRING );
Foreach $ name_value (@ name_value_pairs ){
($ Name, $ value) = Split (/=/, $ name_value );
......
}
$ Value = ~ TR/+ //;#
$ Value = ~ S/% ([/DA-fa-F] [/DA-fa-f])/Pack ("C", Hex ($1)/EG;
2nd step
5.6 an example of Form
Processing
• You now can consider the CGI program
That will not just read and display
Names and values in the query string,
Also process the data from the popcorn
Form.
• Page 153-157 process. pl
5.7 The CGI. PM module
• Much of what a CGI program must do is
Routine-that is, it is nearly the same
All CGI programs. Therefore, it is natural
Have standard routines to do these things.
CGI. PM, which was developed by Lincoln
Stein, is a Perl module of functions
These common tasks.
• CGI. PM which was developed by Lincoln
Stein, is a Perl module of functions
These common tasks.
5.7 The CGI. PM module
• A Perl Program specifies that it needs
Access to a special module with the use
Declaration. In the case of CGI. PM, only
Part of the module is usually needed. This
Can be requested in the use declaration
With a list of module part names each
Proceeded by a colon (:). The part you
Need here, which is the most often used
Part is named standard.
Use cgi qw (: Standard) cgipm. pl
5.7 The CGI. PM module
• Functions of the functions in CGI. PM produce
HTML tages. In these cases, the functions
Have the names of their associated html
Tags, counter t that they usually use only
Lowercase letters. These functions are
Called shortcuts.
• P;