0% found this document useful (0 votes)
1K views10 pages

Understanding Common Gateway Interface

Common Gateway Interface (CGI) is a standard for exchanging data between web servers and custom scripts. It allows web servers to interact with users by processing data submitted through HTML forms. CGI scripts are written in languages like C, Perl, and Python. They run on the server and return dynamic content to users by accessing databases or other sources of information.

Uploaded by

pc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views10 pages

Understanding Common Gateway Interface

Common Gateway Interface (CGI) is a standard for exchanging data between web servers and custom scripts. It allows web servers to interact with users by processing data submitted through HTML forms. CGI scripts are written in languages like C, Perl, and Python. They run on the server and return dynamic content to users by accessing databases or other sources of information.

Uploaded by

pc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1

What is Common Gateway Interface?

Common Gateway Interface (CGI) is a set of standards used for running scripts
and programs on a web server. As CGI is a standard interface, the
communication between the web server and the client’s web browser follows specified
rules. In other words, the Common Gateway Interface acts as a middleware between
WWW servers and external databases and information sources. CGI was described by
the WWW consortium, which also described the way the program interacts with a
HyperText Transfer Protocol (HTTP) server. The scripts are written in PHP and ASP
and processed on the web server before the page gets loaded and the result is sent to the
client’s browser.

The Common Gateway Interface (CGI) is described as a set of rules for exchanging
information between a web server and a custom script. CGI is one of the most common
ways for web servers to interact with users by sending the data. The programming
languages used can be C, C++, Java, Perl, Python, or VB (Visual Basic).
2

Several HTML pages consist of forms, which use CGI programs to process the data
available in forms. Scripts or programs are considered to be an alternative method used
to provide feedback to the web users, while these operate from the user’s machine
instead of the Web server and make use of programs like Java Scripts, Java applets, or
ActiveX controls. These various technologies as a whole are known as client-side
solutions and the use of CGI is known as server-side solutions as the processing is from
the webserver.

How does Common Gateway Interface (CGI) Script


Work?
3

For the Common Gateway Interface script to work, the web server needs to support it
and should have been configured to process it. These scripts are mostly written in PERL/
C or it could be a shell script also.

To understand its concept let us take a look at what happens after there is a typical
action of clicking a hyperlink on a web page:

 A user requests the HTTP web server and demands for the URL. The user can also
type the URL in a browser’s location window, which can be a hyperlink or specified
as HTML <form> tag.
 The browser at the client passes this information to the web server with a request for
a filename, via the URL.
 The web server would parse the URL and search for the filename. If found, the file
would be returned to the browser or an error message is sent back.
 The web browser would then either display the file or the error message to the user.

Now in this scenario, if the web server is configured in a way that if the requested file is
in a certain directory then rather than sending the file, a program is run and the output of
that program is sent back to the user. This is where Common Gateway Interface comes
into play.
All the Common Gateway Interface scripts or programs, which the web server
executes, are kept in a pre-configured directory. This is the CGI directory and
conventionally called /CGI-bin.
4

Features of Common Gateway Interface (CGI)


The features of CGI include:

 These applications run on the server.


 They are reusable pieces of code.
 It is a very well defined standard which is supported by most modern browsers.
 Since the interface is pretty consistent, these application can be written by a
programmer in many different languages like C, C++, Java, and PERL.
 The person writing the Common Gateway Interface program can write it
independently of the operating system which the server uses.
 It is a very simple and basic way of passing information about the user’s request
from the web server to the application program, and getting the response back.
 By default, the CGI scripts run in the security context of the web server.

When Common Gateway Interface (CGI) is Used


 These scripts are used for the below reasons:
 When there is a need for the webserver to dynamically interact with user, CGI
scripts are one of the most common ways of doing it.
 These scripts are mostly used when there is data to be submitted by the user to the
webserver by filling out a form. The CGI program would then receive data, process
it and return the result back to the webserver and in turn to the user.
5

