Previous Page Next Page

16.4. How HTML and CGI Work Together

As previously discussed, HTML is the markup language used to determine the way a Web page will be displayed. CGI is a protocol that allows the server to extend its functionality. A CGI program is executed on behalf of the server mainly to process forms, such as a registration form or a shopping list. If you have purchased a book or CD from Amazon.com, you know what a form looks like. When a browser (client) makes a request of the server, the server examines the URL. If the server sees cgi-bin as a directory in the path, it will go to that directory, open a pipe, and execute the CGI program. The CGI program gets its input from the pipe and sends its standard output back through the pipe to the server. Standard error is sent to the server's error log. If the CGI program is to talk to the server, it must speak the Web language, since this is the language that is ultimately used by the browser to display a page. The CGI program then will format its data with HTML tags and send it back to the HTTP server. The server will then return this document to the browser, where the HTML tags will be rendered and displayed to the user.

16.4.1. A Simple CGI Script

Before writing more sophisticated dynamic pages, we will look at a simple CGI script written in Perl and executed by the Apache server. The date and time will change each time this script is executed in the browser. The script consists of a series of print statements, most of which send HTML output back to stdout (piped to the server) as depicted in Figure 16.6. Some things to consider that make this script different from ordinary Perl scripts:

This program is executed directly from the CGI directory, cgi-bin.

Figure 16.6. The client/server/CGI program relationship.


The first line in the script is the called the "shbang" line and tells the server where your Perl interpreter is installed. Any error in this line causes an "Internal Server Error."

The next important line in the CGI script is the header line, called the HTTP MIME header. It tells the browser what type of output the program is sending in this example; "Content-type: text/html" is sent to the browser to announce that both regular text and HTML tags are contained within the document. We will go into more detail later in this chapter.

Right after the header line, there must be a blank line. This can be accomplished by ending the line with "\n\n", or if in a here document, providing a blank line as shown in a simple Perl CGI script created in a text editor

Example 16.6.

1 #!c:/ActivePerl/bin/perl.exe
2 $now=localtime;
  $myname="Willie";
3  print <<EOF;
4  Content-type:text/html
5   <body bgcolor="silver">
     <div align="center">
     <font face="arial" size="+2" color="darkblue">
     <b>Welcome,  Wee $myname!<p>
     <img src="/Williewonker.jpg" width="200"
          height="230" border="1">
     <p>
6    Today is $now<br>
    </body>
7 EOF

Explanation

  1. This first line is critical. Many Web servers will not process a CGI script if this line is missing. It tells the server where Perl is installed.

  2. Two scalar variables are defined: $now will receive the return value from Perl's localtime function. Each time this program is executed, the time will change, making the page dynamic. The variable $myname is assigned the string "Willie".

  3. This line starts a here document (see Chapter 2). The here document provides a blocked form of quoting, allowing us to use just one print function to print multiple lines. EOF is the user-defined terminator that is matched on line 7. All text between the two EOF markers will be sent as output and displayed in the browser.

  4. This line is called the MIME header. No matter what programming language you are using, the first output of your CGI program must be a MIME header followed by two newlines. This line indicates what type of data your application will be sending. In this case, the CGI script will be sending HTML text back to the server. The \n\n cause a blank line to be printed. The blank line is also crucial to success of your CGI program. All headers require a blank line before the document's content.

  5. This is text mixed with HTML tags. The browser will render the tags to provide the HTML page.

  6. The value of the variable will change each time this program is executed, indicating the time and date coming from the server side.

  7. The EOF marks the end of the here document.

Figure 16.7. Output of the Perl CGI script in the browser.


Some servers require that CGI script names end in .cgi or .pl so they can be recognized as CGI scripts. After creating the script, the execute permission for the file must be turned on for UNIX systems by typing at the shell prompt:

chmod 755 <scriptname>

or

chmod +x <scriptname>

The URL entered in the browser Location window includes the protocol, the name of the host machine, the directory where the CGI scripts are stored, and the name of the CGI script. The URL will look like this:

http://localhost/cgi-bin/cgisimple.pl

The HTTP Headers

The first line of output for most CGI programs is an HTTP header that tells the browser what type of output the program is sending to it. Right after the header line, there must be a blank line and two newlines. The two most common types of headers, also called MIME types (which stands for multipurpose Internet extension), are "Content-type: text/html\n\n" and "Content-type: text/plain\n\n". Another type of header is called the Location header, which is used to redirect the browser to a different Web page. And finally, Cookie headers are used to set cookies for maintaining state; that is, keeping track of information that would normally be lost once the transaction between the server and browser is closed. Right after the header line, there must be a blank line. This is accomplished by ending the line with \n\n in Perl.

Table 16.5. HTTP Headers
HeaderTypeValue
Content-type:text/plainPlain text
Content-type:text/htmlHTML tags and text
Content-type:image/gifGIF graphics
Location:http://www....Redirection to another Web page
Set-cookie: NAME=VALUE...CookieSet a cookie on a client browser


16.4.2. Error Log Files

Error Logs and STDERR

Normally, error messages are sent to the terminal screen (STDERR) when something goes wrong in a Perl script, but when launched by a server as a CGI script, the errors are not sent to the screen but to the server's error log file. In the browser you may see "Empty Document" or "Internal Server Error," which tells you nothing about what went wrong in the program.

Always check your syntax at the shell command line with the -c switch before handing the script to the server. Otherwise, you will not see your error messages unless you check the log files. Check the syntax of your Perl scripts with the -c switch.

Example 16.7.

(At the Command line)
1   perl -c perlscript
2   perlscript syntax OK

Example 16.8.

(Perl syntax errors shown in the Apache server's error log)

    [Mon Jul 20 10:44:04 2006] access to /opt/apache_1.2b8/
        cgi-bin/submit-form failed for susan, reason: Premature end
        of script headers
    [Mon Sep 14 11:11:32 2006] httpd: caught SIGTERM, shutting down
    [Fri Sep 25 16:13:11 1998] Server configured -- resuming normal
        operations
1   Bare word found where operator expected at welcome.pl line 21,
         near "/font></TABLE"
    (Missing operator before TABLE?)
2   syntax error at welcome.pl line 21, near "<TH><"
    syntax error at welcome.pl line 24, near "else"
    [Fri Sep 25 16:16:18 2006] access to /opt/apache_1.2b8/
        cgi-bin/visit_count.pl failed for susan, reason:
        Premature end of script headers

Previous Page Next Page