home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


How CGI Works

A CGI program is only a program, and most CGI programs are straightforward things written in C or Perl, two popular programming languages. Listing 28.1 shows a standard “Hello World” example in C.


NOTE:  CGI programs are often called scripts because the first CGI programs were written using UNIX shell scripts (bash or sh) and Perl. Perl is an interpreted language, somewhat like a DOS batch file but much more powerful. When you execute a Perl program, the Perl instructions are interpreted and immediately compiled into machine instructions. Some other languages such as C are compiled ahead of time, and the resulting executable isn’t normally called a script. Compiled programs usually run faster but are harder to modify.

In the CGI world, however, interpreted and compiled programs are both called scripts—that’s the term used in this book.


Listing 28.1 Hello World CGI Script in C


   int main(int argc, char *argv[])
   {
      printf(“Content-type: text/html\n\n”);
      printf(“Hello, World!\n”);
      return (0);
   }

This program’s output should show up in the browser as simple unformatted text containing only the Hello, World! line. The program in Listing 28.2 adds a few HTML tags to its output to send an actual HTML document to the browser.

Listing 28.2 Hello World CGI Script in C, with Basic HTML Output Added


   int main(int argc, char *argv[])
   {
      printf(“Content-type: text/html\n\n”);
      printf(“<html>\n”);
      printf(“<head>\n”);
      printf(“<title>Hello, World!</title>\n”);
      printf(“</head>\n”);
      printf(“<body bgcolor=\”#FFFFFF\”>\n”);
      printf(“<center><h1>Hello, World!</h1><center>\n”);
      printf(“</body>\n”);
      printf(“</html>\n”);
      return (0);
}

A CGI Hello World example in Perl is as simple or perhaps even simpler than one in C. Listing 28.3 shows a basic Perl script that sends Hello World to your browser.

Listing 28.3 Hello World CGI Script in Perl, with Basic HTML Output Added


   #!/usr/bin/perl
   print (“Content-type: text/html\n\n”);
   print (“Hello, World!\n”);

Listing 28.4 shows a slightly longer Perl script for an HTML version of Hello World.

Listing 28.4 Hello World CGI Script in Perl


   #!/usr/bin/perl
   print >>END_of_HTML;
   Content-type: text/html

   <html>
   <head>
   <title>Hello World in Perl</title>
   </head>
   <body bgcolor=”#FFFFFF”>
   <center><h1>Hello, World!</h1><center>
   </body>
   </html>
   END_of_HTML

If you use Windows 95 or NT, you probably can get by with just the last two lines. Including the “shebang” line that starts with #! (sharp bang) won’t hurt, though, because comments in Perl start with #. Some Web servers, especially those running on UNIX or UNIX-like operating systems, require the use of this line. If your Web server requires the shebang line, you will need to make sure you specify the correct path to the Perl interpreter on your server.


Sometimes you will need to write out two blank lines after the Content-type line instead of one, although one should be enough. If your programs are not working as HTML, try this trick. The revised line in C would read
printf(“Content-type: text/html\n\n\n”);

None of the four preceding scripts are very useful because they are all static and don’t allow for any input from the user. But they are a good start for building more complicated CGI scripts.

Some of the most interesting CGI scripts work with an HTML form. They get input through the server from the user and send custom HTML—or data in another MIME-type format—back through the server to the browser.

When you write such a program, you might have to decode the QUERY_STRING environment variable and properly test the values in it for possible security flaws and other errors, or you do the same for input values from STDIN. Luckily, a handy module called CGI.pm is included with Perl 5.004+. For C, a library called cgic can be found at http://www.boutell.com/cgic. Using CGI.pm or cgic will eliminate some of these problems for you; or if you choose, you can get your variables “from scratch.” Listing 28.5 provides an example of parsing input from scratch using Perl.

Listing 28.5 Perl Code that Parses Input from the Web Server


if ($ENV{‘REQUEST_METHOD’} eq ‘POST’)
{
        read(STDIN, $buffer, $ENV{‘CONTENT_LENGTH’});
}
if ($ENV{‘REQUEST_METHOD’} eq ‘GET’)
{
        $buffer = $ENV{‘QUERY_STRING’};
}
        @pairs = split(/&/, $buffer);
        foreach $pair (@pairs)
        {
                ($name, $value) = split(/=/, $pair);
                $value =~ tr/+/ /;
                $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack(“C”, hex($1))/eg;
                $contents{$name} = $value;
        }

The following are sample HTML and Perl scripts that together enable you to type your name into a text-type <input> element inside an HTML <form> element, and then, instead of telling the world hello, it tells you hello. Listing 28.6 shows the HTML document.

Listing 28.6 An HTML Form That Will Pass Your Name to a CGI Program


<html>
<head>
<title>Set up for Hello, YOU!</title>
</head>
<body bgcolor=”#FFFFFF”>
<form action=”http://www.yoursite.com/cgi-bin/helloyou.plx”>
<h1>Enter your name, up to 20 letters:</h1><bR>
<input type=”text” name=”yourname” size=”20"><bR>
<input type=”submit”>
</form>
</body>
</html>

The following Perl script uses the CGI.pm module to get your name from the form in your browser window and then shows you another form that tells you hello. This simple script ignores security concerns but makes a good example.

Listing 28.7 A Perl Script to Get Your Name from a Form and then Tell You Hello


#!/usr/local/perl -w
#helloyou.plx is a program to tell you hello by name
#set up to use the CGI.pm module
use CGI qw(param);

#get your name you typed on the HTML form, using the CGI.pl module
my $yourname = param(“yourname”);

#send the top part of the new HTML code to the browser
print >>END_top;
Content-type: text/html

<html>
<head>
<title>The next step</title>
</head>
<body bgcolor=”#FFFFFF”>
<br>
END_top

#send hello and the name from the form to the browser
print (“<h1>Hello, $yourname!</h1>”);

#send the last part of the new HTML code to the browser
print >>END_bottom;
</body>
</html>
END_bottom


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.