Dissecting the simplest CGI script: Hello Net!


This is a very simple CGI script:

#!/bin/sh

#Filename: Hello.sh.cgi
echo "Content-type: text/html"
echo

echo "<HTML><HEAD><TITLE>Hello Net!</TITLE></HEAD>"

#HTML body

ECHO "<BODY><H1>Hello Net!</H1></BODY></HTML>"

Explanation

First of all you have to know that a CGI script usually:

  1. Read input (usually by means of a form)
  2. Do something
  3. Send output to the client (the web browser)

Ok, now let's see the first row: #!/bin/sh

The first symbol (#) is just a special character used to define a comment line, and the script interpreter won't execute it. However this is a very special row, and you have to write it inside of ALL CGI scripts. If you omit this row, well, simply you can't run the script. In fact this row specifies to the web server WHAT interpreter it has to use and WHERE it can find it. In this example you are specifying that you want to use the Bourne Shell language (written by Steve Bourne) by means of 'sh' and that language interpreter is located in this directory: '/bin' (this is a special directory where are located several Unix command). Well, I've just said that '#' defines a comment. It's right, but when you use this symbol: '!' it's not true anymore...So by means of '#!/bin/sh' you are saying: 'hey, server! Use the Bourne Shell interpreter to run this script! You can find it at /bin...'.

Well, the second line is just a comment, but the third line: echo "Content-type: text/html"
just PRINT the line 'Content-type: text/html' on the stdout (standard output). We are talking about Unix systems and here there is a special file named 'standard output' that usually indicates the screen. Well it isn't really so, in fact a web server is composed by: the operating system (usually Unix) the web server (another piece of software) and eventually CGI scripts. The CGI script print data to the standard output (the screen on Unix systems) but the web server reads the script's output. You have't to know Unix details to build a simple CGI script, you have just to know that you have to follow some rules (in fact CGI are only a set of rules...). So the CGI script is saying to the web browser that it is going to send a simple text file, and that file is an HTML document by means of the Content-type header (if you don't specify that, the web browser will read it as a simple ASCII file, and you won't see a web page). Here some common file formats:

HTML text/html
Text text/plain
GIF image/gif
JPEG image(jpeg
PostScript application/postscript
MPEG video/mpeg

The formats specified above are also called MIME types. MIME stands for Multipurpose Internet Mail Extension and was originally developed to expand the email system features. There are 3 kind of header: Content-type, Location and Status. Sometimes you haven't to build an HTML page 'on fly', because you can send to the web browser a previously written web page. So you can specify to the web server where it can find that page. In this case you can use the 'location' header. For example: 'Location: ../pages/mypage.html'. Obviously, if you use this header, you haven't to use the 'content-type' header. Sometimes you haven't to send anything at all to the web browser, so you can send a line like this: 'Status: 204 No Response'. In this case the web browser will know that it has to wait no response from the server.

The fourth line is needed to print a couple of newline. This is a rule: after content-type you have to print 2 newline (echo print 1 newline, so the previous line - the third - write the first newline, and this line - the fourth - print the second newline). This because the web server uses 2 newline to understand the end of the header (that maybe content-type, location or status).

Finally there are the HTML tags that define the web page showing: Hello Net!

The following is the same script written in PERL:

#!/usr/bin/perl
#Filename: hello.pl
print "Content-type: text/html"

#HTML body

ECHO "<BODY><H1>Hello Net!</H1></BODY></HTML>"


Index           Home  Back       About  Contact us!

Copyright (c) 1998-2006 Wowarea