Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Perl CGI Programming

Perl CGI Programming


What is CGI

CGI is currently maintained by NCSA, which defines CGI as follows:

CGI (Common Gateway Interface) is a program running on a server such as an HTTP server, providing an interface with the client's HTML pages.


Web browsing

To better understand how CGI works, we can follow the flow of clicking a link or URL on a web page:

A CGI program can be a Python script, PERL script, SHELL script, C or C++ program, and so on.


CGI architecture diagram

cgiarch


Web server support and configuration

Before you start CGI programming, make sure your web server supports CGI and has a CGI handler configured.

Apache CGI support configuration:

Set up the CGI directory:



ScriptAlias /cgi-bin/ /var/www/cgi-bin/

All HTTP servers store CGI programs in a preconfigured directory. This directory is called the CGI directory and, by convention, is named /var/www/cgi-bin.

The extension for CGI files is .cgi, and Perl can also use the .pl extension.

By default, the cgi-bin directory on Linux servers is configured under /var/www.

If you want to specify another directory for running CGI scripts, modify the httpd.conf configuration file as shown below:





   AllowOverride None

   Options +ExecCGI

   Order allow,deny

   Allow from all



Add the .pl suffix in AddHandler so that we can access Perl script files ending in .pl:



AddHandler cgi-script .cgi .pl .py

Your First CGI Program

Below we create a test.cgi file, with the following code:

test.cgi code

#!/usr/bin/perl

print

"

Content-type:text/html

\r

\n

\r

\n

"

;

print

'

'

;

print

'

'

;

print

'

'

;

print

'

Runoob Tutorial(runoob.com)

'

;

print

'

'

;

print

'

'

;

print

'

Hello Word!

'

;

print

'

The first CGI program from Runoob Tutorial.

'

;

print

'

'

;

print

'

'

;

1

;

Then open http://localhost/cgi-bin/test.cgi in the browser, and the output is as follows:

The output content on the first line of the script, "Content-type:text/html\r\n\r\n", is sent to the browser and tells the browser that the content type to display is "text/html".


HTTP headers

The "Content-type:text/html" in the test.cgi file content is part of the HTTP header; it is sent to the browser to tell the browser the content type of the file.

The HTTP header format is as follows:



HTTP 字段名: 字段内容

For example:



Content-type:text/html\r\n\r\n

The following table describes HTTP header information commonly used in CGI programs:

Header Description
Content-type: The MIME information corresponding to the entity in the request. For example: Content-type:text/html
Expires: Date The date and time when the response expires
Location: URL Used to redirect the recipient to a location other than the requested URL to complete the request or identify a new resource
Last-modified: Date The last modification time of the requested resource
Content-length: N The content length of the request
Set-Cookie: String Set an HTTP Cookie

CGI Environment Variables

All CGI programs receive the following environment variables, which play an important role in CGI programs:

Variable Name Description
CONTENT_TYPE The value of this environment variable indicates the MIME type of the information being passed. Currently CONTENT_TYPE is generally application/x-www-form-urlencoded, indicating that the data comes from an HTML form.
CONTENT_LENGTH If the server transfers information to the CGI program using POST, this environment variable is the number of bytes of valid data that can be read from standard input STDIN. This environment variable must be used when reading the input data.
HTTP_COOKIE The COOKIE contents on the client.
HTTP_USER_AGENT Provides client browser information including the version number or other proprietary data.
PATH_INFO The value of this environment variable is the additional path information immediately following the CGI program name. It often appears as a parameter of the CGI program.
QUERY_STRING If the server passes information to the CGI program using GET, the value of this environment variable is the information being passed. It follows the CGI program name, separated by a question mark '?'.
REMOTE_ADDR The value of this environment variable is the IP address of the client sending the request, such as 192.168.1.67 above. This value is always present, and it is the only identifier the web client needs to provide to the web server, so it can be used in CGI programs to distinguish different web clients.
REMOTE_HOST The value of this environment variable contains the host name of the client sending the CGI request. If the lookup is not supported, this environment variable need not be defined.
REQUEST_METHOD Provides the method by which the script was invoked. For scripts using the HTTP/1.0 protocol, only GET and POST are meaningful.
SCRIPT_FILENAME The full path of the CGI script
SCRIPT_NAME The name of the CGI script
SERVER_NAME This is the host name, alias, or IP address of your WEB server.
SERVER_SOFTWARE The value of this environment variable contains the name and version of the HTTP server invoking the CGI program. For example, the value above is Apache/2.2.14(Unix)

