What Is CGI??

Overview

What is CGI?

How Does CGI Work?

  1. The browser (Netscape, Internet Explorer, etc.) runs on the client (e.g. PC, Mac, UNIX).
  2. This browser displays the html code sent by a server which may contain a ``form'' or other CGI information.
  3. The client sends the CGI request to the server. See Figure 1.
  4. The server processes the client's CGI request. Typically this involves running an application on the server. See Figure 2.
  5. The application sends the results of the CGI request back to the server. See Figure 3.
  6. The server sends new html code to the client. See Figure 4.
  7. The client displays this new html code in the browser's window.

What are the prerequisites?

This introduction to CGI is written specifically for www.wsu.edu. However, most of the information included is applicable on other web servers as well.

What is a gateway anyway?

A gateway gives the client easy access to information not typically available to client browsers. This includes SQL access to databases (e.g. Oracle, Sybase, Ingres, etc.)

How does a Gateway Work?

  1. First the client sends a request to the CGI script. See Figure 5.
  2. Next the CGI script sends back a customized form to the client to fill out. See Figure 6. The form could be different depending on different values. For example, someone in Japan could be presented with a form written in Japenese. Someone with Netscape could be presented with a form the looks different than someone with Internet Explorer or Mosaic.
  3. The client fills out the form and sends the results back to the CGI script. See Figure 7.
  4. The CGI script passes the request to the database program. See Figure 8. This could be something like an SQL or other databse request.
  5. The database returns the data requested to the CGI script. See Figure 9.
  6. The CGI script then builds a new html stream based on the results of the database query and sends it to the client to be displayed. See Figure 10.

CGI Languages

There are several languages you may use to program CGI scripts in:

You must already have a public_html directory setup on www.wsu.edu. If your haven't already done this, you can do so with the new_www program:

unicorn(23)% new_www

All cgi scripts must end in ".cgi" and must reside in your own public_html directory. It is recommended to create a directory called cgi-bin within your public_html directory to keep your cgi scripts:

unicorn(54)% cd
unicorn(55)% mkdir public_html/cgi-bin

In order to execute CGI script, you must have the script executable and readable by the world so the www server can see the script. reflect.cgi is used in this example:

unicorn(33)% pico reflect.cgi
unicorn(34)% ls -l reflect.cgi
-rw-r--r-- username group 132 Nov 1 17:05 reflect.cgi
unicorn(35)% chmod 755 reflect.cgi
unicorn(34)% ls -l reflect.cgi
-rwxr-xr-x username group 132 Nov 1 17:05 my.cgi

The script reflect.cgi is illustrated in Figure 15 and the source file looks like:

#!/usr/local/bin/perl
# reflect.cgi
#
open(CLIENT,">-") || die "cannot open STDOUT";
$title="title of page";

print CLIENT "Content-type: text/html\n\n";
print CLIENT "\n<html>\n";
print CLIENT "<head>";
print CLIENT "<title>$title </title>";
print CLIENT "</head>";
print CLIENT "\n<body>\n";

$remote_address = $ENV{'REMOTE_HOST'};
$uptime = `/usr/bin/uptime`;
($days,$hours,$minutes) = ($uptime =~ /up (.*) days, (\d+):(\d+), (.*)/);

print CLIENT "Hello!! My inside sources ";
print CLIENT "revealed to me that ";
print CLIENT "you are running a ";
print CLIENT "client on $remote_address. ";
print CLIENT "The server has been up $days days, ";
print CLIENT "$hours hours and $minutes minutes.";
print CLIENT "</body></html>";

close(CLIENT);

exit(0);

WSU Specific Macros

There are a collection of macros called wsumacs.pl which make some CGI programming a little easier here at WSU. With wsumacs.pl the previous CGI script could be shortened by using the &open_client, &print_header and &close_client macros. You can use these macros when you specify:

require "/cgi-bin/wsumacs.pl";

at the beginning of your CGI script.

This script is demonstrated at http://www.wsu.edu/cgi-bin/examples/reflect.cgi

The source for reflect.cgi is:

#!/usr/local/bin/perl

require "/cgi-bin/wsumacs.pl";
&open_client;
&print_header("test showing uptime");

$remote_address = $ENV{'REMOTE_HOST'};
$uptime = `/usr/bin/uptime`;
($days,$hours,$minutes) = ($uptime =~ /up (.*) days, (\d+):(\d+), (.*)/);

print CLIENT "Hello!! My inside sources ";
print CLIENT "revealed to me that ";
print CLIENT "you are running a ";
print CLIENT "client on $remote_address. ";
print CLIENT "The server has been up ";
print CLIENT "$days days, $hours hours ";
print CLIENT "and $minutes minutes.\n";

&close_client;

exit(0);


All of the CGI languages have available environment variables specific to the server and variables specific to the client. This list of variables is illustrated in Figure 16. The following example can be tested at: http://www.wsu.edu/cgi-bin/examples/env_vars.cgi

#!/usr/local/bin/perl
# env_vars.cgi

require "/cgi-bin/wsumacs.pl";

&open_client;
&print_header("Print All Environment Variables");
print CLIENT "<h1> Print All ";
print CLIENT "Client/Server Environment Variables</h1>";


