Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction.

Similar presentations


Presentation on theme: " 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction."— Presentation transcript:

1  2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction 7.3A Simple CGI Script 7.4Using CGI.pm to Generate HTML 7.5Sending Input to a CGI Script 7.6Using HTML Forms to Send Input 7.7Using CGI.pm to Create forms and Read Input 7.8Other Headers 7.9Example: An Interactive Portal 7.10Internet and World Wide Web Resources

2  2001 Prentice Hall, Inc. All rights reserved. Outline 1#!/usr/bin/perl 2# Fig 7.2: fig07_02.pl 3# Displays the current date and time in a Web browser. 4 5use warnings; 6use strict; 7 8print "Content-type: text/html\n\n"; 9print " Current date and time "; 10print " \n "; 11print scalar( localtime() ); 12print " "; The use warnings statement warns the user of possible typos, the use of uninitialized variables and other potential problems in the code. The statement use strict forces the programmer to declare all variables as package variables or lexically scoped variables. Until now, the output of print has always been displayed on the screen. However, technically speaking, the default target for print is standard output. When a Perl program is executed as a CGI script, the standard output is redirected to the client Web browser. Built-in function localtime, when evaluated in scalar context, returns a string which, in this case, is output directly to the screen by print. HTTP header.

3  2001 Prentice Hall, Inc. All rights reserved. Outline 1#!/usr/bin/perl 2# Fig. 7.4: fig07_04.pl 3# Program to display CGI environment variables. 4 5use warnings; 6use strict; 7use CGI qw( :standard ); 8 9print header(), start_html( "Environment Variables" ); 10 11print ' '; 12 13foreach my $variable ( sort( keys %ENV ) ) { 14 print Tr( td( b( "$variable:" ) ), 15 td( i( $ENV{ $variable } ) ) ); 16} 17 18print ' ', end_html(); The use statement directs the Perl interpreter to include a set of functions from the CGI.pm module. This standard function set is designated by the import tag :standard. Directs the Perl program to print the HTTP header using function header from the CGI library. Function start_html returns a string containing the standard opening tags of an HTML document (,,, etc., up to the opening tag). When the function is called with a single argument, the argument is embedded between the tags of the returned string to give the Web page a name, which is displayed in the browser’s title bar. Begins the HTML table in which the data will be displayed. The %ENV hash is a built-in hashtable in Perl that contains the names and values of all the environment variables. The foreach loop iterates through each of the keys of %ENV, sort ing them lexically first. Function Tr returns a string containing the opening (table row) tag and the closing tag to create a table row. Function Tr takes two arguments, each of which represent one cell in the table row; thus, the table is output with two columns. The first argument calls function td to generate the opening and closing (table data) tags around each cell of the table column. Each cell contains the output of the b function, which generates (bold) tags around text to be output in bold type. The actual text to be output in the cell is simply the name of the environment variable, followed by a colon ( "$variable:" ). In the second argument we output the value of the environment variable, $ENV { $variable }, italicized (with tags generated by function i within a table cell (using td ). We close the table and call end_html to generate the final and tags.

4  2001 Prentice Hall, Inc. All rights reserved. Outline

5  2001 Prentice Hall, Inc. All rights reserved. Outline

6  2001 Prentice Hall, Inc. All rights reserved. Outline 1#!/usr/bin/perl 2# Fig. 7.5: fig07_05.pl 3# An example of using QUERY_STRING. 4 5use warnings; 6use strict; 7use CGI qw( :standard ); 8 9my $query = $ENV{ "QUERY_STRING" }; 10 11print header(), start_html( "QUERY_STRING example" ); 12print h2( "Name/Value Pairs" ); 13 14if ( $query eq "" ) { 15 print 'Please add some name-value pairs to the URL above. '; 16 print 'Or try this.'; 17} 18else { 19 print i( "The query string is '$query'." ), br(); 20 21 my @pairs = split( "&", $query ); 22 23 foreach my $pair ( @pairs ) { 24 my ( $name, $value ) = split( "=", $pair ); 25 print "You set '$name' to value '$value'.", br(); 26 } 27} 28 29print end_html(); The QUERY_STRING variable contains extra information that is appended to a URL in a GET request, following a question mark ( ? ). The value of QUERY_STRING is placed into variable $query. We test to see if $query is empty. If so, we print a message instructing the user to add a query string to the URL. To break apart each of these resulting name– value pairs, we use a foreach loop that iterates through each $pair in turn. We call split to break the pair at the equals sign into a $name and a $value.

7  2001 Prentice Hall, Inc. All rights reserved. Outline

8  2001 Prentice Hall, Inc. All rights reserved. Fig. 7.6 HTML form elements.

9  2001 Prentice Hall, Inc. All rights reserved. Outline 1#!/usr/bin/perl 2# Fig 7.7: fig07_07.pl 3# Demonstrates GET method with HTML form. 4 5use warnings; 6use strict; 7use CGI qw( :standard ); 8 9our ( $name, $value ) = split( '=', $ENV{ QUERY_STRING } ); 10 11print header(), start_html( 'Using GET with forms' ); 12print p( 'Enter one of your favorite words here: ' ); 13 14print ' '; 15print ' '; 16print ' '; 17print ' '; 18 19if ( $name eq 'word' ) { 20 print p( 'Your word is: ', b( $value ) ); 21} 22 23print end_html(); The form is output with the tags. Notice that the method attribute is GET and the action attribute is the name of a file, in this case the file is fig07_07.pl. The form contains two input fields. The first is a single-line text field ( type = "text" ) with the name word. The second displays a button, labeled Submit word, to submit the form data. During the second execution of the script, when the query string is decoded, value word is assigned to variable $name and the user’s favorite word is assigned to variable $value. Also during the second execution of the script, the conditional statement is true and the word outputs to the screen.

