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.
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.)
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);
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;
#!/usr/local/bin/perlA 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:
require "/cgi-bin/wsumacs.pl"; $BACKGROUND="/images/back16.gif";
$BGCOLOR="#FFFFFF";
&open_client;
&print_header("test showing uptime");
<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>When you run the emphasis form, the results of entering text on Figure 17 is illustrated in Figure 18.
<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>
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;
There are many different types of information that can be returned from the cgi script to the client:
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
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
There is a good search engine illustrated in
Figure 11. Or you can go there yourself for a demo at http://www.lycos.com
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/
Figure 19 illustrates a guestbook created with
a CGI script. You can go there yourself at
http://www.cosy.sbg.ac.at/rec/guestbook
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
Here are some www accessible URLs to CGI documentation:
Books to get for using CGI: