您的位置:寻梦网首页编程乐园CGI编程Zhanshou's CGI Tutorial

What is CGI?


CGI stands for Common Gateway Interface, which is a standard for external gateway programs to interface with information servers such as HTTP servers. CGI is not a program or a programming language. It is a collection of protocols (or rules) that allow Web clients to execute programs on a Web server and receive their output (if any).

Usually, the most common way for CGI to work is that the Web client (users) enters input data (if needed, some CGI programs do not need any input, such as "Hello Somebody" example mentioned earlier) , which are transferred to the server based on some protocols. The server receives the input, then passes the input to the CGI program. Then the CGI program is executed (fore example, by either sending mail to somebody via Formmail, or returning the search result back to the users if it is a search program...).

The conceptual working of a form-based CGI query is illustrated in Figure 1.


Figure 1.

Now let's reference Figure 1. Take the above "Hello, somebody" CGI program as an example to see the exact steps to run a form-based CGI program:

  1. The browser requests an HTML document from the server.

  2. The server sends the document, which includes a form-based form.

    This is what we received:

    Interactive CGI Example

    This is an interactive CGI program, which is the well-known "hello, somebody!" program. Enter your name and click the submit button, see what will happen!

             Enter your Name:   
                                      

  3. The reader enters the requested information into the form.

    Now you enter your name, for example " Zhanshou Yu".

  4. When the reader clicks the submit button, the browser sends the data in the form field as well as the name of a CGI program to run.

    When you click the submit button, the data will be sent in the format of "name=Zhanshou+Yu" ( We will discuss the details in Section Encoding Scheme later in this tutorail) to the server.

  5. The server runs the CGI program passing it the form data.

    After receive the message, the server passes the input string "name=Zhanshou+Yu" to the CGI program "hello.pl" which is specified in the HTML FORM tag. Here "hello.pl" is the CGI program. The following is the PERL script of "hello.pl":

    #!/usr/local/bin/perl
    #-------------------------------------------
    # hello.pl by Zhanshou Yu
    #-------------------------------------------
    
    # Get the input
     read(STDIN,$buffer,$ENV{'CONTENT_LENGTH'});
    
    #Split the name-value pairs
     ($name,$value)=split(/=/,$buffer);
    
    # Substitute special character to its original character
     $value=~ tr/+/ /;
     $value=~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
    
    
    #------print the return HTML------------------
    
     #print the MIME type
     print"Content-type: text/html\n\n";
    
     #print the HTML body
     print"<html>\n;";
     print "<head><title>Hello!!! </title></head>\n";
     print "<body><h2><font color=#ff00ff> Hello!</font> </h2>\n";
     print "<center><blink><h1><font color=#20b2aa >$value</font></h1></blink></center>\n";
     print "<center><h2><font color=#ff0000> How nice to see you right here!</font></h2>\n";
     print "<hr>\n";
     print "<a href=\"../hello.html\">Back</a>\n";
     print "</body></html>\n";
     exit;
    

    Now the CGI program runs and produces the following output:

    Content-type: text/html
    
    <html>
    <head><title>Hello!!! </title></head>
    <body><h2><font color=#ff00ff> Hello!</font> </h2>
    <center><blink><h1><font color=#20b2aa >Zhanshou Yu</font></h1></blink></center>
    <center><h2><font color=#ff0000> How nice to see you right here!</font></h2>
    <hr>
    <a href=\"../hello.html\">Back</a>
    </body></html>
    

  6. The CGI program processes the output and sends it back to the server, which in turn passes it back to the browser.

    The above output was sent back to the server, the server then sent it back to the Web browser (e.g., Netscape or Internet Explorer). The browser displays the final result , this is what we see on the screen:

    Hello!

    Zhanshou Yu

    How nice to see you right here!


      Previous Page Table of Content Next Page