How Common Gateway Interface Scripts are Used?


The web server using CGI application for its working allows the owner of the website to
determine which URL should run which script. The steps involved in it can be broadly
and simply stated as below:
 A directory is created within the webserver which contains the scripts. This directory
is normally called CGI-bin. Let us say this directory is /user/local/CGI-bin.
 The user sends a request to the server in the form of URL http://mywebsite.com/cgi-

bin/mycgiscript.pl.
 The server recognizes that the file being requested is a CGI script and instead of
sending back the file, it runs the script and passes back the output of the script to the
web client
Let us take a closer look at a simple CGI program which prints out a line to the user and
we will go through some of the headers and their descriptions. Below is a sample script
written in PERL:
6

The output of this would appear on the web client as below:

Here the first line is an HTTP header which makes the browser understand that the
content is a text string, “Content-type: text/html\r\n\r\n”
Content-type – This is the HTTP Field Name.
text – This is the HTTP Field Content.
There are a few other important HTTP headers used in CGI, as listed below:
7

While writing CGI programs, environment variables play an important role. All these
programs have access to the following environment variables:

Advantages of Common Gateway Interface (CGI)


Following are some of the advantages of using CGI scripts:

 They are language independent so it makes it easier for any programmer to choose
their preferred language.
 They are very simple and can do advanced tasks much easier than other server-side
programming languages.
 There is a huge existing CGI code library that can be reused.
 They are secure as they run on the web server.
 They are very lightweight as no specific library is needed to be present in order for
the code to execute.
8

Disadvantages of Common Gateway Interface (CGI)


CGI programs come with their own set of disadvantages such as:

 Since the CGI program forks a new process for each HTTP request, it takes up a lot
of server memory.
 It also needs to open a new database connection each time a request is made, which
is again a slow and costly process.
 Data cannot be cached in memory between page loads, hence it is less efficient.

Applications of Common Gateway Interface


CGI transforms the Web from collecting static data into a new interactive structure,
wherein users can interact with the number of questions to run applications. Some of the
applications that are designed using CGI are:

Forms
Forms are one of the most significant users of CGI. Forms allow the user to share
information and is a subset of HTML. A CGI program makes these forms very
interactive for both user and provider by processing and selecting the appropriate forms
that match the selection criteria.

Gateway
Web gateways are alike programs or scripts. It is used to access the necessary
information that is not directly readable by the client from the database. The CGI
9
program is employed to serve as a gateway and use appropriate programing language to
read the information, format and share it with the client.

Virtual Documents
Virtual document creation is the most important part of the CGI.
While virtual documents are created as per the user’s request, it can vary from virtual
HTML, images, plain text to sometimes audio.

Working Applications of Common Gateway Interface


Some of the impressive CGI programs on the web that describes the powerful aspects of
the CGI are listed below.

Lycos World Wide Web Search


This server helps users to browse for particular documents. In response to the user’s
request, Lycos returns the hypertext document matching the user’s Web search criteria.
The links are www.lycos.com.

Coloring Book
The amazing application designed for users who enjoys coloring. The link is
www.ravenna.com/coloring.

Japanese to English Dictionary


It is a virtual document-based application. The cutting-edge CGI program inquires for an
English word from the user and returns an equivalent Japanese word with appropriate
images.
10

Guestbook with World Map


It uses a form-based application, which enables users to leave messages that are read by
everyone.

CONCLUSION
One of the first steps you can take to understand CGI or the HTTP protocol is to
familiarize yourself with the underlying variables and syntax. This includes the
environment variables just outlined. Though CGI is rarely used today, many current web
development languages like PHP also use many of these variables. As a result, learning
them will also help you write robust programs even for current web development
languages.

REFERENCES
 https://www.geeksforgeeks.org/perl-cgi-programming/
 https://en.wikipedia.org/wiki/Common_Gateway_Interface
 https://www.tutorialspoint.com/cplusplus/cpp_web_programming.htm

You might also like