Concept of CGI
CGI stands for Common Gateway Interface, and couples your web page to a program which does the desired task for you. Now you can make dynamic web pages, pages that can interact with the user. You can think of forms, quizzes, counters, a bulletin board system, you name it! CGI is intended to be platform and language independent.
There are variety of popular server- side technologies for developing Web- based applications. Historically, the most widely used has been CGI.CGI let’s HTTP(Hyper transfer protocol) clients interact with programs across a network through a web server. CGI is a standard for interfacing applications with a web browser. These CGI applications can be written in many different programming languages. Permission is granted within the web browser by a web master(or author of web site) to allow specific programs to be executed on the web server. Typically, CGI applications reside in the directory /cgi –bin.
CGI is not a language. It's a simple protocol that can be used to communicate between Web forms and your program. A CGI script can be written in any language that can read STDIN, write to STDOUT, and read environment variables, i.e. virtually any programming language, including C, Pearl, or even shell scripting.
It is not a programming language. That means, for example:
- You do not have to learn Pearl
- You can use the languages you already know
- You can use any language as long as it
- can read input
- can write output
And what computer language can’t?
- For that matter, you do not need to use a language.
- It is not a programming style. You can use your own.
- It is not cryptic. Pearl is cryptic, all right, but see above: You don’t need to use Perl.
- It is not for Unix gurus only. In fact, you don’t have to be any kind of guru. All you need is to know how to program. And you already know that!
Structure of a CGI Script
Here's the typical sequence of steps for a CGI script:
- Read the user's form input.
- Do what you want with the data.
- Write the HTML response to STDOUT.
Reading the User's Form Input
"Name1=value1&name2=value2&name3=value3"
So just split on the ampersands and equal signs. Then, do two more things to each name and value:
Convert all
"+"
characters to spaces, and
Convert all
"%xx"
sequences to the single character whose ASCII value is
"xx"
, in hex. For example, convert "%3d" to "=".
This is needed because the original long string is URL-encoded, to allow for equal signs, ampersands, and so forth in the user's input.
So where do you get the long string? That depends on the HTTP method the form was submitted with:
For GET submissions, it's in the environment variable QUERY_STRING.
For POST submissions, read it from STDIN. The exact number of bytes to read is in the environment variable CONTENT_LENGTH.
Sending the Response Back to the User
First, write the line
Content-type: text/html
plus another blank line, to STDOUT. After that, write your HTML response page to STDOUT, and it will be sent to the user when your script is done. That's all there is to it.
Yes, you're generating HTML code on the fly. It's not hard; it's actually pretty straightforward. HTML was designed to be simple enough to generate this way.
If you want to send back an image or other non-HTML response:
For image contents:
Content-type: image/gif
GIF89a&%*$@#--- binary contents of GIF file here ---$(*&%(*@#......
Let's write a simple first program. Enter the following lines into a new file, and name it "first.pl".
#!/usr/bin/perl
Print "Hello, world!\n";
Save the file. Now, in the Unix shell, you'll need to type:
chmod 755 first.pl
This changes the file permissions to allow you to run the program. You will have to do this every time you create a new script; however, if you're editing an existing script, the permissions will remain the same and won't need to be changed again.
Now, in the Unix shell, you'll need to type this to run the script:
./first.pl
If all goes well, you should see it print Hello, world! to your screen.
A CGI program is still a Pearl script. But one important difference is that a CGI usually generates a web page (for example: a form-processing CGI, such as a guestbook, usually returns a "thank you for writing" page.) If you are writing a CGI that's going to generate a HTML page, you must include this statement somewhere in the script, before you print out anything else:
print "Content-type:text/html\n\n";
This is a content header that tells the receiving web browser what sort of data it is about to receive - in this case, an HTML document. If you forget to include it, or if you print something else before printing this header, you'll get an "Internal Server Error" when you try to access the CGI. A good rule of thumb is to put the Content-type line at the top of your script (just below the #!/usr/bin/perl/ line).
Now let's take our original first.pl script, and make it into a CGI script that displays a web page. If you are running this on a Unix server that lets you run CGIs in your public_html directory, you will probably need to rename the file to first.cgi, so that it ends in the .cgi extension. Here is what it should look like:
#!/usr/bin/perl
print "Content-type:text/html\n\n";
print "Test Page\n";
print "\n";
print "
Hello, world!
\n";
print "\n";
to run this Just move it into your public_html or CGI-bin directory, and type the direct URL for the CGI
the output would be