8.3.1 A Very Simple CGI Program: Counting
8.3.1 A Very Simple CGI Program: Counting
We start with a CGI program that can be called directly from the Location field of a browser, or from an HREF link on a Web page. It is not called from an HTML form as is usually the case with most CGI programs. For a CGI program to work, it has to be placed in one or more pre-determined places in the computer that contains the Web server. One must ask one’s system administrator what such a location is. The locations can be obtained by looking at the Web server configuration file as well. Assuming we know the location, it is possible to call a CGI program directly by typing in its
URL in the browser.
Assume the server is pikespeak.uccs.edu. pikespeak.uccs.edu is a Red Hat Linux machine whose system CGI directory is /var/www/cgi-bin as discussed in Section 8.2. In this directory, we can create soft link to a designated directory in user kalita’s home directory. The home directory for the user kalita, viz., /home/kalita is also, called
~kalita in abbreviated form in Unix. In the home directory, we create a sub-directory, say public_html/cgi-bin if it does not already exist. On a Unix machine, we can now create a soft link using the ln command with the -s option.
%ln -s /var/www/cgi-bin/kalita ~kalita/public_html/cgi-bin
The first argument is the name of the new link we create, and the second argument is the existing file or directory to which the link refers. The ln command links the kalita name in the system CGI directory (/var/www/cgi-bin) to the sub-directory public_html/cgi-bin in kalita’s home directory. Now, when we use a URL such as
http://pikespeak.uccs.edu/cgi-bin/kalita/perlbook/countdown.pl on a Web browser, the server knows right away it is a CGI program due to the presence of the sub-string cgi-bin in the URL, and the URL is translated by the pikespeak.uccs.edu server to the path
/home/kalita/public_html/cgi-bin/countdown.pl in order to execute the CGI program.
Let us assume that we invoke a simple CGI program by typing
http://pikespeak.uccs.edu/cgi-bin/kalita/perlbook/countdown.pl. The output this program produces on a browser is given in Figure 8.3.
Figure 8.3: Counting in a CGI Program
Program 8.1
#!/usr/bin/perl #countdown.pl print "Content-type: text/html", "\n\n"; print "", "\n"; print "Simply counting... ", "\n"; print "Simply counting...
"; print "", "\n"; $countdown = 10; while ($countdown != 0) { print "$countdown...\n"; print "
", "\n";
", "\n"; --$countdown; } print "
Usually, a CGI program must have the location of the Perl interpreter as a comment on the first line starting from the first column. The Apache Web server requires such a line whether it is being used in the Unix or Windows environment.
Whatever a CGI program prints to the standard output is sent over by the Web server to the browser. When a Web page comes to a Web browser, the first one or more lines must be HTTP headers so that the Web browser knows how to display the page. This is the standard way an HTTP server communicates with an HTTP browser. This is not something special about a Web document produced by a CGI program. There could be quite a bit of header information that the Web server sends to the browser. Each header is one single line by itself and contains the name of a header field and its value. At a minimum, the server must tell the Web browser the kind of content being returned so that the browser
knows how to display the information appropriately. In this program, we have only one header being returned.
Content-type: text/html
This line says that the content being sent is text and it follows HTML syntax. It is important to know that the headers returned must follow the header syntax in the HTTP protocol. The last header line must be followed by a blank line. This blank line separates the headers from the actual content of the page. Here, there is only one line of header and this line is followed by two newline characters or a single blank line.
Next, a CGI program must produce the contents of the document to be displayed. In this case, the CGI program produces simple HTML content, but it is conceivable that a CGI program produces XML content, or even graphic content. The CGI program creates HTML content by printing to the standard output HTML tags and text. An HTML document is enclosed within <HTML> and </HTML> tags. The tags are case-insensitive. An HTML page is composed of a head section enclosed within <HEAD> and
</HEAD> tags, and a body enclosed within <BODY> and </BODY> tags. The head part usually contains a title to be displayed on the title bar of the browser. In the document produced by this program, the body repeats the title within <H1> and </H1> tags, creating a first-level header. The body of the Web page is produced inside the while loop. <P> is a paragraph break, and <BR> is a newline. In the HTML, we must use the HTML tag <BR> or <P> to break a line. The Perl newline character \n does not produce a line break in HTML because the browser considers it as space in HTML and ignores it. Once the body is produced, the program
produces the terminating tags. Thus, a CGI program is like any other Perl program except that it must produce HTTP headers before it does any other writing to standard output.
