What is CGI and How Can It Be Useful?
Well, to be honest, in the present day the CGI wrapper is used much less commonly with all the new web programming languages such as: PHP, Ruby, etc. However, CGI, to put it simply, is a kind of C wrapper for websites. The concept sounds confusing and the truth is, it is just like any other programming language.
Let’s begin discussing the uses of it. The most important use of it today is for hosts that are not PHP (or any other language) enabled. Most hosts allow CGI (even though if programmed wrong can make the system vulnerable) if they don’t allow PHP or some other advanced server-side scripting. Normally, these files need to be placed in the “cgi-bin” to execute. Hosts do this as a sort of precaution, trying to confine the areas of break-ins due to poor programming.
Actually using it is different now. If you’re writing a Perl script, there is no compilation needed, you write like normal. However, if you are a C programmer, such as myself, and wish to use C, compile the source as program.cgi. Now, I’ve dabbled with Perl and can program in it fairly decently, but I’d much rather use C. It’s much easier for me. So, I’ll provide source examples for both styles here!
Now, let’s look at an example script:
The first and most basic script to any sort of programming is “Hello World” so I’ll provide the source for each (and the compiled binaries in the package below for the C-version scripts)
hello_world.pl (This will run properly as is)
#!/usr/bin/perl
# Hello World CGI Script (Perl Version) by Dennis M.
#
# Remember now, comments in Perl must begin with the '#' character
## Our content type ##
print "Content-type: text/html\n\n";
## Now print the page! ##
print "<html>\n
<head><title>Hello World!
</title></head>\n
<body>\n
<h1>Hello World!</h1>\n
Hello World Script!\n
</body>\n
</html>";
and here is the C source (which, again, needs to be compiled)
hello_world.c
/**
* Hello World CGI Script (C Version) by Dennis M.
*
* I prefer this (although it needs to be compiled) because
* I'm a C programmer
*
* oh yeah, and because we're compiling it with GCC or whatever
* you use, we can use normal comments
*
*/
#include <stdio.h>
int main(){
// Our content type
printf("Content-Type: text/html;charset=us-ascii\n\n");
// Our page!
// We're going to break it up line by line so you can
// see what's going on here.
printf("<html>\n");
printf("<head><title>Hello World!
</title></head>\n");
printf("<body>\n");
printf("<h1>Hello World!</h1>\n");
printf("Hello world! C style
\n");
printf("</body>\n");
printf("</html>\n\n");
// Let's terminate the program now
return 0;
}
Now, we won’t go too deep into the code because simply, they are two completely different languages. But, it’s important to point out that both send headers (Content-type: text/html). You can Google content-type’s and figure what does what, but there are many different types. Another commonly used type is Content-type: text/plain. Notice, if this had been the case, no HTML would have been parsed by the server and everything would have been displayed as plain-text!
So that’s basically it
CGI is just a wrapper to run Perl and C programming on the web!
Here you can download the sources AND binaries:
CGI Tutorial – Hello World
One more tutorial left in the series! Keep sending in the questions so I have a few more topics to write about!
Regards,
Dennis M.
Here’s a somewhat improved version of the perl one:
#!/usr/bin/perl
# Hello World CGI Script (Perl/CGI Version) JR
#
## Have perl tell us if we make silly mistakes
use strict;
## Catch potential errors
use warnings;
## load the CGI module, importing the html methods
use CGI qw/:html/;
## output errors in the browser for testing
use CGI::Carp qw(fatalsToBrowser);
## Create a CGI object
my $cgi = CGI->new();
## Our content type ## (defaults to text/html)
print $cgi->header();
## Now print the page! ##
’), # start the HTML
print $cgi->start_html(‘Hello World!
$cgi->h1(‘Hello World!’), # level 1 header
‘Hello World Script’, # plain text
$cgi->end_html; # end the HTML
See also: http://search.cpan.org/~lds/CGI.pm-3.43/CGI.pm
Jess
Hi, good post. I have been wondering about this topic,so thanks for writing. I’ll likely be subscribing to your posts. Keep up the good work