What does CGI stand for / what is CGI

CGI stands for the Common Gateway Interface. It is the name given to scripts which can be executed from within pages of the world wide web. Although it is possible to use any language in CGI programs (hence the word `common'), the usual choice is Perl, because of the ease with which Pen can handle text.The CGI interface is pretty unintelligent, in order to be as general as possible, so we need to do a bit of work in order to make scripts work.

Protocols

CGI script programs communicate with W3 browsers using a very simple protocol. It goes like this:

  • A web page sends data to a script using the 'forms' interface. Those data are concatenated into a single line. The data in separate fields of a form are separated by ‘&’ signs.

New lines are replaced by the text %OD%OA, which is the DOS ASCII representation of a newline, and spaces are replaced by `+' symbols.

  • A CGI script reads this single line of text on the standard input.
  • The CGI script replies to the web browser. The first line of the reply must be a line which is tells the browser what mime-type the data are sent in. Usually, a CGI script replies in HTML code, in which case the first line in the reply must be.

Content-type: text/html

This must be followed by a blank line.

HTML coding of forms

To start a CGI program from a web page we use a form which is a part of the HTML code enclosed with the parentheses

<FORM method="POST" ACTION="/cgi-script-alias/program.pl> 
</FORM>

The method 'post' means that the data which get typed into this form will be piped into the CGI program via its standard input. The 'action' specifies which program you want to start. Note that you cannot simply use the absolute path of the file; for security reasons.

Within these parentheses, you can arrange to collect different kinds of input. The simplest kind of input is just a button which starts the CGI program. This has the form

<INPUT TYPE=”submit" VALUE="Start my program">

This code creates a button. When you click on it the program in your 'action' string gets started. More generally, you will want to create input boxes where you can type in data. To create a single line input field, you use the following syntax:

<INPUT NAME="variable-name" SIZE=40>

This creates a single line text field width 40 characters.

<TEXTAREA NAME-"variable-name" ROW=50 COL5=50>

which means: create a text area of fifty rows by fifty columns with a prompt to the left of the box. Again, the size has only to do with the visual formatting, not to do with limits on the amount of text which can be entered.

As an example, let's create a WWW page with a complete form which can be used to make a guest book, or order form.

<HTML> 
  <HEAD> 
   <TITLE>Example form</TITLE>
   <! -- Comment : Mark Burgess, 27-Jan-1997 -->
   <LINK REV-"made" HREF="mailto:mark@iu.hio.no">
  </HEAD>
  <BODY>
   <CENTER><H1>Write in my guest book ...</H1></CENTER>
   <HR>
   <CENTER>
   <H1>Please leave a comment using the form below.</H2>
   <P> 
    <FORM method="POST" ACTION="/cgi-bin-mark/comment.p1"›
     Your Name/e-mail: <INPUT NAME="variablel" SIZE=40><BR><BR>
     <P>
     <TEXTAREA NAME="variable2" cols=50 rows=8></TEXTAREA>
     <P>
     <INPUT TYPE=submit VALUE="Add message to book"› 
     <INPUT TYPE=reset VALUE="Clear message"› 
    </FORM>
   <P>
  </BODY> 
</HTML>

The reset button clears the form. When the submit button is pressed. The CGI program activated.

PHP and the web

PHP code can be embedded inside HTML pages provided your WWW server is configured with PHP support. PHP code lives inside a tag with the general form

For example, we could use this to import one file into another and print out a table of numbers:

<html>
   <body>
   <?php
     include "file.html"
     for ($i=0; $i < 10; $i++)
     {
        print "Counting $i<br>";
     }
   ?>
   </body> 
</html>

This makes it easy to generate WWW pages with a fixed visual layout:

<?php 
# Standard layout
# Set $title, $comment and $contents 
# print "<body>\n"; 
print "<img src..img/header.gif>"; 
print "<hl>"$title</hl>"; 
print "<em4comment</em>"; 
print "<blockquote>\n"; 
include $contents; 
print ("</blockquote>\n"); 
print ("</body>\n"); 
print (“</html>\n"); 

Variables are easily set by calling PHP code in the form of a CGI program from a form.

PHP and forms

PHP is particularly good at dealing with forms, as a CCI scripting language. Consider the following form:

<html>
<body>
  <form action="/cgi-bin-scriptalias/spititout.php"method="post">
   Name:<input type="text"name="personal[name]"><br> 
   Email: <input type="text" name="personal[email]"><br> 
   Preferred language:
   <select multiple name="language[]">
     <option value="English">English
     <option value="Norwegian">Norwegian 
     <option value="Gobbledigook">Gobbledigook 
   </select>
   <input type=image src="image.gif" name="sub"›
  </form> 
</body> 
</html>

This produces a page into which one types a name and email address and chooses a language from a list of three possible choices. When the user clicks on a button marked by the file 'image.gif' the form is posted. Here is a program which unravels the data sent to the CGI program:

#!/local/bin/php 
<?php 
# A CGI program which handles a form 
# Variables are translated automatically
$title = "This page title"; 
$comment = "This pages talks about the following....”;
echo "<body>";
echo "<hl>$title</hl>";
echo "<em>$comment</em>"; 
echo "<blockquote>\n";
echo "Your name is $personal[name]<br><br>";
echo "Your email is $personal[email]<br><br>";
echo "Language options:"; 
echo "<table>”;
for ($i = 0; strlen($language[$i]) > 0; $i++)
{
   echo "<tr><td bgcolor=#ff0000>Variable language[$i] =$language[$i]</td></tr>";
}
if ($language[0]=="Norwegian") 
{
  echo "Hei alle sammen<p>";
}
else 
{ 
  echo "Greetings everyone, this page will be in English<p>";
}
echo "</table> ";
echo ("</blockquote>\n");
echo ("</body>\n"); 
echo ("</html>\o"); 
?>

A complete PHP guestbook

If your web-server supports PHP there is no need for separate CGI-scripts handling form output. A single PHP-script can create the form and handle the output simultaneously. In addition this script can be placed wherever the web-server is able to read HTML files. PHP defines a special variable $PHP_SELF which provides the action= assignment of the form with the script itself. Moreover such PHP-scripts checks whether the user has submitted any data by checking if the variables of the form is set with the command isset ().

<html>
<head>
<title>My Guestbook</title>
</head>
<body>
  <h1>Welcome to my Guestbook</h1>
  <h2>Please write me a little note below</h2>
  <form action="<?echo $PHP_SELF?>" method="POST">
       <textarea cols=40 rows=5 name=note wrap=virtual></textarea> <input type=submit value="Send it">
  </form>
<?php
$file= "/iu/nexus/ud/haugerud/www/cgi-out/guestbook.txt"; if(isset($note))
{
  $date = date("F j, Y, G:i");
  $buffer = "<h3>Message sent from IP-address $REMOTE_ADDR</h3>\n"; $buffer .= "   <h4>$date</h4>\n";
  $buffer .= n12br($note).'<br>';
  $handle = fopen ($file, "r"); 
  while (!feof($handle))
  {
     $buffer .= fread($handle,4096);
  }
  fclose($handle);
  $outhandle=fopen ($file,"w"); 
  fputs($outhandle,$buffer); 
  fclose($outhandle);
}
?>
<h2>The entries so far:</h2>
<? neadFile($file) ?>
</body> 
</html>