7.1 Introduction


Thus far we have dealt only with static web pages, which are simply HTML text files that don't change except when the file is edited by hand or generated by WML. In this chapter, we learn about dynamic web pages, which contain content that is created when requested . Now all the Perl that you learned in Chapter 4 and the MySQL that you learned in Chapter 5 will come in handy.

In this chapter, we discuss the basic way to create dynamic web pages: the Common Gateway Interface (CGI), a standard for communication between a client and the server. CGI scripts can be written in almost any language; we like Perl. Perl is well suited to the types of text processing common for many tasks , such as search engines and forms interfaces. Other benefits of Perl include portability, ease of programming, and overall computational power and performance. And to top it off, the Perl module CGI.pm is a useful way to make Perl CGI script writing quick and easy.

CGI scripts can do simple things that require no input from the client, such as displaying the current time or a random banner when a web page is accessed. Or they can do more complicated tasks involving posted form data from the client, such as entering a credit card number, searching a database and returning the information, and filling out a form. In Chapter 8, you'll find that mod_perl is equally suitable for this purpose, and in many cases, mod_perl is faster, so why learn about CGI scripts at all?

First, the basics of how data is passed from browser to server and from server to browser are the same using either CGI or mod_perl. CGI scripting is easy. There are a wealth of examples to draw from. Apache is probably already configured for CGI. [1] mod_perl often requires you to restart the server when a program changes, but CGI changes do not. The rest of the world uses CGI. And you already know Perl. So the main reason to use CGI is that it's common, and at some point in your webmaster career, you'll need to understand how CGI works because you or one of your clients will want to use an existing script. And most everything you learn here will be applicable for mod_perl in some way or another.

[1] It is if you installed Linux as we suggested in Chapter 2.

Many web sites have Perl CGI programs available for free download. There are too many to mention here, and we wouldn't anyway because it has been our experience that many of the CGI programs available for download ”both free and payware packages ”are poorly written at best and serious security risks at worst. Therefore, allow us to go on record as saying that although scripts available online can be useful for learning, we suggest that you avoid using them without careful examination. But you wouldn't use a program that you downloaded from Joe Schmoe's web site without looking it over, would you? Caveat emptor .

7.1.1 CGI Explained

Figure 7.1 depicts what happens during the request and execution of a CGI program. The webserver recognizes a CGI request by the location of the thing requested (or by the filename extension). For instance, if you load the URL www.example.com/cgi-bin/a.cgi into the browser, the webserver contacted, www.example.com , receives a request such as the following:

Figure 7.1. CGI explained

graphics/07fig01.gif

 GET /cgi-bin/a.cgi HTTP/1.0 

The server notices that the directory that contains the thing requested is cgi-bin . It is configured to take the object requested, here a.cgi , which is a program located on the server, and execute it as a stand-alone program. The program generates standard output (in Perl, we would use print() ). This output is in an important format: a header, a blank line, and the body.

The header is a very important piece of information that is sent back to the browser because it tells the browser how to render the data that follows . The primary piece of information that is sent in the header is the Content-type . If the header contains Content-type: text/plain , the browser displays the data that follows as plain text. If the header contains Content-type: text/html , the browser treats the data that follows as HTML and renders it appropriately. And this is what is really important: Programs must output the header, then a blank line, and then the content to be displayed. The blank line is essential ”it tells the browser that the header is complete and the body is about to begin.

It is easy to see the header, blank line, and body output from a CGI program by using a shell and telnetting to a server. The following code is an example of connecting to a CGI program named test.cgi , which simply prints the content type, the blank line, and some important text:

 $  telnet www.not_a_real_web_server.com 80  Trying 1.299.299.1  Connected to www.not_a_real_web_server.com (1.299.299.1)  Escape character is  ^  ].  GET /cgi-bin/test.cgi HTTP/1.0  HTTP/1.1 200 OK  Date: Thu, 17 Jan 2002 19:57:05 GMT  Server: Acme Web Server Version 0.001b  Connection: close  Content-Type: text/plain  There's more than one way to do it.  Connection closed by foreign host. 

When the server accepts the connection, it tells the client so. Then we see the HTTP request:

 GET /cgi-bin/test.cgi HTTP/1.0 

followed by a blank line. The webserver prints some header stuff, including the content type that the program prints, followed by a blank line that the program prints, followed by an important philosophical assertion in the next -to-last line. Then, had we used a browser intead of a telnet session, it would have taken the information in the header and the body and rendered it appropriately based on the content type in the header.

Again, that blank line that separates the header and the body is important . If it is not present, a server error will result.



Open Source Development with Lamp
Open Source Development with LAMP: Using Linux, Apache, MySQL, Perl, and PHP
ISBN: 020177061X
EAN: 2147483647
Year: 2002
Pages: 136

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net