[Ilugc] [TIP] CGI tutorial

  • From: girishvenkatachalam@xxxxxxxxx (Girish Venkatachalam)
  • Date: Sat, 19 Nov 2011 06:59:39 +0530

Dear all,

CGI stands for Common Gateway Interface.

It means nothing to us except that we have heard this being used for
 using executable programs with the web.

What is meant by the web?

It is a combination of HTML, HTTP and related technologies like CGI, Ajax and
 javascript to give the user a rich experience.

There is JSON and web APIs for sending data using HTTP between two
unconnected hosts.

All this takes time to learn. So I am going to focus on this theme for a week.

What is CGI?

It is a way to obtain dynamic data between the parent HTTP server and
the browser.

Whenever you visit a website using the http:// URL instead of the
file:/// URL even if you
 access a resource locally you are talking to a web server like Apache.

CGI is the only way to dish out freshly generated content using which
we can run any program on the
 web server and display it on a web page.

So CGI works within HTTP and HTML. There is no exception to this.

And this can only work between one browser and one server.

Now let us look at a very simple example.

Most CGI scripts are written in perl. You can use any language, even C
but perl is what we will use.

If you dunno perl you can use python or shell scripts. But perl used
to be the best choice long ago
 and I think it still is.

There are several frameworks built on top of CGI like Ajax(which we
will see very soon) and Catalyst.

But if you understand CGI you will know how

a) Google search works
b) online banking works
c) ticket reservation for air or train or bus
d) online purchase like eBay
e) Paypal

Nearly every website that offers a service with a DB backend uses CGI.

Usually CGI in the server is neatly handled by javascript in the
browser(browser is the OS for javascript)
 to give the user a thorough user experience.

We will conclude today's tip with a CGI sample.

# cat first.pl
#!/usr/bin/perl
use CGI;

$q = new CGI;

print $q->header;

This is the simplest CGI skeleton you can get.

After this preamble you can use any executable perl code like this.

$uname=`uname -a`;
print  $uname;

Instead of perl you can use shell like this:

#!/bin/sh

echo "Content-Type: text/plain";

echo " How are you?";

In the beginning you will have trouble. execute permissions, location
of CGI script,
 how the server invokes it etc.

Look at error_log in Apache's logs to find out. But once you cross
this stage we are
ready for tomorrow's CGI examples.

-Girish

-- 
G3 Tech
Networking appliance company
web: http://g3tech.in ?mail: girish at g3tech.in

Other related posts:

  • » [Ilugc] [TIP] CGI tutorial - Girish Venkatachalam