CGI

What is CGI?

A web server exists to return HTML documents, images, Java applets, and anything else a client web browser requests from it. However, it also knows how to run programs. If a server gets a request to run a program, then instead of returning an HTML document, or image, the server will execute the program locally, and return the output from that program to the client browser.

In this way, the server can return dynamic, rather than static information. For instance, you may have seen web sites with page counters on them, which record how many visits a particular site has had. Every time the HTML page is loaded into a browser, a line of HTML code causes a program to run on the server. This program finds a stored count of hits on the server, increments it, and constructs an image "on the fly", which is then displayed on the HTML page. Compare this with loading a normal image, which would never change between visits.

CGI is the Common Gateway Interface, a protocol which governs how two things occur:

  1. How the web browser requests a program to be run on the server (as opposed to requesting a static document or image), and how it passes information to that program
  2. How the web server passes this information to the program itself, and how the program returns its output to the server, for transmission back to the client browser

Why use CGI?

CGI is one of the methods available to the web developer which can add interactivity to a web site. Whilst HTML and graphics can make a site look great, by themselves they are unable to actually do much.

For example, let's suppose that you wanted to make a catalog of your company's products available on your website. By just using HTML, the only option would be to write a page for each product, or group of products, and have the user navigate through a potentially enormous number of menus and options to locate the one they want. If you wanted to organise your products by price, category, and availability, you would have to do it three times. Maintaining such a site (for instance, if a given product is temporarily out of stock) would be a nightmare.

However, a better solution might be to maintain a database of products on the web server, which can be easily maintained. Then, by using a CGI script in conjuction with an HTML form, you could allow the user to search the entire database for the product they want, by price, category, product code, availability, or any other criteria. The form would send a request to the CGI script, which would query the database in real time, and construct a list of results to send back to the user. Whenever you update that database, any changes become instantly available to the user. As an analogy, compare having to navigate through a directory-type listing of websites by category (such as on www.yahoo.com) with using a search engine.

Some sample CGI applications

CGI scripts can be used for a huge variety of applications, limited only by the imagination and skill of the developer. Some common uses for CGI are listed below:

The potential applications of CGI are incredibly wide-ranging. It is this sort of interactivity which can really bring a site to life, although newer technologies such as Java and JavaScript present some attractive alternatives.