WebDevelopersJournal.comTips on Web Page Design, HTML and Graphics
SITE SEARCH
Newsletters
Java/Open Source Daily



Jobs at webdeveloper.com

Resources By Subject
Technical
Graphical
Authoring
Business
WDJ resources
Archive

internet.com

internet.commerce
  • Partner With Us
















Developer Channel


Find a web host with:
CGI Access DB Support Telnet Access
NT Servers UNIX Servers



Semi-automatic?

JavaScript
JavaScript Helper:
Meet Paige Turner, the least geeky geek we've ever come across.

Variables and Operators Explained:
First of a three part guide to JavaScript basics.

Controlling Forms:
Enhance your HTML forms with a touch of JS.

DHTML:
Forget how it works, let's see some in action!


CGI Programming Made Overly Simple

by Gary Lee

With about ten lines (or two with CGI.pm), you can write a functioning CGI program that is the basis of ALL CGI programs, big or small.

Alas, my four years of studying Basket Weaving prevents me from breaking into that group. Nevertheless, I'll try to shed some light on the subject of CGI programming. My main qualification is that I'm writing this for free.
September 18, 1997

There is a certain clique-mentality among programmers, built around years of torment in high school from the "jocks", the "bad kids", the "music students", and even from the "glee club".

Many CGI programs are written in perl, a programming language suited to manipulating text and files. Let's look at a very simple perl CGI program line-by-line to get a little feel of perl, CGI, and to fill my word quota.

This program will read input from an HTML form...something like:

Your input :
and return what you typed in each field.

This is the first line of the perl program:

#!/usr/bin/perl

It tells the computer where the perl interpreter is located. Every instruction that follows will be shoved into the interpreter, causing it to perform accordingly.

print "Content-type: text/html\n\n";

This tells whatever web browser is accessing this program to get ready for some HTML, so it can display something instead of acting like a blank screen saver. (Note: I prefer Netscape's blank screen to Explorer's blank screen.)

read(STDIN,$input,$ENV{'CONTENT_LENGTH'})

"STDIN" is short for "standard input"". Just like you have a mouth as standard input for food, as opposed to your nose or something more imaginative, programs have a standard input to shove in information. So, the web server shoves information from an HTML form into this standard input, the program reads the amount of info (stored in a special variable called $ENV{'CONTENT_TYPE'}) and copies it to "$input". All simple variables in perl start with a dollar sign.

foreach (split(/&/, $input))
{
    ($NAME, $VALUE) = split(/=/, $_);
    $NAME =~ s/\+/ /g;
    $NAME =~ s/%([0-9|A-F]{2})/pack(C,hex($1))/eg;
    $VALUE =~ s/\+/ /g;
    $VALUE =~ s/%([0-9|A-F]{2})/pack(C,hex($1))/eg;
    print "$NAME : $VALUE <BR>\n";   
}

This wee section digests the info from an HTML form Each input field in the form has a NAME. For example, the HTML for the input field above is:

<INPUT NAME="yourinput">

So, if you type "How do you pronounce meep?" into the input box, the web server will package it up according to the rules of CGI into a single continuous line containing the name and value you typed:

yourinput=How+do+you+pronounce+meep%3F

See how the name of the input field comes first, followed by an equal sign and its value? Also, notice how the spaces have been replaced by plus signs, and the question mark has been replaced by "%3F. This is not some obscure code...it's the world-famous ASCII code. Some weirder (i.e. non-alphanumeric) characters are replaced by a percent sign followed by their ASCII value. The question mark is number 63 on the ASCII table. In hexidecimal numbers, that's 3F. Voila.

Now, if there were two input fields, they would be packaged up into one gigantic line separated by an ampersand. For example:

yourinput=How+do+you+pronounce+meep (no break)
%3F&yourinput2=who+put+all+those+things+in+your+head

Well, the code (let me reprint it):

foreach (split(/&/, $input))
{
    ($NAME, $VALUE) = split(/=/, $_);
    $NAME =~ s/\+/ /g;
    $NAME =~ s/%([0-9|A-F]{2})/pack(C,hex($1))/eg;
    $VALUE =~ s/\+/ /g;
    $VALUE =~ s/%([0-9|A-F]{2})/pack(C,hex($1))/eg;
    print "$NAME : $VALUE <BR>\n";   
}

splits the input along the ampersands. Then, you end up with separate name=value elements. For each one of them, you split again...this time along the equal signs. During the second split, the stuff before the equal sign is assigned to the variable "$NAME", and the stuff after is assigned to "$VALUE".

Next comes what perl is famous for...text manipulation. For both "$NAME" and "$VALUE", all plus signs are substitued with spaces, and anything that matches the pattern "%xx" is replaced by the character who's ASCII value is "xx".

Finally, it prints out the name stored in "$NAME", and the value stored in "$VALUE":

yourinput : How do you pronounce meep?

And now you can write any other CGI program you want, simply by tacking stuff onto this.

Do you have to know all this? Well, not really. But it's nice to know what is going on. With the latest version of perl (perl 5), you can get something called CGI.pm, which does all the reading and substituting for you.

But the main point is, it's not particularily hard to write the mysterious CGI program. With about ten lines (or two with CGI.pm), you can write a functioning CGI program that is the basis of ALL CGI programs, big or small.

The secondary point is, I've filled my quota of words.

Gary Lee is a co-owner of meep! media inc., an Internet and Intranet consulting company. He is also one of the programmers, and creator of meep! media's first product, meep!Board, a message board system. Gary is the Editor of Web Tech, one of meep! media's online publications.
Suits PonytailsPropheadsContact WDJDiscussWeb AudioSearch


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers