As the number of users on the World Wide Web increases every day, its use in different areas is also growing. One of the most powerful aspects of the Web is that anybody who has Internet access can browse on the net. This enables sharing the information worldwide. As we traverse the vast frontier of the World Wide Web, we’ll come across documents that make we wonder, “How did they do this?” These documents could consist of, among other things, forms that ask for feedback or registration information, image maps that allow to click on various parts of the image, counters that display the number of users that accessed the document, and utilities that allow to search databases for particular information. In most cases, we’ll find that these effects were achieved using the Common Gateway Interface, a.k.a. CGI.
Overview of Common Gateway Interface (CGI)
One of the Internet’s worst-kept secrets is that CGI is amazingly simple. That is, it’s trivial in design, and anyone with an iota of programming experience can write rudimentary scripts that work. It’s only when your needs are more demanding that you have to master the more complex workings of the Web. In a way, CGI is easy the same way cooking is easy: anyone can toast a muffin or poach an egg. It’s only when you want a Hollandaise sauce that things start to get complicated.
The Common Gateway Interface (CGI) is not a programming language. Rather, it is a simple standard governing how a Web server interacts with and runs scripts to process forms and complete Web requests. Any program in any language that outputs a Web page can be a CGI program. Perl is particularly well suited to CGI because of its powerful text processing capabilities and its simple memory management.
CGI is just a protocol, a formal process between a Web server and a separate server-side progra. The server encodes the client’s form input data, and the CGI program decodes the form and generates output. The protocol is independent from the programming language used to program CGI. CGI is the part of the Web server that can communicate with other programs running on the server. With CGI, the Web server can call up a program, while passing user-specific data to the program (such as what host the user is connecting from, or input the user has supplied using HTML form syntax). The program then processes that data and the server passes the program’s response back to the Web browser
CGI scripts called in two main ways (methods). The HTTP GET method is used in document retrievals where an identical request will produce an identical result, such as a dictionary lookup. The HTTP POST method sends form data separate from the request. The GET method is only safe for short read-only requests, whereas POST is safe for forms of any size, as well as for updates and feedback responses. Therefore, by default, the CGI module uses POST for all forms it generates.
Working of CGI (Common Gateway Interface)
CGI is a standard for helping web servers run external programs and return dynamic web pages.
CGI (Common Gateway Interface) is a standard way of running programs from a Web server. Often, CGI programs are used to generate pages dynamically or to perform some other action when someone fills out an HTML form and clicks the submit button.
Figure 1: CGI Working
As shown in the given figure 1, a Web browser running on a client machine exchanges information with a Web server using the Hyper Text Transfer Protocol or HTTP. The Web server and the CGI program normally run on the same system, on which the web server resides, Depending on the type of request from the browser, the web server either provides a document from its own document directory or executes a CGI program.
The sequence of events for creating a dynamic HTML document on the fly through CGI scripting is as follows:
- A client makes an HTTP request by means of a URL. This URL could be typed into the ‘Location’ window of a browser, be a hyperlink or be specified in the ‘Action’ attribute of an HTML <form> tag.
- From the URL, the Web server determines that it should activate the gateway program listed in the URL and send any parameters passed via the URL to that program.
- The gateway program processes the information and returns HTML text to the Web server. The server, in turn, adds a MIME header and returns the HTML text to the Web browser.
- The Web browser displays the document received from the Web server.
Why is CGI Used?
An interesting aspect of a CGI enabled Web server is that computer programs can be created and deployed that can accept user input and create a webpage on the fly. Unlike static Web pages that display some preset information, these interactive web pages enable a client to send information to the Web server and get back a response that depends on the input.
To create an interactive Web page, HTML elements are used to display a form that accepts a client’s input and passes this to special computer programs on the Web server, these computer programs process a client’s input and return requested information, I usually in the form of a web page constructed on the fly by the computer program.
These programs are known as gateways because they typically act as a channel between the Web server and an external source of information, such as a database. Gateway programs exchange information with the Web server using a standard known as The Common Gateway Interface. This is the reason CGI programming is used to describe the task of writing computer programs that handle client requests for information.
The term gateway describes the relationship between the WWW server and external applications that handle data access and manipulation chores on its behalf. A gateway interface handles information requests in an orderly fashion, and then returns an appropriate response. For example, an HTML document generated on the fly which contains the results of a query applied against an external database.
In other words, CGI allows a WWW server to provide information to WWW clients that would otherwise not be available to those clients. This could, for example, allow a www client to issue a query to an Oracle database and receive an appropriate response in the form of a custom built Web document. Some common uses of CGI include: Gathering user feedback about a product line through an HTML form. Querying an Oracle database and rendering the result as an HTML document.
 Shishir Gundavaram, “CGI Programming on the World Wide Web”, online available at: http://www.oreilly.com/openbook/cgi/ch01_01.html
 “What is CGI and How Does it Work?” online available at: https://www.tcl.tk/man/aolserver3.0/cgi-ch1.htm
 “Introduction”, online available at: https://www.cse.unr.edu/~fredh/papers/thesis/006-cetin/paper.pdf
 Dinesh Thakur, “What is CGI? | Common Gateway Interface Definition”, online available at: http://ecomputernotes.com/html/tutorial/common-gateway-interface