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.
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.
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
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
|
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 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.
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.
(At the Command line)
1 perl -c perlscript
2 perlscript syntax OK |
(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 |