|
To access the contents, click the chapter and section titles.
HTML 4.0 Sourcebook
Figure 11.7 Listing for the program rot-new.pl, which randomly selects an HTML document segment for insertion within an HTML document. Line numbers (in italics) are added for illustration only and are not present in the original program. The boldfaced comments are referred to by the text. 1 #!/usr/local/bin/perl 2 # rotator.pl 3 # Author: Ian Graham 4 # Information Commons, University of Toronto 5 # <[email protected]> 6 # Version: 0.1b. Date: July 13 1995 7 8 print Content-type: text/html\n\n; # print content-type header 9 10 $include_path=/svc/www/InsTest;# Directory containing include files 11 # Second Block: Get listing for 12 if( !opendir(DIR, $include_path)) {# the directory containing the inserts 13 &f_error(Unable to open notices directory\n, __LINE__, __FILE__); } 14 @tmp = readdir(DIR); # Read list of filenames; then check 15 foreach (@tmp) { # to see if they are files, and not Fig:16 # directories (-T tests for real files 17 push(@filenames, $include_path./.$_) if -T $include_path./.$_; } 18 close(DIR); 19 $last_index = $#filenames;# Get index of last entry 20 $last_index += 1; # in array of filenames 21 22 if($last_index < 0) { 23 print no files to insert ....\n; die;# no stuff, so don't do nuthin' 24 } 25 else { # If there are files to be 26 # inserted, select one at random 27 srand(time); # and print it to stdout. 28 $rand_index = int(rand($last_index)); 29 open(TEMP, $filenames[$rand_index]) || # Open selected file -- 30 &f_error(Unable to open insertion file.\n, __LINE__,__FILE__); 31 @insertion = <TEMP>; close(TEMP); 32 print @insertion; # Print contents to standard output 33 } 34 # --------------- FINISHED ----------- FINISHED -------------- 35 # Error Handling Subroutine 36 37 sub f_error { # What to do if there is an error 38 print Content-type text/html\n\n; 39 print Fatal error at line $_[1] in file $_[2].\n; 40 print Please send mail to: <BR>\n; 41 print <A HREF=\mailto:[email protected]\>[email protected]</A><BR>\n; 42 print to inform us of this error. If you can please, quote the URL\n; 43 print of the page that gave this error.\n<HR>\n; 44 die Fatal Error: $_[0] at line $_[1] in file $_[2] \n; 45 } Example 30: WebNoticeA Web-Based System for Distributing NoticesWebNotice is a Web-based package for posting and distributing notices on the World Wide Web. WebNotice uses the FORM interface to collect information from users, and uses gateway programs to process the submitted data, to archive that data in a server-side database, and to extract data from the database for return to the user. Any user can access the database to retrieve posted notices, but only authorized users can add new notices to the systemauthorization is accomplished using the Basic authentication scheme, discussed in Chapter 9. The stored notices are organized into groups, so that notices can be posted under different group categories (in this example, the groups are different university departments). Each group has its own distinct set of authorized users, and a user can only post under groups for which he or she is authorized. WebNotice consists of 28 gateway programs, 19 HTML document templates (read in by the gateway programs, and processed into complete HTML documents), 10 HTML documents, and a 30-page instruction manual, making it impossible to explain the whole package in this short section. The following will simply give an outline of the package, with an explanation of how the important parts work. The intent here is not to explain the detailed functioning of any particular component, but to give an idea of the overall design of a large gateway programming system. The WebNotice system is currently running at a number of sites. To see how it works, you can visit the original home of the package at: Figure 11.8 shows the organization of the different gateways, with the arrows showing the flow as the user traverses the system. Everything begins with the program notices_main.pl, which returns a simple HTML document describing the various options. The program generates this document by reading in a simple HTML template document, which it customizes according to the local configuration of the WebNotice system. An example of the resulting document is shown in Figure 11.9. Adding a Notice with WebNotice There are several possible options shown in Figure 11.9as an example, we follow the link to Add a notice. Selecting this link accesses the gateway program add_notices.pl . This program checks a server-side database to obtain a list of all the groups registered with the system and returns a page containing this list. This page is shown in Figure 11.10. The list of groups (here, a list of university departments) is presented as a list of selectable items (radio buttons) in a fill-in FORM. The user selects the group under which he or she wishes to submit a notice, and presses the Submit buttonin this example, the user has selected the Department of Statistics . Pressing the Submit button sends the FORM data to the server, and to the program deposit_form.pl (see Figure 11.8 to follow the flow). The Notice Fill-in FORMdeposit_form.pl Figure 11.11 shows the document returned by the program deposit_form.pl. Note how this gateway program was accessed using the GET methodyou can tell this by the encoded group information appended to the URL. This lets the user bookmark this page, so that he or she can return here without having to restart at notices_home.pl. This judicious choice of the GET method makes it possible for users to bookmark pages they are likely to access often.
The document returned by deposit_form.pl contains an extensive fill-in FORM, into which the user enters the information required for a notice announcement (an announcement of events such as concerts or seminars)there are fields for the time, date, and location of the event, the name of the presenter, and so on. Not obvious here is that the FORM also contains the hidden input element: <INPUT TYPE=hidden NAME=dept VALUE=stats>
This was inserted into the FORM by the program deposit_form.pl. Thus, when the data in this new FORM are sent the server, they will include the name of the group under which the notice is to be recorded.
|
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. Read EarthWeb's privacy statement. |