Archive

Posts Tagged ‘cgi’

What is CGI and How Can It Be Useful?

April 17th, 2009 admin 2 comments

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! :D </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! :D </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.

Categories: C/C++, Other Tags: , , , ,