"; 10 print "\n"; 11 print scalar( localtime() ); 12 print ""; The statement use strict forces the programmer to declare all variables as package variables or lexically scoped variables. The use warnings statement warns the user of possible typos, the use of uninitialized variables and other potential problems in the code. HTTP header. Built-in function localtime, when evaluated in scalar context, returns a string which, in this case, is output directly to the screen by print. 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."> Current date and time"; 10 print "\n"; 11 print scalar( localtime() ); 12 print ""; The statement use strict forces the programmer to declare all variables as package variables or lexically scoped variables. The use warnings statement warns the user of possible typos, the use of uninitialized variables and other potential problems in the code. HTTP header. Built-in function localtime, when evaluated in scalar context, returns a string which, in this case, is output directly to the screen by print. 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.">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 - Introduction to Common Gateway Interface (CGI)

Similar presentations


Presentation on theme: "Chapter 7 - Introduction to Common Gateway Interface (CGI)"— Presentation transcript:

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

2 1 #!/usr/bin/perl 2 # Fig 7.2: fig07_02.pl 3 # Displays the current date and time in a Web browser. 4 5 use warnings; 6 use strict; 7 8 print "Content-type: text/html\n\n"; 9 print "<html><head><title>Current date and time</title>"; 10 print "</head>\n<body>"; 11 print scalar( localtime() ); 12 print "</body></html>"; The statement use strict forces the programmer to declare all variables as package variables or lexically scoped variables. The use warnings statement warns the user of possible typos, the use of uninitialized variables and other potential problems in the code. HTTP header. Built-in function localtime, when evaluated in scalar context, returns a string which, in this case, is output directly to the screen by print. 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.

3 Begins the HTML table in which the data will be displayed.
1 #!/usr/bin/perl 2 # Fig. 7.4: fig07_04.pl 3 # Program to display CGI environment variables. 4 5 use warnings; 6 use strict; 7 use CGI qw( :standard ); 8 9 print header(), start_html( "Environment Variables" ); 10 11 print '<table border = "0" cellspacing = "2">'; 12 13 foreach my $variable ( sort( keys %ENV ) ) { 14 print Tr( td( b( "$variable:" ) ), td( i( $ENV{ $variable } ) ) ); 16 } 17 18 print '</table>', 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 (<html>, <head>, <title>, etc., up to the opening <body> tag). When the function is called with a single argument, the argument is embedded between the <title> 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 foreach loop iterates through each of the keys of %ENV, sorting them lexically first. The %ENV hash is a built-in hashtable in Perl that contains the names and values of all the environment variables. The first argument calls function td to generate the opening and closing <td> (table data) tags around each cell of the table column. Each cell contains the output of the b function, which generates <b> (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:"). Function Tr returns a string containing the opening <tr> (table row) tag and the closing </tr> 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. In the second argument we output the value of the environment variable, $ENV{ $variable }, italicized (with <i> tags generated by function i within a table cell (using td). We close the table and call end_html to generate the final </body> and </html> tags.

4

5

6 The value of QUERY_STRING is placed into variable $query.
1 #!/usr/bin/perl 2 # Fig. 7.5: fig07_05.pl 3 # An example of using QUERY_STRING. 4 5 use warnings; 6 use strict; 7 use CGI qw( :standard ); 8 9 my $query = $ENV{ "QUERY_STRING" }; 10 11 print header(), start_html( "QUERY_STRING example" ); 12 print h2( "Name/Value Pairs" ); 13 14 if ( $query eq "" ) { 15 print 'Please add some name-value pairs to the URL above. '; print 'Or try <a href = "fig07_05.pl?name=Joe&age=29">this</a>.'; 17 } 18 else { 19 print i( "The query string is '$query'." ), br(); 20 21 = split( "&", $query ); 22 23 foreach my $pair ) { my ( $name, $value ) = split( "=", $pair ); print "You set '$name' to value '$value'.", br(); 26 } 27 } 28 29 print 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

8 Fig. 7.6 HTML form elements.

9 1 #!/usr/bin/perl 2 # Fig 7.7: fig07_07.pl 3 # Demonstrates GET method with HTML form. 4 5 use warnings; 6 use strict; 7 use CGI qw( :standard ); 8 9 our ( $name, $value ) = split( '=', $ENV{ QUERY_STRING } ); 10 11 print header(), start_html( 'Using GET with forms' ); 12 print p( 'Enter one of your favorite words here: ' ); 13 14 print '<form method = "GET" action = "fig07_07.pl">'; 15 print '<input type = "text" name = "word">'; 16 print '<input type = "submit" value = "Submit word">'; 17 print '</form>'; 18 19 if ( $name eq 'word' ) { 20 print p( 'Your word is: ', b( $value ) ); 21 } 22 23 print end_html(); 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. 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. The form is output with the <form> 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. Also during the second execution of the script, the conditional statement is true and the word outputs to the screen.

10

11

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

13

14 Function end_form prints the closing form tag, </form>.
1 #!/usr/bin/perl 2 # Fig 7.9: fig07_09.pl 3 # Demonstrates use of CGI.pm with HTML form. 4 5 use warnings; 6 use strict; 7 use CGI qw( :standard ); 8 9 my $word = param( "word" ); 10 11 print header(), start_html( 'Using CGI.pm with forms' ); 12 print p( 'Enter one of your favorite words here: ' ); 13 14 print start_form(), textfield( "word" ); 15 print submit( "Submit word" ), end_form(); 16 17 print p( 'Your word is: ', b( $word ) ) if $word; 18 print 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 <form> tag. When no arguments are given, start_form defaults to the POST method. Function textfield generates HTML to create an <input> 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, </form>. We use an if structure to execute the print statement only if $word was assigned a value from function param.

15

16 Field to collect client’s name.
1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 2 <!-- Fig. 7.10: fig07_10.html --> 3 4 <html><head><title>Enter here</title></head> 5 6 <body> 7 <h1>Welcome to Deitel Travel!</h1> 8 9 <form method = "POST" action = " 11 12 Please enter your name:<br/> 13 <input type = "text" name = "name"><br/> 14 15 Members, please enter the password:<br/> 16 <input type = "password" name = "password"><br/> 17 18 <font size = "-1"> 19 <i>Note that password is not encrypted.</i><br/><br/> 20 21 <input type = "submit"> 22 </form> 23 24 </body></html> Field to collect client’s name. Field to collect member’s password.

17

18 We assign the form-field values to variables $name and $password.
1 #!perl 2 # Fig. 7.11: fig07_11.pl 3 # Handles entry to Deitel Travel 4 5 use warnings; 6 use strict; 7 use CGI qw( :standard ); 8 9 print redirect( "/fig07_10.html" ) unless param( "name" ); 10 11 my $name = param( "name" ); 12 my $password = param( "password" ); 13 14 print header(), start_html( "Deitel Travel" ), h1( "Welcome, $name!" ); 16 17 print "Here are our weekly specials:", br(), ul( li( "Boston to Taiwan for \$300" ) ); 19 20 if ( $password eq "Coast2coast" ) { 21 print hr(), "Current specials just for members:", br(), ul( li( "San Diego to Hong Kong for \$250" ) ); 23 } 24 elsif ( $password ne "" ) { 25 print i( "Sorry, you entered the wrong password.", "If you have the correct password, enter", "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 29 else { 30 print i( "Become a member today for more great deals!" ); 31 } 32 33 print hr(), end_html(); If no password was entered, a message promoting the benefits of membership is output.

20


Download ppt "Chapter 7 - Introduction to Common Gateway Interface (CGI)"

Similar presentations


Ads by Google