CGI Quick Reference
Cgi
Back Button = '$backlink = $ENV{'HTTP_REFERER'};'
print <<EOF #print html code directly, with variable interpolation
EOF #end html code
dbm(database management) files:
opening:
A class implementing an ordinary array should have
the following methods:
TIEARRAY classname, LIST
DESTROY this
FETCH this, key
STORE this, key, value
if ($flag & O_RDONLY) { "r" }
elsif ($flag & O_WRONLY) { ($flag & O_APPEND) ? "a" : "w" }
elsif ($flag & O_RDWR) {
if ($flag & O_CREAT) { "w+" }
dbmopen(%users,"users",0666) || die "Can't open\n";
-opens file "users" for rw access(0666),
associating it to the associative array
%users.
-attempts to create it if it doesn't exist.
-returns '0' if cannot be opened.
to make sure a key doesn't exist in the dbm file:
if (!defined($users{$fields{'email'}})) {
ssi(server side includes) -p.67(*que)
<!--#exec cgi="/fxrsg-cgi/program_to_print_stuff"-->
(don't currently work on NeXT lab web server)
paging through a file (like web searches): p.63(*que)
to pass a value to the next script, call the script with:
...action="/cgi-bin/guestbook"...
-tells what script to call to process the information
<form method=get ...
-tells it that we're getting something in the form,
followed by the action (see above). This method sends the
information in the call to the script (after a '?' in the url)
-to process the sent data, use the following:
$temp=$ENV{'QUERY_STRING'};
@pairs=split(/&/,$temp);
foreach $item(@pairs)
{
($key,$content)=split (/=/,$item,2);
$content=~tr/+/ /;
$content=~ s/%(..)/pack("c",hex($1))/ge;
$fields{$key}=$content;
}
then use $fields{'value-name'} to access the data
passing a plain value to the next script
"/fxrsg-cgi/script.pl/value1"
$x = $ENV{'PATH_INFO'};
$x =~ s/^\///; #remove '/'s
$x = 0 if $x eq ''; #if null
<form method=post ...
-send the data through STDIN, sending only the 'ammount' of
data through the environment variable CONTENT_LENGTH.
-to process the sent data, use the following:
read(STDIN,$temp,$ENV{'CONTENT_LENGTH;});
# CONTENT_LENGTH = # of bytes of data
@pairs=split(/&/,$temp);
foreach $item(@pairs)
{
($key,$content)=split (/=/,$item,2);
$content=~tr/+/ /;
$content=~ s/%(..)/pack("c",hex($1))/ge;
$fields{$key}=$content;
}
boxes:
<input type=text name=name size=30 #for a single-line
input box 30 characters long
<textarea name=comment rows=5 cols=40></textarea>
#for a 5x40 text box
rows=5
cols=40
<input type=submit value="Sign In">
#for a box labeled 'Sign In' that sends the data off to
the cgi script specified earlier
html at the beginning of a cgi script:
print "Content-type: text/html\n\n"; #has to be there to
tell what type of output to have.
print "Content-type: text/plain\n\n"; #plain text output only
environment variables
GATEWAY_INTERFACE
-version of cgi supported by the web server
HTTP_USER_AGENT
-browser software & version number
PATH_INFO - extra path information from the request
PATH_TRANSLATED
-maps virtual path to absolute path
(*)QUERY_STRING
-string containing data from a form submission
REMOTE_ADDR -client machine IP
REMOTE_HOST -client machine hostname
REQUEST_METHOD -method by which script was called
(usually GET or POST)
SERVER_NAME -configured host name for the server
SERVER_PORT -port to which server is 'listening'
SERVER_PROTOCOL -version of web protocol used
SERVER_SOFTWARE -name & version of server software
colors:
<body bgcolor="#______" - background
text="#______" - text
link="#______" - links
vlink="#______" - virtual links
000000-black, ff0000-red, 00ff00-green, 0000ff-blue
Environment
%ENV - environment variables associative array.
$ENV{'PATH'} - returns path