Dissecting the simplest CGI script: Hello Net! |
| This is a very simple CGI script: #!/bin/sh #Filename: Hello.sh.cgi echo "<HTML><HEAD><TITLE>Hello Net!</TITLE></HEAD>" #HTML body ECHO "<BODY><H1>Hello Net!</H1></BODY></HTML>" ExplanationFirst of all you have to know that a CGI script usually:
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"
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 #HTML body ECHO "<BODY><H1>Hello Net!</H1></BODY></HTML>" |
Index Home Back About Contact us!
Copyright (c) 1998-2006 Wowarea