您的位置:寻梦网首页编程乐园CGI编程 >Basic CGI

The Most Simple Intro to CGI

How the Internet Basically Works

This tutorial is not intended to be an indepth or a 100% technically correct treatise. It is an explanation in plain English by analogy of how HTML and CGI-BIN requests are handled. This is the first installment of a series. Oh, I swear that no animals were hurt in the making of this web page.

- Bruce

The illustration above is a simple, but accurate representation of the Internet. We have rats out looking for food, which are our browsers. We have our garbage cans, better known as servers providing storage and a way of doling out the food when asked for it. So in a nutshell, the rat asks for food and the garbage can gives it to the rat.

Now of course our garbage can in addition to holding food, also has a bunch of little beasties living in it. You never heard of a smart garbage can, have you? Now you have. Let's flip the lid and see what's going on in there.

Well, actually there are other little beasties in the Unix world that are referred to as daemons. Hmmm. To make a file readable and writable to everyone, we issue the command chmod 666 filename. Coincidence? I don't think so... Now here is how all of this is going to make sense to you very quickly:
  • Telnet: This daemon is usually called telnetd. It's job in life is to wait for you to try and log on to the system. If at your location window in your browser, you enter telnet:server.com, you will get a login prompt. This is assuming your browser has a helper application- and that the server accepts telnet requests.

  • FTP: This is the ftpd daemon. It handles requests that start with ftp., i.e., ftp://bignosebird.com/bnbform.tar. FTP stands for File Tranfer Protocol.

  • HTTP: Ah, this is the daemon httpd, the one that we must concentrate on. The httpd handles all requests that start with http. For the purposes of doing HTML and CGI-BIN, this is going to be our favorite beastie!

When you issue a command from your browser, such as http://bignosebird.com/index.shtml, the request is sent to my server and the httpd daemon finds a document named index.shtml, which is then devoured by a school of very nasty sharks. They rip the page to shreds to find out what they have to do. Each shark takes one instruction. For instance:
If the page has text, three images, and a midi music file- in all, five sharks will be called to duty. It is the job of each shark to go and handle retrieving the requested information and packaging it to go back to the requesting browser.

Let's see how our sharks are doing. It looks like the text shark, two of the image sharks, and the music shark are doing just fine. Uh oh, something is wrong. One of our sharks has gone belly up on us. Did the author name that image file properly? Did somebody delete the file? Well, it's taps for that shark. The browser will display its icon for a missing graphic.

Why is CGI Different?

First, a big hello to KJ the Killer Whale. Unlike a simple HTML document request, a CGI-BIN (Common Gateway Interface) request requires something a whole lot smarter than a school of hungry, stupid sharks. When you click on a link that looks like:
http://bignosebird.com/cgi-bin/Guestbook/guestbook.cgi
You are really instructing the server to run an actual program that handles only this one type of request. In this case it runs the program that calls up the form that allows you to make an entry in the guestbook. Here is what happens:
  • Your browser sends the request shown above to my server.

  • My server runs the guestbook.cgi program.

  • The guestbook.cgi program realizes that you are not adding anything yet, so it makes the decision to create and return to you, a blank data entry form.

  • Your browser then displays this form as it would any other HTML document.
Please note that the item(s) sent back to the browser do not have to be text, it can be other types of information such as a graphical counter image.
So you take a moment and fill in all the fields and click on the SUBMIT button. Now a more complex series of actions take place. The command in the browser location window may look something like this:
/cgi-bin/Guestbook/guestbook.cgi?name=bruce&email=bnb@xyz.com

When KJ receives the request, it executes a program called guestbook.cgi that is located in the Guestbook directory under cgi-bin. What is different now is that in addition to a program request, information is being fed to KJ. Think of the items listed after each question mark as a fish. Here is what KJ has to do:
  • Load the guestbook.cgi program into memory for running.

  • It notices that it has a school of fish to munch on and proceeds to split them up using the & as a way of telling where one fish starts and another one ends.

  • KJ then writes a name on each fish so she can remember what is in that fish.

  • Seeing that the e-mail fish has a name in it, sends a thank you note to the person that signed the book.

  • Sends a note to the webmaster that a new entry is in the guestbook.

  • Takes all of the fish, and uses their information to create a new entry that is tagged onto the guestbook.html document.

  • Creates an HTML document saying "Thank you for signing my guestbook!" that is returned to the browser.
KJ's job is done. Everything went great and let's have a round of applause for KJ.

The important concept is that in order to use CGI-BIN, you must create a real, however big or small computer program. It is not embedded in your HTML document, but it is installed in your cgi-bin directory located on your server.

Are you ready to try and write your first cgi program?