10  2001 Prentice Hall, Inc. All rights reserved. Outline

11  2001 Prentice Hall, Inc. All rights reserved. Outline

12  2001 Prentice Hall, Inc. All rights reserved. Outline 1#!/usr/bin/perl2# Fig 7.8: fig07_08.pl 3# Demonstrates POST method with HTML form. 4 5use warnings; 6use strict; 7use CGI qw( :standard ); 8 9our ( $data, $name, $value ); 10 11read( STDIN, $data, $ENV{ 'CONTENT_LENGTH' } ); 12( $name, $value ) = split( '=', $data ); 13 14print header(), start_html( 'Using POST with forms' ); 15print p( 'Enter one of your favorite words here: ' ); 16 17print ' '; 18print ' '; 19print ' '; 20print ' '; 21 22if ( $name eq 'word' ) { 23 print p( 'Your word is: ', b( $value ) ); 24} 25 26print end_html(); The POST method sets the environment variable CONTENT_LENGTH, to indicate the number of characters of data that were sent (or posted). Function read is used to read in exactly that many characters from standard input ( STDIN ) and store the characters in the $data variable.

13  2001 Prentice Hall, Inc. All rights reserved. Outline

14  2001 Prentice Hall, Inc. All rights reserved. Outline 1#!/usr/bin/perl 2# Fig 7.9: fig07_09.pl 3# Demonstrates use of CGI.pm with HTML form. 4 5use warnings; 6use strict; 7use CGI qw( :standard ); 8 9my $word = param( "word" ); 10 11print header(), start_html( 'Using CGI.pm with forms' ); 12print p( 'Enter one of your favorite words here: ' ); 13 14print start_form(), textfield( "word" ); 15print submit( "Submit word" ), end_form(); 16 17print p( 'Your word is: ', b( $word ) ) if $word; 18print end_html(); Function param takes one argument: the name of an HTML form field. If form data containing a name–value pair with the given name (in this case, word ) were submitted, then the name’s associated value would be returned. Otherwise, param returns undef. Function start_form generates the opening tag. When no arguments are given, start_form defaults to the POST method. Function textfield generates HTML to create an single-line form element with the name word. Function submit creates a submit button with the given argument ( Submit word ) as a label. Function end_form prints the closing form tag,. We use an if structure to execute the print statement only if $word was assigned a value from function param.

15  2001 Prentice Hall, Inc. All rights reserved. Outline

16  2001 Prentice Hall, Inc. All rights reserved. Outline 1 2 3 4 Enter here 5 6 7 Welcome to Deitel Travel! 8 9<form method = "POST" 10 action = "http://localhost/cgi-bin/fig07_11.pl"> 11 12Please enter your name: 13 14 15Members, please enter the password: 16 17 18 19 Note that password is not encrypted. 20 21 22 23 24 Field to collect client’s name. Field to collect member’s password.

17  2001 Prentice Hall, Inc. All rights reserved. Outline

18  2001 Prentice Hall, Inc. All rights reserved. Outline 1#!perl 2# Fig. 7.11: fig07_11.pl 3# Handles entry to Deitel Travel 4 5use warnings; 6use strict; 7use CGI qw( :standard ); 8 9print redirect( "/fig07_10.html" ) unless param( "name" ); 10 11my $name = param( "name" ); 12my $password = param( "password" ); 13 14print header(), start_html( "Deitel Travel" ), 15 h1( "Welcome, $name!" ); 16 17print "Here are our weekly specials:", br(), 18 ul( li( "Boston to Taiwan for \$300" ) ); 19 20if ( $password eq "Coast2coast" ) { 21 print hr(), "Current specials just for members:", br(), 22 ul( li( "San Diego to Hong Kong for \$250" ) ); 23} 24elsif ( $password ne "" ) { 25 print i( "Sorry, you entered the wrong password.", 26 "If you have the correct password, enter", 27 "it to see more specials." ); 28} Checks if any data were posted for the name field using function param. Unless data have been posted, the client is redirected to the portal page We assign the form-field values to variables $name and $password. We use $name to print a personal greeting for the client. The current weekly specials are displayed. If the member password is correct, additional specials are output. If the password is not correct and the client submitted anything other than the empty string, the client is informed that the password was invalid.

19  2001 Prentice Hall, Inc. All rights reserved. Outline 29else { 30 print i( "Become a member today for more great deals!" ); 31} 32 33print hr(), end_html(); If no password was entered, a message promoting the benefits of membership is output.

20  2001 Prentice Hall, Inc. All rights reserved. Outline


Download ppt " 2001 Prentice Hall, Inc. All rights reserved. Chapter 7 - Introduction to Common Gateway Interface (CGI) Outline 7.1Introduction 7.2A Simple HTTP Transaction."

Similar presentations


Ads by Google