Below is a simple CGI script that outputs the CGI environment variables:

Example

#!/usr/bin/perl

print

"

Content-type: text/html

\n

\n

"

;

print

'

'

;

print

"

Environment variables:

\n

"

;

foreach

(

sort

keys

%ENV

)

{

print

"

$_

:

$ENV

{

$_

}

\n

"

;

}

1

;


File Download

If we want to implement file downloads through Perl CGI, we need to set different header information, as shown below:

Example

#!/usr/bin/perl

HTTP Header

print

"

Content-Type:application/octet-stream; name=

"

FileName

"

\r

\n

"

;

print

"

Content-Disposition: attachment; filename=

"

FileName

"

\r

\n

\n

"

;

Actual File Content will go hear.

open

(

FILE

,

"

"

)

;

while

(

read

(

FILE

,

$buffer

,

100

)

)

{

print

(

"

$buffer

"

)

;

}


Transferring data with the GET method

The GET method sends encoded user information to the server; the data is included in the URL of the requested page, separated by a "?" as shown below:



http://www.test.com/cgi-bin/test.cgi?key1=value1&key2=value2

Some other notes about GET requests:

Simple URL example: GET method

The following is a simple URL that uses the GET method to send two parameters to the test.cgi program:



/cgi-bin/test.cgi?name=Runoob Tutorial&url=http://www.runoob.com

The following is the code for the test.cgi file:

Example

#!/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

"

GET

"

)

{

$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

"

"

;

print

"

"

;

print

'

'

;

print

'

Runoob Tutorial(runoob.com)

'

;

print

"

"

;

print

"

"

;

print

"

$name

URL:

$url

"

;

print

"

"

;

print

"

"

;

1

;

View in the browser, the output is as follows:

Simple form example: GET method

The following is an HTML form that uses the GET method to send two pieces of data to the server; the server script submitted to is also the test.cgi file. The test.html code is as follows:

test.html file code

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>

Runoob Tutorial(runoob.com)

title

head

<body>
<form action="/cgi-bin/test.cgi" method="get">

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 result is as follows:


Passing data with the POST method

Using the POST method to pass data to the server is safer and more reliable; sensitive information such as user passwords should be sent with POST.

The following is also test.cgi, which can also handle POST form data submitted by the browser:

test.cgi code

#!/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

"

"

;

print

"

"

;

print

'

'

;

print

'

Runoob Tutorial(runoob.com)

'

;

print

"

"

;

print

"

"

;

print

"

$name

URL:

$url

"

;

print

"

"

;

print

"

"

;

1

;

The following is an HTML form that uses the GET method to send two pieces of data to the server; the server script submitted to is also the test.cgi file. The test.html code is as follows:

test.html code

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>

Runoob Tutorial(runoob.com)

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 result is as follows:

Passing checkbox data through a CGI program

checkbox is used to submit one or more option values; the test.html code is as follows:

test.html code

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>

Runoob Tutorial(runoob.com)

title

head

<body>
<form action="/cgi-bin/test.cgi" method="POST" target="_blank">
<input type="checkbox" name="runoob" value="on" />

Runoob Tutorial

<input type="checkbox" name="google" value="on" />Google>
<input type="submit" value=">

Select website

"

/>

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

{

runoob

}

)

{

$runoob_flag

=

"

ON

"

;

}

else

{

$runoob_flag

=

"

OFF

"

;

}

if

(

$FORM

{

google

}

)

{

$google_flag

=

"

ON

"

;

}

else

{

$google_flag

=

"

OFF

"

;

}

print

"

Content-type:text/html

\r

\n

\r

\n

"

;

print

"

"

;

print

"

"

;

print

'

'

;

print

'

Runoob Tutorial(runoob.com)

'

;

print

"

"

;

print

"

"

;

print

"

Runoob Tutorial selected state:

$runoob_flag

"

;

print

"

Google selected state:

$google_flag

"

;

print

"

"

;

print

