Internet Programming
COMMON GATEWAY   INTERFACE (CGI)

 
Introduction  

One of the best things about the World Wide Web is that you can use it to interact with potentially millions of users to obtain and provide different information. Due to the dynamic nature of this information, static HTML pages alone are not enough. There has to be a way  to display dynamic information to those surfing your Web site based on what they need. CGI is a mechanism that enables you to do just that. CGI stands for Common Gateway Interface. After you have your Web site all set up and have created some Web pages, it's time to think about making your Web site dynamic by setting up CGI scripts on your  Web server. By utilizing CGI, you can exploit the World Wide Web to its fullest potential because it allows you to interact with browsers of your Web site. 

Just like application programs, CGI programs can be written in almost any programming language that will let you either create an executable program or let you interpret it in real time with another program. The following list a few languages that you can utilize to create CGI applications : 
AWK, C/C++, Fortran, Pascal, Perl, Visual Basic, Batch, shell, etc. 
Depending on your expertise, what's available, and the nature of your CGI projects, you will have to choose the best language to suit your needs. Customarily, CGI scripts are stored in the CGI-BIN directory of the Web server's document root directory. All files and pathnames of a Web site are relative to this directory.

The CGI protocol allows you to  add custom programs and services to your Web server. This protocol acts as an interface between software that you write  in a standard programming or scripting language and the Web server. The CGI process works  as follows :

  1. The user, running a Web browser, selects an item that  activates a CGI script. The client sends to the server both the name of the script and any associated data, such as  information entered on a form.
  2. The server sets up the proper operating environment and calls the CGI program. The environment  allows the exchange of data between the server and the CGI  program that may or may not be located on the same host. The server sends the input data from the client to the server at this time.

  3. The CGI program executes, returning any output back to the server.  The server in turn passes the information to the client.

Benefits  

Plain text HTML files retrieved by Web clients are static. The information contained in these files never changes unless you manually edit them to make changes. However, by utilizing CGI scripts, your Web pages can be created dynamically each time a client accesses them. To the client, it will look as if the page has been specially created for him or her based on the information needed. Obviously, this is a very powerful tool for interacting with Web surfers. You should utilize CGI to make your Web site interactive so that you can provide customized content and enable those browsing  your Web site to interact with the information you provide. 

Benefits of CGI are invaluable to any Web site. These benefits range from having a customized input form for feedback to allowing someone browsing your Web site to update and retrieve information from a database on your server.  By setting up a customized email feedback form, you can make sure you are provided with all the information on your need. Furthermore, you can be sure that your email feedback form will always work because it does not depend on how the email capability of your client's Web browser is setup. In the "Setting Up a Feedback Form" section, you will be shown how to utilize CGI to setup an email feedback page. Furthermore, if you want to setup a database that collects data from users browsing your Web site, you can use a CGI script to update information provided by these users to database. With the aid of CGI, it is possible to update a database on your server without your direct human intervention. As you can see, the possibilities and applications of CGI are endless. 

One of CGI's best features is its capability to let Web surfers interact with databases on your server. For example, you might have a Microsoft Access database on your server that needs to be updated with information provided by users surfing your Web site. You might also need to make parts of this database available to authenticated users for querying. Although you can use plain old email to correspond with people, and manually perform database queries and updates, this is not very practical after you start getting more and more visitors. Eventually, you will end up spending the whole day answering and responding to email. By setting up a simple form, you can perform updates to your database by utilizing a CGI script. Keep on reading, and soon you will find out how easy it is to use CGI to interact with people browsing your Web site. 

By utilizing CGI to make your Web  site interactive, users visiting your Web site will be able to easily find the information they need. Because your Web site is easy to navigate, these users will visit it again and again for more information they need. Because your Web site is easy to navigate, these users will visit it again and again for more information. CGI also enables you to customize what people see when browsing your Web site by providing dynamic content. Furthermore, you can use a CGI script to provide content that's customized for the Web browsers being used to access the information. 

Applications

