CGI Scripting – How it works

What is CGI Scripting and how can I use it on my site?
Common Gateway Interface (CGI) is a standard method used to generate dynamic content on Web pages and Web applications.
In plain words inside your web server you will find a folder www/cgi-bin, here you can store executable files.

An ultra simple example:

Browser send -> www.your.com/your.html -> Static Content
Browser send -> www.your.com/your.php -> Dynamic content PHP language
Browser send -> www.your.com/cgi-bin/your.cgi -> Dynamic Content PERL/C Language

CGI scripts can be PERL (.pl) or C (.c)

Your web hosting have to allow CGI scripts!

Hello there!

We will get the same output “Hello there!” using the codes below;

simplest.html:


<html>
  <body>
     <h1>Hello there!</h1>
  </body>
</html>

It prints:

Hello there!

simplest.c:


#include <stdio.h>

int main()
{
  printf("Content-type: text/html\n\n");
  printf("<html>\n");
  printf("<body>\n");
  printf("<h1>Hello there!</h1>\n");
  printf("</body>\n");
  printf("</html>\n");
  return 0;
}

I can use GCC (GNU Compiler Collection – https://gcc.gnu.org/) by saying: gcc simplest.c -o simplest.cgi, the output file is ‘simplest.cgi’.
I can upload simplest.cgi inside my server at www/cgi-bin and call it widt my browser typing www.your.com/cgi-bin/simplest.cgi

It prints:

Hello there!

Notice:


printf("Content-type: text/html\n\n");

The line “Content-type: text/html\n\n” is special piece of text that must be the first thing sent to the browser by any CGI script.
It sends: ‘Hey Browser, you have to consider that an html file’ :)
If you forget, the browser will reject the output of the script.

The perl code (http://perldoc.perl.org/index.html) here:

simplest.pl:


#! /usr/bin/perl
print "Content-type: text/html\n\n";
print "<html><body><h1>Hello World!";
print "</h1></body></html>\n";

Place the file into your cgi-bin directory. On a UNIX machine, it may help to also type:
chmod 755 simplest.pl

This tells UNIX that the script is executable.

Counter

count.c:


#include <stdio.h>

int incrementcount()
{
  FILE *f;
  int i;

  f=fopen("count.txt", "r+");
  if (!f)
  {
     sleep(1);
     f=fopen("count.txt", "r+");
     if (!f)
       return -1;
  }

  fscanf(f, "%d", &i);
  i++;
  fseek(f,0,SEEK_SET);
  fprintf(f, "%d", i);
  fclose(f);
  return i;
}

int main()
{
  printf("Content-type: text/html\n\n");
  printf("<html>\n");
  printf("<body>\n");
  printf("<h1>The current count is: ")
  printf("%d</h1>\n", incrementcount());
  printf("</body>\n");
  printf("</html>\n");
  return 0;
}

Compile it by typing: gcc count.c -o count.cgi

Create another text file named count.txt and place a single zero in it. By placing counter.cgi and count.txt in the cgi-bin directory, you can run the script. All that the script does is generate a page that says, “The current count is: X,” where X increments once each time you run the script. Try running it several times and watch the content of the page change!

The count.txt file holds the current count, and the little incrementcount() function is the function that increments the count in the count.txt file. This function opens the count.txt file, reads the number from it, increments the number and writes it back to the file. The function actually tries to open the file twice. It does that just in case two people try to access the file simultaneously. It certainly is not a foolproof technique, but for something this simple it works. If the file cannot be opened on the second attempt, -1 is the error value returned to the caller. A more sophisticated program would recognize the -1 return value and generate an appropriate error message.

Reference:
http://it.wikipedia.org/wiki/Common_Gateway_Interface
http://computer.howstuffworks.com/cgi.htm