"

"

;

1

;

In the browser, the execution result is as follows:


Passing radio data through a CGI program

Radio passes only 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>

Runoob Tutorial(runoob.com)

title

head

<body>
<form action="/cgi-bin/test.cgi" method="post" target="_blank">
<input type="radio" name="site" value="runoob" />

Runoob Tutorial

<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

"

"

;

print

"

"

;

print

'

'

;

print

'

Runoob Tutorial(runoob.com)

'

;

print

"

"

;

print

"

"

;

print

"

Selected website

$site

"

;

print

"

"

;

print

"

"

;

1

;

In the browser, the execution result is as follows:


Passing textarea data through a CGI program

Textarea passes multiple lines of data to the server; the test.html code is as follows:

test.html code

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>

Runoob Tutorial(runoob.com)

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

"

"

;

print

"

"

;

print

'

'

;

print

'

Runoob Tutorial(runoob.com)

'

;

print

"

"

;

print

"

"

;

print

"

The entered text content is:

$text_content

"

;

print

"

"

;

print

"

"

;

1

;

In the browser, the execution result is as follows:


Passing drop-down data through a CGI program

The HTML dropdown code is as follows:

test.html code

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>

Runoob Tutorial(runoob.com)

title

head

<body>
<form action="/cgi-bin/test.cgi" method="post" target="_blank">
<select name="dropdown">
<option value="runoob" selected>

Runoob Tutorial

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

;

}

$site

=

$FORM

{

dropdown

}

;

print

"

Content-type:text/html

\r

\n

\r

\n

"

;

print

"

"

;

print

"

"

;

print

'

'

;

print

'

Runoob Tutorial(runoob.com)

'

;

print

"

"

;

print

"

"

;

print

"

The selected website is:

$site

"

;

print

"

"

;

print

"

"

;

1

;

In the browser, the execution result is as follows:


Using cookies in CGI

A major drawback of the http protocol is that it does not identify users, which causes great inconvenience for programmers; the cookie feature was introduced to make up for this.

A cookie is data written to the client's hard disk through the client's browser while the client visits a script; when the client visits the script again, the data is retrieved, making identity determination possible. Cookies are often used for authentication.

Cookie syntax

http cookies are sent through http headers, before the file is transferred. The syntax of the Set-Cookie header is as follows:



Set-cookie:name=name;expires=date;path=path;domain=domain;secure 


Setting cookies

Setting cookies is very simple; cookies are sent separately in the HTTP header. The following example sets UserID, Password, and expires in a cookie:

Example

#!/usr/bin/perl

print

"

Set-Cookie:UserID=XYZ;

\n

"

;

print

"

Set-Cookie:Password=XYZ123;

\n

"

;

print

"

Set-Cookie:Expires=Tuesday, 31-Dec-2017 23:12:40 GMT

"

;\

n

"

; print

"

Set

Cookie

:

Domain

=

www

.

runoob

.

com

;\

n

"

; print

"

Set

Cookie

:

Path

=/

perl

;\

n

"

; print

"

Content

type

:

text

/

html

\

r

\

n

\

r

\

n

"

; ...........other HTML content

Find Cookie

Retrieving cookie information is also very simple: cookie information is stored in the CGI environment variable HTTP_COOKIE, in the following format:

Example

#!/usr/bin/perl

$rcvd_cookies

=

$ENV

{

'

HTTP_COOKIE

'

}

;

@cookies

=

split

/

;

/

,

$rcvd_cookies

;

foreach

$cookie

(

@cookies

)

{

(

$key

,

$val

)

=

split

(

/=/,

$cookie

)

;

splits on the first =.

$key

=~

s

/^\

s

+//;

$val

=~

s

/^\

s

+//;

$key

=~

s

/\

s

+$//;

$val

=~

s

/\

s

+$//;

if

(

$key

eq

"

UserID

"

)

{

$user_id

=

$val

;

}

elsif

(

$key

eq

"

Password

"

)

{

$password

=

$val

;

}

}

print

"

User ID =

$user_id

\n

"

;

print

"

Password =

$password

\n

"

;

The output of the above example is:



User ID = XYZ

Password = XYZ123


CGI modules

Perl provides many built-in CGI modules; the following two are commonly used: