Perl CGI Programming
What is CGI
CGI is currently maintained by NCSA. NCSA defines CGI as follows:
CGI (Common Gateway Interface), the general gateway interface, is a program that runs on a server such as an HTTP server and provides an interface to client HTML pages.
Web Browsing
To better understand how CGI works, we can start from the process of clicking a link or URL on a web page:
- 1. Use your browser to access the URL and connect to the HTTP web server.
- 2. After receiving the request, the web server parses the URL and checks whether the requested file exists on the server. If it exists, it returns the content of the file; otherwise, it returns an error message.
- 3. The browser receives the information from the server and displays the received file or the error message.
CGI programs can be Python scripts, Perl scripts, Shell scripts, C or C++ programs, etc.
CGI Architecture Diagram

Web Server Support and Configuration
Before you start CGI programming, make sure your web server supports CGI and has the CGI handler configured.
Apache CGI Configuration:
Set up the CGI directory:
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
All CGI programs executed by HTTP servers are stored in a preconfigured directory. This directory is called the CGI directory, and by convention it is named /var/www/cgi-bin.
CGI files have the extension .cgi. Perl can also use the .pl extension.
By default, Linux servers are configured with /var/www as the cgi-bin directory for running CGI scripts.
If you want to specify another directory for running CGI scripts, you can modify the httpd.conf configuration file as follows:
<Directory "/var/www/cgi-bin"> AllowOverride None Options +ExecCGI Order allow,deny Allow from all </Directory>
Add the .pl suffix to AddHandler so that we can access Perl script files ending with .pl:
AddHandler cgi-script .cgi .pl .py
First CGI Program
Below we create a test.cgi file with the following code:
test.cgi Code
Then open http://localhost/cgi-bin/test.cgi in a browser, and the output is as follows:

The output 'Content-type:text/html\r\n\r\n' on the first line of the script is sent to the browser to inform the browser that the content type to display is 'text/html'.
HTTP Header
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 it the content type of the file.
The format of the HTTP header is as follows:
HTTP 字段名: 字段内容
For example:
Content-type:text/html\r\n\r\n
The following table describes the information frequently used in HTTP headers in CGI programs:
| head | Description |
|---|---|
| Content-type: | MIME information corresponding to the requested entity. For example: Content-type:text/html |
| Expires: Date | The date and time at which the response expires |
| Location: URL | Used to redirect the receiver 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 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 transmitted information. Currently, the environment variable CONTENT_TYPE is generally: application/x-www-form-urlencoded, which indicates that the data comes from an HTML form. |
| CONTENT_LENGTH | If the transfer method between the server and the CGI program is POST, this environment variable is the number of valid data bytes that can be read from standard input STDIN. This environment variable must be used when reading the input data. |
| HTTP_COOKIE | The COOKIE content in the client. |
| HTTP_USER_AGENT | Provides client browser information containing version numbers or other proprietary data. |
| PATH_INFO | The value of this environment variable represents other path information immediately following the CGI program name. It often appears as a parameter to the CGI program. |
| QUERY_STRING | If the transfer method between the server and the CGI program is GET, the value of this environment variable is the transferred information. This information 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 making the request, for example 192.168.1.67 above. This value is always present. It is also the unique identifier that a web client must provide to the web server, and it can be used in CGI programs to distinguish different web clients. |
| REMOTE_HOST | The value of this environment variable contains the hostname of the client that sent the CGI request. If the server does not support the lookup you want, this environment variable need not be defined. |
| REQUEST_METHOD | Provides the method by which the script was called. For scripts using the HTTP/1.0 protocol, only GET and POST are meaningful. |
| SCRIPT_FILENAME | The full path to the CGI script |
| SCRIPT_NAME | The name of the CGI script |
| SERVER_NAME | This is the hostname, alias, or IP address of your web server. |
| SERVER_SOFTWARE | The value of this environment variable contains the name and version number of the HTTP server that invoked the CGI program. For example, the value above is Apache/2.2.14(Unix). |
The following is a simple CGI script that outputs CGI environment variables:
Example
File Download
If we want to implement file download through Perl CGI, we need to set different header information, as follows:
Example
Using GET Method to Transfer Data
The GET method sends encoded user information to the server. The data is included in the URL of the requested page, separated by a '?' sign, as shown below:
http://www.test.com/cgi-bin/test.cgi?key1=value1&key2=value2Some other notes about GET requests:
- GET requests can be cached
- GET requests remain in the browser history
- GET requests can be bookmarked
- GET requests should not be used when processing sensitive data
- GET requests have a length limit
- GET requests should only be used to retrieve data
Simple URL Example: GET Method
The following is a simple URL that sends two parameters to the test.cgi program using the GET method:
/cgi-bin/test.cgi?name=Example&url=http://www.example.com
The following is the code for the test.cgi file:
Example
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 data items to the server. The submitted server script is also the test.cgi file. The test.html code is as follows:
test.html File Code
In the browser, the execution effect is as follows:

