CGI Programming Part 2

advertisement
CGI Programming Part 2
Input Tags
• Many different ways of getting data from
the user.
• The <input> tag is used most often.
• <input> has a type attribute
– Specifies the method with which to get data
from the user.
Other Attributes of <input>
•
•
•
•
name – name of an input field.
value – value of an input field.
size – width of an input field.
maxlength – maximum number of
characters that can be entered in an input
field.
• checked – whether a radio or checkbox is
“turned on”.
• src – url of an image.
Input Type: Text
• Setting the type to text creates a text box.
• For example,
<input type=“text”
name=“street”
size = 30>
• Note, name does not appear in the webpage.
• It is seen only by the CGI program.
Input Type: Radio
• Setting the type to radio creates a radio
button.
• For example,
<input type=“radio” name=“title” value=“ms” checked> Ms. <br>
<input type=“radio” name=“title” value=“mrs”> Mrs. <br>
<input type=“radio” name=“title” value=“mr”> Mr. <br>
<input type=“radio” name=“title” value=“dr”> Dr. <br>
• All related radio buttons have the same
name.
• The values are all preset.
Input Type: Submit and Reset
• submit creates a submit button to contact a
CGI program.
• For example,
<input type=“submit” value=“Submit Form”>
• reset creates a button to erase all values in a
form.
• For example,
<input type=“reset” value=“Clear Form”>
• The “value” appears in the button.
Input Type: Others
• checkbox – creates a checkbox
• password – creates a password field. A text
field with characters displayed as ****.
• button – creates a button the user can press.
• file – creates a field to upload a file.
• image – creates an image the user can click
to submit a form.
Inputs Other Than <input>
• <textarea> - multi-line text field. You can
specify the rows and cols attributes.
• <select> - creates a drop down menu.
Handling the Input
• Input is sent to the CGI program specified
in the <form> tag using either the get or
post method.
• It is best to write CGI programs that handle
both.
• Can be done by examining the environment
variable, REQUEST_METHOD.
An Example
$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’});
}
CGI.pm
• Many parts of CGI programs are repeated.
– Makes sense to capture these parts for reuse.
• CGI.pm is a module of Perl functions to do
many common tasks.
• Many functions provide shortcuts to create
HTML.
• Module can be used as a collection of
functions or an object oriented class.
Object-Oriented Use
• Can be used as an object-oriented class by:
use CGI;
…
$q = new CGI;
• Access subroutines as methods of $q
• For example,
print $q ->start_html();
Function-Oriented Use
• Import a set of functions to be called directly.
– non-object oriented is often faster than object-oriented.
• Use a quoted list to tell Perl which functions to
import.
use CGI (“start_html”, “end_html”, “header”);
• CGI.pm also defines sets of these functions.
‘standard’ and ‘html3’ are the most common.
use CGI qw(:standard :html3);
header
• The header function creates the HTTP
header.
• The default is “text/html”.
• For example,
print header();
creates
print “Content-type: text/html\n\n”;
start_html
• start_html create the HTML header
• start_html can take one parameter, the title.
• For example,
print start_html(‘My Home Page’);
creates
<HTML>
<HEAD>
<TITLE> My Home Page </TITLE>
</HEAD>
<BODY>
start_html (cont.)
• start_html can also take named parameters.
• Specify attributes to give to the <body> tag.
• For example,
print start_html (-title => “My Title”,
-bgcolor => “Red”);
creates
<html>
<head>
<title> My Title </title>
</head>
<body bgcolor = “Red”>
end_html
• end_html creates the ending part of an
HTML document.
• For example,
print end_html();
creates
</body>
</html>
param
• param gets the parameter values sent to a
CGI program.
• Query String:
name=Max%20Powers&job=ceo
• Can get the query values by:
$name = param(“name”);
$job = param(“job”);
• This is the scalar context.
Contexts of param
• Can be called in list context with no
arguments. Returns names of all parameters.
– e.g. @params = param();
• Can be called in list context with an
argument. Returns an array of all values for
that argument.
– e.g. @vals = param(“title”);
Locking Files
• Many webpages allow multiple users to
access the same information.
– i.e. multiple user writing to the same file.
• Need a mechanism to ensure consistency.
– Do not want one user’s action to undo the
actions of another.
• A file must be locked when being written to.
flock
• flock can be used to lock a file.
• The syntax is
flock (filehandle, mode);
• Set mode to
– 2 to lock a file.
– 8 to unlock a file.
• For example,
flock (FILE, 2);
Debugging
• Debugging a CGI script can be frustrating.
• Browser will display “500 Internal Server
Error” if any errors occur.
• Some useful tips:
– Run the program from the command line.
– Use perl –c <filename> to see if file compiles.
Download