A Introduction to Common Gateway Interface (CGI)
by K. Yue, copyright 2000
Revised: September 17, 2000

Introduction

Data From Web Server To Gateway Programs Dynamic HTML Contents from Gateway Program To Web Servers Data as Command Line Arguments Example (adapted from the HTML sourcebook).

#!/bin/sh
echo Content-TYPE:     text/html
echo
if [ $# = 0 ]           # is the number of arguments == 0 ?
then                    # do this part if there are NO arguments
   echo "<HEAD>"
   echo "<TITLE>Local Phonebook Search</TITLE>"
   echo "<ISINDEX>"
   echo "</HEAD>"

   echo "<BODY>"
   echo "<H1>Local Phonebook Search</H1>"
   echo "Enter your search in the search field.<P>"
   echo "This is a case?insensitive substring search: thus"
   echo "searching for 'ian' will find 'Ian' and Adriana'."
   echo "</BODY>"
else                    # this part if there ARE arguments
   echo "<HEAD>"
   echo "<TITLE>Result of search for \"$*\".</TITLE>"
   echo "</HEAD>"
   echo "<BODY>"
   echo "<H1>Result of search for \"$*\".</H1>"
   echo "<PRE>"
     for i in $*
     do
         grep ?i $i /users/ns-home/docs/yue/phonebk.dat
     done
   echo "</PRE>"
   echo "</BODY>"
fi

Data Passed By Environment Variables

Example:

#!/bin/sh
echo Content?TYPE:  text/html
echo
echo "<HTML>"
echo "<HEAD>"
echo "<TITLE>Not Really A Search</TITLE>"
echo "<ISINDEX>"
echo "</HEAD>"
echo "<BODY>"
echo "<H1> The Environment Variables </H1>"
echo "<PRE>"     # print the environment variables
echo " SERVER_SOFTWARE = $SERVER_SOFTWARE"
echo " SERVER_NAME = $SERVER_NAME"
echo " GATEWAY_INTERFACE = $GATEWAY_INTERFACE"
echo " SERVER_PROTOCOL = $SERVER_PROTOCOL"
echo " SERVER_PORT = $SERVER_PORT"
echo " REQUEST_METHOD = $REQUEST_METHOD"

echo " HTTP_ACCEPT = $HTTP_ACCEPT"
echo " PATH_INFO = $PATH_INFO"
echo " PATH_TRANSLATED = $PATH_TRANSLATED"
echo " SCRIPT_NAME = $SCRIPT_NAME"
echo " QUERY_STRING = $QUERY_STRING"
echo " REMOTE_HOST = $REMOTE_HOST"
echo " REMOTE_ADDR = $REMOTE_ADDR"
echo " REMOTE_USER = $REMOTE_USER"
echo " AUTH_TYPE = $AUTH_TYPE"
echo " CONTENT_TYPE = $CONTENT_TYPE"
echo " CONTENT_LENGTH = $CONTENT_LENGTH"
echo "</PRE>"
echo "</BODY>"
echo "</HTML>"

Exercise 1:

Consider a file that stores data in the following format for each line.

Name;password;h1-grade;h2-grade;h3-grade;final-grade

For example:

Kwok-Bun Yue;mint;89;92;95;A

indicates that Kwok-Bun Yue’s password is "mint" and have grades 89, 92, 95 and A respectively.

Write the HTML file and the cgi Perl program that allows a student to type in their names and passwords to find out their grades.

Data Passed By Standard Input