Using POST Method to Pass Data
Using the POST method to pass data to the server is safer and more reliable. Sensitive information such as user passwords needs to be transmitted using POST.
The following is also test.cgi; it can also handle POST form data submitted by the browser:
test.cgi Code
The following is an HTML form that uses the GET method to send two data items to the server. The submitted server script is also the test.cgi file. The test.html code is as follows:
test.html Code
In the browser, the execution effect is as follows:
Passing Checkbox Data via CGI Program
Checkbox is used to submit one or more option data. The test.html code is as follows:
test.html Code
The following is the code of the test.cgi file:
test.cgi Code
In the browser, the execution effect is as follows:
Passing Radio Data via CGI Program
Radio only sends one data item to the server. The test.html code is as follows:
test.html Code
The test.cgi script code is as follows:
test.cgi Code
In the browser, the execution effect is as follows:

Passing Textarea Data via CGI Program
Textarea sends multiple lines of data to the server. The test.html code is as follows:
test.html Code
The test.cgi script code is as follows:
test.cgi Code
In the browser, the execution effect is as follows:

Passing Dropdown Data via CGI Program
The HTML dropdown box code is as follows:
test.html Code
The test.cgi script code is as follows:
test.cgi Code
In the browser, the execution effect is as follows:

Using Cookies in CGI
A major disadvantage of the HTTP protocol is that it does not judge user identity, which brings great inconvenience to programmers. The emergence of cookie functionality makes up for this shortcoming.
A cookie is when a client visits a script, and through the client's browser, record data is written to the client's hard disk. When the client accesses the script next time, the data information is retrieved, thereby achieving the function of identity determination. Cookies are commonly used in identity verification.
Cookie Syntax
HTTP cookies are sent through the HTTP header, which occurs before the file transfer. The syntax of the Set-Cookie header is as follows:
Set-cookie:name=name;expires=date;path=path;domain=domain;secure
- name=name:Need to set the cookie value (name cannot use ";",symbol), when there are multiple name values, use ";" to separate, for example:name1=name1;name2=name2;name3=name3。
- expires=date:Cookie validity period, format: expires="Wdy,DD-Mon-YYYY HH:MM:SS"
- path=path: Set the path supported by the cookie. If path is a path, the cookie takes effect for all files and subdirectories under this directory, for example: path="/cgi-bin/". If path is a file, the cookie only takes effect for this file, for example: path="/cgi-bin/cookie.cgi".
- domain=domain:The domain name for which the cookie is valid, for example: domain="www.example.com"
- secure:If this flag is given, it means the cookie can only be transmitted through an HTTPS server using the SSL protocol.
- Cookie reception is implemented by setting the environment variable HTTP_COOKIE. CGI programs can retrieve cookie information by querying this variable.
Setting Cookies
Setting cookies is very simple; cookies are sent separately in the HTTP header. The following example sets UserID, Password, and expires in the cookie:
Example
Retrieving Cookies
The cookie information retrieval page is very simple. Cookie information is stored in the CGI environment variable HTTP_COOKIE, in the following format:
Example
The output result of the above example is:
User ID = XYZ Password = XYZ123
CGI Module
Perl provides many built-in CGI modules, the following two are commonly used:
Other Extensions