CGI-Bin Program/ScriptsDefinitions: CGI-Bin Programs and ScriptsStructure of a CGI ScriptHow to Call a CGI Script in HTMLWhat happens when you invoke a CGI Script
DefinitionsCGI-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 ScriptWe 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 HeaderCGI 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.
Example Simple Unix Bourne shell ScriptBelow 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 dateThe 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> EOFIn 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 fileUsually, 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, .cgiBelow 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 argumentsYou can invoke a script in a few ways:
E.g. < a href="cgi/getdate"> Get Date </a> To call a script with argumentsYou 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:
E.g. <FORM METHOD=POST ACTION="/cgi/getdate?arg1+arg2+arg3+arg4"> E.g. < a href="/cgi/getdate?arg1+arg2+arg3+arg4"> Get Date </a> The Method attribute: POST or GET
What happens when you invoke a CGI Script from a Web page
|
||
© Lynne Grewe |