CGI-Bin Program/Scripts



 

Definitions: CGI-Bin Programs and Scripts

Structure of a CGI Script

How to Call a CGI Script in HTML

What happens when you invoke a CGI Script

 
 



Definitions

CGI-Bin Program = is a program that runs on the same machine of the web-page that invokes it (i.e. the web-server). These programs can hold many purposes, they can process forms on the computer, they may search a database, they can tell you the time, etc. Your imagination is only the limit. There are two types of CGI-Bin's one is a program and the other is a script. The difference is that a program has been compiled or translated into the special binary (0's and 1's) language the web computer's CPU ("the brain") understands. On the other hand, a script (HTML is an example of a scripting language) is never compiled to binary language but, the computer interprets the language of the script at run-time (the time you ask the computer to run the script). Note that Java falls somewhere in between scripts and compiled languages.

Some of the compiled languages you can used to create CGI-Bin programs are: C, C++, Pascal, Fortran, Basic, Java Some of the scripting languages you can use to create CGI scripts are: Perl, Unix Bourne shell/C-shell, AWK, Java Script, AppleScript (Mac Only)
 
 
 


Structure of a CGI Script

We will discuss briefly some of the features you find in most scripts that will help you understand somewhat what is going on in a CGI script you may find on the Internet. There are many excellent and detailed books about Scripting that are language specific...check out your local library.

Typically, a CGI script will write some output to standard out (your computer screen) so that it can display the results of the processing to you. It prefaces any data it wants to display to you with a header.

The Output Header

CGI scripts must include as the first line the command used to run the scripting languages interpreter and the next line must be blank. Following this a series of header information may be given and this is followed by the data you wish to send. The header information may include: Content-type, Location or Status. Content-type is most always sent, if not the type teext/plain is assumed.
  1. Content-type
    • Use if script is sending data back to the browser... e.g. a newly generated HTML file.
    • Example: Content-type: text/html
    • OR Content-type: text/plain
    • OR Content-type: image/jpeg
    • OR Content-type: video/mpeg
    • OR Content-type: image/gif
    • You describe the content by the MIME types (see your book)
  2. Location
    • Use if script wants to open and load another existing document on its server
    • Can list full URL or relative path information.
    • Example: Location: http://newton.sci.csuhayward.edu/~login/cgi-bin
  3. Status
    • Used to send special status code back to the browser to influence how it should respond.
    • Example: Status: 204 No Response
      • This means the browser should not expect any input back from the server.
 

Example Simple Unix Bourne shell Script

Below is the Unix Bourne shell script to create and then display a simple HTML file that says thank you. This is a dummy example just to show you how such script would appear (we know we don't have to create a script to do this simple operation). Note that the word echo in C-Shell is a special command that says in essence "send the following information to the output".
#!/bin/sh

echo "Content-type:  text/html"

echo

echo "<HTML>"

echo "<HEAD><TITLE>Thanks</TITLE></HEAD>"

echo "<BODY>Thank You </BODY>"

echo "</HTML>"
Note that the content-type is HTML as you would expect. Also notice that there is a blank like produced by the second echo in the script...this is required to separate the header information from the main output of the script.

Example 2: Print the current date

The following script prints the current date to the screen preceded by the message the Current Date Is:
#!/bin/sh

echo "Content-type:  text/html"

echo

cat << EOF

<HTML>

<HEAD><TITLE>Date</TITLE></HEAD>

<BODY>The Current Date Is: 

EOF

/bin/date

cat << EOF

</BODY>

</HTML>

EOF
In the script above, the line /bin/date calls the Unix program that prints the current date. The lines cat << EOF are Unix commands that say echo everything from here to the symbol EOF..thus I did not need individual echo calls as in the example before.
 
  ADVANCED - More on Header Info

 


Calling a CGI-Bin Program or Script from inside an HTML file

Usually, your web-master will setup a special directory on your server in which you can save and create CGI-Bin Programs or Scripts. Thus your server will know when you are invoking a CGI-Bin program or Script to run. Another method of indicating a CGI script is by giving it a special file extension, .cgi

Below we will assume that you are using my account whose path is "public_html/~login/cgi" on our ebox server. If you have your own account YOU MUST substitute the correct path.

To call a script with no arguments

You can invoke a script in a few ways:
  • As part of a form (see your book for more details)
    E.g. <FORM METHOD=POST ACTION="cgi/getdate">
  • As the result of a hyper-text link

  • E.g. < a href="cgi/getdate"> Get Date </a>
 

To call a script with arguments

You can invoke a script in the same ways as above, but this time say you want to pass the arguments arg1, agr2, agr3,agr4. You do this by adding on to the scripts URL above a question mark ? separating the name of the script from the arguments and plus signs + separating each argument as shown below:
  • As part of a form ( see your book for more info):

  • E.g. <FORM METHOD=POST ACTION="/cgi/getdate?arg1+arg2+arg3+arg4">
  • As the result of a hyper-text link

  • E.g. < a href="/cgi/getdate?arg1+arg2+arg3+arg4"> Get Date </a>

  The Method attribute: POST or GET
 

GET

  • This is the default method and causes the fill-out form contents to be appended to the URL as if they were a normal query
  • . When the submit button is pressed, the contents of the form will be assembled into a query URL that looks like this: action?name=value&name=value&name=value
  • Specifically, your CGI program will receive the encoded form input in the environment variable QUERY_STRING.
  • Strange characters in any of the "name" or "value" instances will be escaped as usual; this includes "=" and "&". Note: This means that instances of "=" that separate names and values, and instances of "&" that separate name/value pairs, are not escaped.
  • For text and password entry fields, whatever the user typed in will be the value; if the user didn't type anything, the value will be empty but the "name=" part of the query string will still be present.
  • For checkboxes and radio buttons, the VALUE attribute specifies the value of a checkbox or radio button when it is checked. An unchecked checkbox is disregarded completely when assembling the query string. Multiple checkboxes can have the same NAME (and different VALUEs), if desired. Multiple radio buttons intended to have "one of many" behavior should have the same NAME and different VALUEs.
  • If you use the GET method in your form, you will notice that the example GET server will choke if too much data (more than a couple hundred bytes) is submitted at a time -- the server is passing the data to the form-processing module via a shell command line, and the maximum shell command line length is being exceeded. This problem does not exist with the POST method and server.

POST

  • This method causes the fill-out form contents to be sent to the server in a data body rather than as part of the URL.
  • The contents of the form are encoded exactly as with the GET method (above), but rather than appending them to the URL specified by the form's ACTION attribute as a query, the contents are sent in a data block as part of the POST operation. The ACTION attribute (if any) is the URL to which the data block is POSTed.
  • Specifically, your CGI program will receive the encoded form input on stdin. The server will NOT send you an EOF on the end of the data, instead you should use the environment variable CONTENT_LENGTH to determine how much data you should read from stdin.


 


What happens when you invoke a CGI Script from a Web page

  1. CLIENT request the URL of the web page that contains the form, etc. that links to the script.
  2. SERVER Sends back the web page with the Form.
  3. CLIENT you fill out the form and submit it. This invokes a call to the SERVER to invoke the Script and passes it the parameters.
  4. SERVER Invokes the Script and passes it the parameters. If results are to be displayed on the CLIENT end you must return HTML code to the CLIENT.
  5. CLIENT Displays any returned HTML.
 
 
 

 

© Lynne Grewe