 |
eKit: Essential HP Solutions for Your Data Center
Data protection and disaster recovery tools help keep data secure and available under the worst of circumstances.
Download this eKit and get:
eBook: Guide to Storage Networking
eBook: Storage Networking 2, Configuration and Planning
Whitepaper: Storage Management Costs in the Enterprise: A Comparison of Mid-Range Array Solutions
Whitepaper: Virtualization - It's Not Just for Enterprises Anymore
Whitepaper: Continuous Real-time Data Protection and Disaster Recovery
Click Here!
|
 |
|
|
|
|
|
CGI: Input
CGI scripts get their input mainly from
environment variables and
standard input (when using the POST method).
These environment variables are set when the server executes the
gateway program.
The environment variable QUERY_STRING is
everything that follows the question mark in the URL.
This information could be added either by an ISINDEX document,
or by a form using the
GET method.
In the
POST method,
form data are read from stdin.
The server will NOT send you an EOF on the end of the data,
instead you should use the environment variable
CONTENT_LENGTH to determine how much data you
should read from stdin.
$method = $ENV{'REQUEST_METHOD'};
# Get the query string, & decode it.
if ( $method eq 'POST' ) {
# The POST method means that the form data
# are coming in through standard input.
read(STDIN, $_, $ENV{'CONTENT_LENGTH'});
}
elsif ( $method eq 'GET' ) {
# The GET method means that the form data
# are coming in through an environment
# variable.
$_ = $ENV{'QUERY_STRING'};
}
else {
print "UnExpected method: $method";
exit (1);
}
# Data have been presented as text between
# ampersands, in the form name=value
foreach $_ (split(/&/)) {
# This string is encoded in the standard
# format of changing spaces to +, and
# encoding special characters with %xx
# hexadecimal encoding.
# Convert plus's to spaces
$in[$i] =~ s/\+/ /g;
# Split into key and value.
($key, $val) = split(/=/,$in[$i],2);
# Convert %XX from hex numbers to
# alphanumeric
$key =~ s/%(..)/pack("c",hex($1))/ge;
$val =~ s/%(..)/pack("c",hex($1))/ge;
# Associate key and value
$in{$key} .= "\0" if (defined($in{$key}));
$in{$key} .= $val;
}
Note that the last 2 lines of code append values having the same
name. This may happen, for example, when the HTML form contains
multiple OPTIONs in a SELECT tag, and the user
selected more than one. Or when the user selects multiple radio
buttons. Or when some of the form elements have the same name (unusual,
but not impossible). So the script will have to know about these cases,
and separate out multiple values, e.g. using split('\0').
Normally you will not write code such as the above into every CGI
program, but use one of the libraries.
A very common approach is to use ReadParse from
cgi-lib.pl. Let us suppose we wanted to echo the values
selected from the following form:
The following code is a (barebones) way to do it:
#!/bin/perl
require '/home/web/public_html/Software/Perl/cgi-lib.pl';
&ReadParse ;
print "Content-type: text/html\n\n";
foreach (split('\0',$in{'options'})) {
print "$_\n";
}
|