CGI Scripts


We are going to talk about CGI (Common Gateway Interface), but first of all you have to know more about client/server issues. In other words, you have to know how servers and clients talk each other. Let's see...

Conversations between a client and a server

When you click on a link contained inside of a web page, maybe you don't know that, but your browser start chatting with the server pointed by that link. A client is any software program which can ask something to a server (even the computer where a client is running is often considered a client itself). So your web browser is a client. A server is any software program running on a computer connected to any network which can reply to requests done by clients (even the computer running a server is often considered a server itself). When you type an URL (Uniform Resource Locator) into the location field of your browser, or when you click on a link (link or URL are the same thing), you are pointing to a certain web page contained inside of a certain computer. A web server is a piece of software which is running uninterruptedly on a machine, waiting for requests. A server is also called a demon. When the server receives a request, it attempts to satisfy it. In other words, when you clink on a link, you are asking (well your browser is asking in your place) for a web page (document). The server receives your request and searches for that page and when it finds that page, it delivers it to your web browser. Clients and servers talk by means of a particular language: HTTP. HTTP stands for HyperText Transfer Protocol. Let's see this URL: http://www.anyserver.com/anydirectory/anypage.html. There are 4 parts:

  1. The method's name (or service)
  2. The server's name
  3. An optional path
  4. The web page's name

The method's name maybe HTTP (web), maybe FTP (FTP), maybe NEWS (newsgroups), maybe MAILTO (SMTP, Simple Mail Transfer Protocol, i.e., email) and so on. The server's name indicates the name of the computer where a certain page is located. An optional path shows inside of which directory that page is stored. And finally, there is the page's name.

When you click on http://www.anyserver.com/anydirectory/anypage.html, first of all your client (the web browser) starts a connection between your computer and the server named www.anyserver.com, asking for a web page named anypage.html. At this point the server receives that request and searches for that page. If the server finds said page, it delivers it to the client, otherwise it sends a well known error message: '404 not found'. Then the server closes the connection. Let's see a little chat between your client and the server named 'www.anyserver.com' here: little chat.

HTTP is a stateless protocol which means that each request your client do to the server, requires a new connection. Each object contained inside of a web page requires a new connection (unlike other protocols such as FTP or Telnet, where the client establish only one connection and can effect several requests during that connection). So when you download a web page which contains several images for example, your client has to establish one new connection for each image it finds reading the web page. Well, during a new request the server can't know anything about the previous request, and indeed, it not even knows who did the previous request. The new HTTP protocol (HTTP-NG or HTTP Next Generation, allows one connection for more requests, so for example you can read a web page with several images establishing just one connection. So a web page is downloaded quicker). So the web server is just a piece of software running uninterruptedly and waiting for requests. When one request is satisfied and the connection is closed, it is ready again, waiting for new requests. In other words the web server is a kind of manservant who is under any web client's orders. However your browser can ask for HTML documents, images, sounds, videoclips or...scripts. But a script is not received, indeed it is EXECUTED. And now let's see what these CGI scripts are. A script is a piece of software that acts as an interface between one client, one web server, the operating system, the hardware peripherals or even other servers. For example, suppose you want to look up in a specific database. Suppose that you want to look up it by using a web interface (a form for example). Well, the server offering that service, surely needs a CGI script, in fact it has to receive your query (that's usually written by using the human language) and translate it into the database's query language. Think about a search engine like Altavista for example. The CGI script receive your query and translate it into the Altavista's database query language and searches for the document you asked for. When it finds something it has to packet data, showing them inside of an HTML page (in other words, the CGI script has to build a web page 'on fly' to show results of your query).

Used languages

You can use any programming language to build a CGI script. Well, obviously the programming language you choose must be supported by the server. For example, if your server is running on an Unix system, you could use the Bourne shell language, or you could use the C language. If your server is running on a Macintosh you have to use the Applescript language, and so on. However, the most used programming language to build a CGI script is surely the PERL language (PERL stands for Practical Extraction and Reporting Language).

Short CGI course


Index           Home  Back       About  Contact us!

Copyright (c) 1998-2006 Wowarea