Many organizations and individual are using CGI for a variety of tasks, from having a simple counter on a Web page to counting the number of access to a CGI script managing an entire store front-end. This CGI script can allow users visiting a Web site to look at various merchandise being sold and even place orders. In addition to this, various Web sites offer search capabilities of the site to make finding information easier. 

You can use CGI whenever you want to interact with those browsing your Web site, to get feedback from those browsing your Web site, or to provide dynamic content. The following lists a few applications of CGI that you can use to enhance the capabilities of your Web site : 

  • Setting up a guest book
  • Setting up a feedback form
  • Adding a counter to a Web page
  • Designing a database front end for the Web
  • Allowing Web surfers to visit various Web pages via a pull down list
  • Enabling those browsing your Web site to email comments
  • Providing customized Web pages based on Web browsers being used by a client
  • Enabling those browsing your Web site to search your Web site.

CGI Basics  

A CGI script is typically used to provide dynamic content to the client that called the CGI script. CGI scripts communicate with Web browsers. If the CGI  script is an interactive script, typically a form with various input controls is sent to the Web client. After filling in the form, the user submits it to the Web server. The Web server then uses CGI to call the CGI script with data from the Web  client. The CGI script processes the data, possibly accessing a database on the server, and sends a message to the client that made the request. If the CGI script is a non interactive CGI script, the  output of the script is  sent directly to the client that called the CGI script with its URL. 

When a CGI script is called, the Web server first examines the REQUEST_METHOD used to  call the CGI script to determine how the Web client is sending data to the CGI script. If the REQUEST_METHOD used to call the CGI script is GET, any data supplied by the  Web client for the CGI script is found immediately following the URL name of the CGI script. Therefore, this information will be stored in the environment variable QUERY_STRING. On the other hand, if the REQUEST_METHOD used was POST or PUT, the size of input for the CGI script is stored in CONTENT_LENGTH. CONTENT_LENGTH contains the size of data supplied to the CGI script in bytes. The CGI script can then read from standard input the number of bytes returned by CONTENT_LENGTH to find out data given to the CGI script. 

How CGI Works  

Although a major use of CGI is to provide dynamic content to those browsing your Web site, CGI programs do not always need to be interactive. You can use non interactive CGI scripts to provide dynamic information that does not need user input to be created. For example, in order to take advantage of various features offered by Web browsers such as Netscape Navigator and Microsoft Internet Explorer, it is relatively simple to write a CGI program to determine the browser being used by a client and send a page specially designed to take advantage of that browser's capabilities. The CGI script can be executed  transparently to the user without any user  intervention. For example, if the default Web page of a Web server is welcome.html, the main Web page of the Web  server can be mapped to a CGI script by creating a URL-CGI mapping. Such a script can determine the browser being used by the client. 

If a CGI script does not make use of  user input, what happens when a client accesses the page is very simple. First, the client connects to the Web server and requests a Web page. Because the document requested is linked to a CGI script, the  Web server executes the CGI program that the page is linked to. Output of the CGI program is then sent to the client that requested the page. Afterward, the connection between the Web server and the Web client is closed. 

One of the greatest aspects of CGI is  its capability to interact with those browsing your Web site. You can ask a user to fill in a form and then submit the form. The CGI script can then validate the user's input, ask the user to complete any incomplete information and process the user's input. 

In the case of a CGI script interacting with a Web client to display dynamic content, first a Web page with various controls is sent to the Web browser. After the user fills in the form, the form is submitted to the Web server to be processed. Depending on the REQUEST_METHOD used to communicate with the CGI script, the CGI script obtains data sent from the client, processes the data, and displays its output to standard output. Everything written to standard output by the CGI script will be visible to the client that called the CGI script. 

References :  
Sanjaya Hettihewa, Windows NT 4, Web Development, Sams net, Indianapolis, First Edition, 1996 
Terry Bernstein, Anish B. Bhimani, Eugene Schultz, Carol A. Siegel, Internet Security for Business, Wiley Computer Publishing, John Wiley & Sons Inc, 1996 

[Home] - [Isi Buku Tamu] - [Lihat Buku Tamu] - [Email]
Copyright © 1999-2007, InVirCom. All rights reserved.