![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Perl CGl Programming: No experience required.
CGI and HTMLIn 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.
HTTP is a protocolthe Hypertext Transfer Protocoland 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 browsers 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 well go there. Table 2.1 shows some other Internet ports.
Depending on the server, you may or may not receive a sign-on message once Telnet connects. You probably wont 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 youll get back, or something similar to what youll get back.
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. Well 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 CommandsNotice 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 itin 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 RulesCGI provides the pipeline between the Web server, your visitors, and your Perl program. The MIME standard provides the rules of communication. Youve 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 wouldnt 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 Netscapes 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.
Youll become better acquainted with MIME in Skill 3. Getting Acquainted with the Sambar ServerIf you decided earlier in this skill to download and install the freeware Sambar Web server, this section is for you. If you didnt install it, you may want to reconsider the Sambar server because in this exercise youll learn how to use it to test your Web pages and CGI programs without even being connected to the Internet.
|
![]() |
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. |