foreach $key (sort keys %ENV) {
print CLIENT $key, " = ", $ENV{$key}, "<p>\n";

}

&close_client;
exit;

Changing the Background

If you have a background you want to use in your page, you can use the "BACKGROUND" option in your cgi script. If you have a particular color you want to you, you can use the "BGCOLOR" option. Both of these, if used, are to be placed after you include the wsumacs.pl macros. For example,
#!/usr/local/bin/perl

require "/cgi-bin/wsumacs.pl"; $BACKGROUND="/images/back16.gif";
$BGCOLOR="#FFFFFF";
&open_client;
&print_header("test showing uptime");
A common use of CGI is in building "forms" where you can gather information from those who visit your pages. Forms can include menus, radio buttons, text fields, graphics, check boxes, etc. A form is submitted from a special button which must be on all forms. Typically a form begins:
  1. When an html file displayed on the client. The form request is then sent to the server ( Figure 1.)
  2. This html file references a cgi script which does the processing. This script hands off the request to the application ( Figure 2.)
  3. The results of processing the cgi script in the application are sent back to the CGI script on the server ( Figure 3.)
  4. FInally the CGI script sends a new html stream back to the client ( Figure 4.)
A form must minimally consist of a "form" and "action" html tag. For example:
<form action=somescript.cgi>
.
.
.
</form>



The form illustrated in Figure 17 is demonstrated at http://www.wsu.edu/examples/emphasis.html. The html source for this form is:

<html><head>
<title>Simple CGI Emphasis Script</title>
</head><body>
<h1>Emphasis CGI Example</h1>
This little form runs emphasis.cgi to print text
in a particular emphasis (roman, italic, bold, typewriter).
<p><hr>
<form action="emphasis.cgi" method=post>
Enter your message here:<p>
<textarea name=comment rows=5 cols=40>
Place your message here...
</textarea><p>
Select the emphasis you want:
<select name=emphasis>
<option>underline</option>
<option>bold</option>
<option>italic</option>
<option>typewriter</option>
<option>strike</option>
<option>blink</option>
</select><p>
Press <i>
<input type=submit value="SUBMIT">
</i> to continue.
</form></body></html>

When you run the emphasis form, the results of entering text on Figure 17 is illustrated in Figure 18.

The wsumacs.pl macros contain a subroutine &parseParms which will return the values of parameters passed from the html form to the cgi script. You call this subroutine by entering

&parseParms;

near the top of your cgi script.

In the html form illustrated for the emphasis example, there were two parameters established: "comment" and "emphasis". After entering &parseParms the values of these two are available as:

$FORM{comment}

and

$FORM{emphasis}

The following example emphasis.cgi illustrates the use of &parseParms:

#!/usr/local/bin/perl
# emphasis.cgi

require "/cgi-bin/wsumacs.pl";

&open_client;
&print_header("CGI test using a form");
&parseParms;
%font = (
'underline','u',
'bold','b',
'italic','i',
'typewriter','tt',
'strike','strike',
'blink','blink'
);

$tag = $font{$FORM{emphasis}};
print CLIENT "The following text is printed using";
print CLIENT $FORM{emphasis},":";

print CLIENT "<p> <$tag>$FORM{comment}</$tag><p>";
&close_client;
exit;

What can be returned?

There are many different types of information that can be returned from the cgi script to the client:

  • graphics,
  • text,
  • PDF or PostScript files,
  • portions of or entire documents,
  • other html code.

FormMail

There is a program available called FormMail which gives you the ability to have people fill out a form and have the results emailed to you. You do not have to write your own cgi script to use FormMail. For more information on FormMail: http://www.wsu.edu/UNIX_Systems/FormMail/using_FormMail.html

CGI.pm Modules

A handy collection of CGI subroutines that makes cgi programming in perl easier. You can get more information at:

http://www.wsu.edu/UNIX_Systems/CGI.pm/cgi_docs.html

Examples

Lycos Search CGI Gateway

There is a good search engine illustrated in Figure 11. Or you can go there yourself for a demo at http://www.lycos.com

Ravenna Coloring Book

There is good example of a cgi using colors illustrated in Figure 11 and Figure 12. Or you can go there yourself at http://www.ravenna.com/coloring/

Cosy Guestbook

Figure 19 illustrates a guestbook created with a CGI script. You can go there yourself at http://www.cosy.sbg.ac.at/rec/guestbook

Japanese to English Dictionary

Another cgi example illustrates a translation program which combines a CGI script and a gateway to a database is illustrated in Figure 13 and Figure 14. You can also view it yourself at http://enterprise.ic.gc.ca/cgi-bin/j-e

Documentation

Here are some www accessible URLs to CGI documentation:

Books to get for using CGI:

  • CGI Programming (ISBN 1-56592-168-2)
  • Learning Perl (Randal Schwartz: O'Reilly)
  • Programming Perl (Schwartz, Wall: O'Reilly)
  • The www Handbook (ISBN 1-85032-205-8)
  • Teach Yourself CGI Programming with Perl in a Week (Eric Herrmann: Sams.net)
  • Developing CGI Applications with Perl (John Deep: Wiley)
This page has been accessed times since December 26, 1996. Revised on January 6, 1997.