The Common Gateway Interface, or CGI, is a standard for communication between a client and the server. CGI scripts can be written in almost any language. A program written in any language that uses this standard to transfer data between a Web server and a client’s browser is a CGI script. In other words, a CGI script is an external gateway program to interface with information servers such as HTTP or Web servers and client browsers. CGI scripts are particularly useful in doing simple things that do not require input from the client. For example, showing the current time or random banner when a webpage is accessed.
CGI scripts are significant in that they allow the web servers to be dynamic and interactive with the client browser as the server receives and accepts user inputs and responds to them in a measured and relevant way to satisfy the user. Without CGI, the information the users would get from an information server would not be packaged based on the request but based on how it is stored on the server.
CGI programs are of two types: those written in programming languages such as C/C++ and Fortran that can be compiled to produce an executable module stored on the server, and scripts written in scripting languages such as PERL, Java and Unix shell. For these CGI scripts, no associated source code needs to be stored by the information server as is the case in CGI programs.
CGI scripts written in scripting languages have not complied like those in non-scripting languages. Instead, they are text code which is interpreted by the interpreter on the information server or in the browser and run right away. The advantage to this is you can copy your script with little or no changes to any machine with the same interpreter, and it will run. Also, the scripts are easier to debug, modify, and maintain than a typical compiled program.
Both CGI programs or scripts, when executed at the information server, help organise information for both the server and the client. For example, the server may want to retrieve information entered by the visitors and use it to package a suitable output for the clients. In additional GCI may be used to dynamically set field descriptions on a form and in real-time inform the user on what data has been entered and yet to be entered.
In the end, the form may even be returned to the user for proofreading before it is submitted. CGI scripts go beyond dynamic form filling to automating a broad range of services in search engines and directories and taking on mundane jobs such as making the download available, granting access rights to users, and order confirmation.
Explanation of CGI
As shown in the image below, what happens during the request and execution of a CGI request. A CGI request is recognised by the web server by the location of the thing requested or by the file name extension. For example, if you load the URL www.example.com/cgi-bin/a.cgi into the browser, the webserver contacted www.example.com receives a request such as the following:
GET /cgi-bin/a.cgi HTTP/1.0
The server notices that the directory that contains the thing requested is cgi-bin. It is configured to take the object requested, which is here a.cgi that is a program located on the server and execute it as a standalone program.
The program generates standard output (in Perl, we would use print ( )). This output is in an important format, a header, a blank line and the body.