Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Perl CGl Programming: No experience required.
(Publisher: Sybex, Inc.)
Author(s): Erik Strom
ISBN: 0782121578
Publication Date: 11/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


CGI and HTML

In the previous section, you learned how you can use Perl to create HTML code and display it on a Web page. Perl is a fully functional and useful programming language, so by definition it provides all of the usual control structures that allow you, the programmer, to make decisions about how data will be processed and displayed.

However, in terms of your Web site and what visitors see, Perl lurks far in the background. Visitors interact with your Web site on a higher level, which is embodied in the relationship between CGI and the HTTP/HTML combination that drives the World Wide Web (see Figure 2.9). Your understanding of Perl in the background will be enhanced if you learn some of the basics of this relationship.


Figure 2.9:  The relationship between Perl, CGI, and HTTP

HTTP is a protocol—the Hypertext Transfer Protocol—and as such it merely sets down a list of rules to follow when connecting to a Web server and interacting with it.

When you connect with a server through a Web browser, the browser takes care of many tedious details for you. However, you can strip away the browser’s protective interface and see precisely what happens in the connection with Telnet, a standard Internet utility that is included with UNIX, Windows NT, and Windows 95.

To see the dialog between a browser and an HTTP server, enter the following string at the command line:

   telnet <URL of your Web site> 80 <ENTER>

Of course telnet is the name of the program. Substitute the address or URL of your Web site for the bracketed part, such as www.daves_site.com. Telnet by default connects to the Internet port that has been set aside by convention for telnet connections, Port 23, but you can tell it to go anywhere. Port 80 is set aside for HTTP connections, so we’ll go there. Table 2.1 shows some other Internet ports.

TABLE 2.1: Some Well-Known Internet Ports

Service Port Purpose

ftp 21 FTP connection
telnet 23 Telnet connection
smtp 25 SMTP mail connection
finger 79 Finger server connection
http 80 Web server connection
pop3 110 POP3 post office connection

Depending on the server, you may or may not receive a sign-on message once Telnet connects. You probably won’t see what you type in next, either, so enter the following line carefully, substituting the path to your CGI script directory for /scripts/perl-cgi/:

   GET /scripts/perl-cgi/hellowww.pl HTTP/1.0

Press [return] twice. That tells the server your request is finished. Figure 2.10 shows what you’ll get back, or something similar to what you’ll get back.


Figure 2.10:  Output from a Telnet/HTTP connection

Now, this trick opens up all kinds of potential nightmare concerns with respect to Web server security. In fact, this is one of the preferred methods for ill-bred little trolls wanting to crack your system.

We’ll discuss system security later. For now, you can see exactly what goes on when a browser connects with your server.

Using the GET and POST Commands

Notice that your hand-typed command to the Web server began with a GET command. If you have done any HTML programming, you will immediately recognize this as one of the methods for passing FORM data to a server through HTML. GET tells the server that it wants the file at the specified URL to be returned to whoever asked for it—in this case, you. Because you connected to the server through Telnet, you saw HTML code generated by hellowww.pl displayed on-screen. A Web browser would have recognized the output as HTML and would have formatted it accordingly.

Another command you will use frequently in HTML forms is POST and it works the same way, except that it is commonly used to send large amounts of data to the server to be processed by a CGI program. The details of how GET and POST work will be saved for Skill 5.

MIME Defines the Rules

CGI provides the pipeline between the Web server, your visitors, and your Perl program. The MIME standard provides the rules of communication.

You’ve already learned that MIME stands for Multipurpose Internet Mail Extensions and that it was developed to allow varying file types to be sent via e-mail.

Like GET and POST, MIME is crucial to the HTTP/HTML combination. It defines the type of data sent to a Web server; without the MIME header, the request to the server would fail because the server wouldn’t know what to do with it.

In hellowww.pl, you developed a standard MIME header for sending HTML code to the Web server:

   print "Content-type: text/html", "\n\n";  # MIME header.

However, there are dozens of other standard types and extensions that can be used. Whether they are recognized by your browser or your Web server depends on whether you or the developers of the software have programmed them in.

For a graphic example, try running Netscape’s Navigator browser if you have access to it, click the Options menu, choose General Preferences and then click the Helpers tab. This brings up a very long list of standard and not-so-standard MIME content types, illustrated in Figure 2.11, along with the actions that Navigator will take when it encounters them.


Figure 2.11:  There are a ton of MIME-compatible data types.

You’ll become better acquainted with MIME in Skill 3.

Getting Acquainted with the Sambar Server

If you decided earlier in this skill to download and install the freeware Sambar Web server, this section is for you. If you didn’t install it, you may want to reconsider the Sambar server because in this exercise you’ll learn how to use it to test your Web pages and CGI programs without even being connected to the Internet.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.