Introduction to CGI programming

January 2017

What is CGI?

CGI (Common Gateway Interface) are programs run by the web server (at "server side"), which send to the user's browser, HTML code automatically created by the server (e.g based on another application, such as a database management system).

One of the main advantages of using CGI is the ability to provide dynamic pages. CGI are commonly used for HTML forms, allowing the users to select/enter data then and then click a button to submit the form.

CGI- the programming language

A CGI program can be written in almost any programming language as long as it is:
  • capable of reading the input data stream;
  • capable of processing strings;
  • able to write to the standard output stream;
  • executable or interpretable by the web server.

The programming languages ??the most commonly used for writing CGI are:
  • The Perl language
  • C and C + +;
  • Java.

When a request is sent to the server, it examines the extension of the requested file to determine what action must be performed. For example:
  • in the case of an HTML file (.htm or .html): The server will generate HTTP response headers, which will be sent to the browser requesting a file and then send the HTML file .
  • in the case of a CGI written in Perl (.pl extension): The server will execute the Perl interpreter, it will interpret the code contained in the .pl file, then send the data to the server. The data will then be sent to the browser.
  • in the case of a CGI file written in C language (.exe extension on Windows): The server will execute the CGI, then send the data (returned by the program) to the browser.

Thus, the role of a CGI script to receive data sent by the server, process them and then return the response headers/valid HTML tags. Indeed, when a CGI program returns a file, it must first send an HTTP header to specify the type of content sent to the browser (called MIME type), that is to say:
In the case of an HTML file, the following string is used:
  • content-type : text/html

In the case of a gif file, the following string is used:
  • content-type : image/gif

In the case of a QuickTime file, the following string is used:
  • content-type : text/quicktime


Orignal document published on


Published by deri58. Latest update on April 7, 2013 at 04:49 AM by deri58.
This document, titled "Introduction to CGI programming," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (