Perl CGI Programming
What is CGI
CGI is currently maintained by NCSA, which defines CGI as follows:
CGI (Common Gateway Interface), the Common Gateway Interface, is a program that runs on the server, such as an HTTP server, providing an interface with client HTML pages.
Web Browsing
To better understand how CGI works, we can follow the process of clicking a link or URL on a webpage:
- Use your browser to access the URL and connect to the HTTP web server.
- The web server receives the request message, parses the URL, and checks if the accessed file exists on the server. If it exists, the file content is returned; otherwise, an error message is returned.
- The browser receives information from the server and displays the received file or error message.
CGI programs can be Python scripts, PERL scripts, SHELL scripts, C or C++ programs, etc.
CGI Architecture Diagram
#!/usr/bin/perl
local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read text information
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "POST")
{
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
} else {
$buffer = $ENV{'QUERY_STRING'};
}
# Read name/value pair information
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
$name = $FORM{name};
$url = $FORM{url};
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print '<meta charset="utf-8">';
print '<title>tutorialpro.org(tutorialpro.org)</title>';
print "</head>";
print "<body>";
print "<h2>$name Website: $url</h2>";
print "</body>";
print "</html>";
1;
Here is an HTML form using the GET method to send two pieces of data to the server, with the server script also being the test.cgi file. The test.html code is as follows:
test.html Code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</title>
</head>
<body>
<form action="/cgi-bin/test.cgi" method="post">
Site Name: <input type="text" name="name"> <br />
Site URL: <input type="text" name="url" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
In the browser, the execution effect is as follows:
Passing Checkbox Data via CGI Program
Checkbox is used to submit one or multiple option data. The test.html code is as follows:
test.html Code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</title>
</head>
<body>
<form action="/cgi-bin/test.cgi" method="POST" target="_blank">
<input type="checkbox" name="tutorialpro" value="on" /> tutorialpro.org
<input type="checkbox" name="google" value="on" /> Google
<input type="submit" value="Select Site" />
</form>
</body>
</html>
The following is the code for the test.cgi file:
test.cgi Code
#!/usr/bin/perl
local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read information
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "POST")
{
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
} else {
$buffer = $ENV{'QUERY_STRING'};
}
# Read name/value pair information
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
if( $FORM{tutorialpro} ){
$tutorialpro_flag ="ON";
} else {
$tutorialpro_flag ="OFF";
}
if( $FORM{google} ){
$google_flag ="ON";
} else {
$google_flag ="OFF";
}
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print '<meta charset="utf-8">';
print '<title>tutorialpro.org(tutorialpro.org)</title>';
print "</head>";
print "<body>";
print "<h2> tutorialpro.org Check Status : $tutorialpro_flag</h2>";
print "<h2> Google Check Status : $google_flag</h2>";
print "</body>";
print "</html>";
1;
In the browser, the execution effect is as follows:
Passing Radio Data via CGI Program
Radio only passes one piece of data to the server. The test.html code is as follows:
test.html Code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</title>
</head>
<body>
<form action="/cgi-bin/test.cgi" method="post" target="_blank">
<input type="radio" name="site" value="tutorialpro" /> tutorialpro.org
<input type="radio" name="site" value="google" /> Google
<input type="submit" value="Submit" />
</form>
</body>
</html>
The test.cgi script code is as follows:
test.cgi Code
#!/usr/bin/perl
local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read information
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "POST")
{
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
} else {
$buffer = $ENV{'QUERY_STRING'};
}
# Read name/value pair information
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
$site = $FORM{site};
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print '<meta charset="utf-8">';
print '<title>tutorialpro.org(tutorialpro.org)</title>';
print "</head>";
print "<body>";
print "<h2> Selected Website $site</h2>";
print "</body>";
print "</html>";
1;
In the browser, the execution effect is as follows:
Passing Textarea Data via CGI Program
Textarea passes multi-line data to the server. The test.html code is as follows:
test.html Code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</title>
</head>
<body>
<form action="/cgi-bin/test.cgi" method="post" target="_blank">
<textarea name="textcontent" cols="40" rows="4">
Enter content here...
</textarea>
<input type="submit" value="Submit" />
</form>
</body>
</html>
The test.cgi script code is as follows:
test.cgi Code
#!/usr/bin/perl
local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read information
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "POST")
{
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
} else {
$buffer = $ENV{'QUERY_STRING'};
}
# Read name/value pair information
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
$text_content = $FORM{textcontent};
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print '<meta charset="utf-8">';
print '<title>tutorialpro.org(tutorialpro.org)</title>';
print "</head>";
print "<body>";
print "<h2>Entered text content is: $text_content</h2>";
print "</body>";
print "</html>";
1;
In the browser, the execution effect is as follows:
Passing Drop-down Data via CGI Program
The HTML drop-down box code is as follows:
test.html Code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>tutorialpro.org(tutorialpro.org)</title>
</head>
<body>
<form action="/cgi-bin/test.cgi" method="post" target="_blank">
<select name="dropdown">
<option value="tutorialpro" selected>tutorialpro.org</option>
<option value="google">Google</option>
</select>
<input type="submit" value="Submit" />
</form>
</body>
</html>
The test.cgi script code is as follows:
test.cgi Code
#!/usr/bin/perl
local ($buffer, @pairs, $pair, $name, $value, %FORM);
# Read information
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
if ($ENV{'REQUEST_METHOD'} eq "POST")
{
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
} else {
$buffer = $ENV{'QUERY_STRING'};
}
# Read name/value pair information
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%(..)/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}
$dropdown = $FORM{dropdown};
print "Content-type:text/html\r\n\r\n";
print "<html>";
print "<head>";
print '<meta charset="utf-8">';
print '<title>tutorialpro.org(tutorialpro.org)</title>';
print "</head>";
print "<body>";
print "<h2>Selected option: $dropdown</h2>";
print "</body>";
print "</html>";
1;
In the browser, the execution effect is as follows:
<meta charset="utf-8"> <title>tutorialpro.org(tutorialpro.org)</title> </head> <body> <form action="/cgi-bin/test.cgi" method="post" target="_blank"> <select name="dropdown"> <option value="tutorialpro" selected>tutorialpro.org</option> <option value="google">Google</option> </select> <input type="submit" value="Submit"/> </form> </body> </html>
test.cgi script code is as follows:
## test.cgi Code
!/usr/bin/perl
local ($buffer, @pairs, $pair, $name, $value, %FORM);
Read information
$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/; if ($ENV{'REQUEST_METHOD'} eq "POST") { read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); }else { $buffer = $ENV{'QUERY_STRING'}; }
Read name/value pairs information
@pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%(..)/pack("C", hex($1))/eg; $FORM{$name} = $value; } $site = $FORM{dropdown};
print "Content-type:text/html\r\n\r\n"; print "<html>"; print "<head>"; print '<meta charset="utf-8">'; print '<title>tutorialpro.org(tutorialpro.org)</title>'; print "</head>"; print "<body>"; print "<h2>Selected website is: $site</h2>"; print "</body>"; print "</html>";
1; ```
In the browser, the execution effect is as follows: