CGI stands for Common Gateway Interface, and is not an actual program that runs on a machine, but is a basic method for information servers to communicate with other programs on a Unix system. CGI sets up a communication standard for input and output. A "cgi program" is a program that gets its input from the web server and gives its output back to the web server.
When a user fills out a form on an HTML page and clicks submit, the web browser begins to format the data in a specific way. When the web server gets a request a cgi program, it is usually accompanied by a stream of extra data. The web server uses the CGI standard to start executing the program, set up some environment options, and send the extra data along to that program via stdin. It will then wait until that external program is finished. The web server will then have some data from that program's standard out, which is usually an HTML page created on the fly, which it will send directly to the client. The web server doesn't have to worry about formatting the data or displaying it. It justs uses the Common Gateway Interface method to communicate with the external program.
CGI programs usually go in a special place in the web servers directory. It is usually called "cgi-bin". The files that go in here are executable files that the Unix system can run. This is referenced in a URL by:
The way in which the browser formats the form data is by using name-value pairs. Every item in the form has a unique name, and some value for it. It all gets concatenated into one large string that looks like this:
The & separate the pairs, and the = tell what value to assign the variable named 'name1'. There are two basic ways a browser can send this information. The first is called GET, and it appends the name-value pairs to the end of the URL for the cgi program with a '?'. So you see at the URL:
However, the length of the URL can only be 1024 characters long. Because of this, GET is hardly ever used. One instance where you have to use GET is when you are calling a cgi program from within a tag, and you must pass it some values. When using GET, the data is passed to the cgi program as a command line argument.
There is another method that can support larger data sizes called POST. This sends the data as a separate data stream, which is read by the cgi program through stdin, instead of as a command line argument. The format of the data is the same, the only difference is how it is sent and how it is recieved by the cgi program.