190 likes | 199 Views
Web Site Design and Authoring. Session 6 Scott Marino. Introduction to Perl HTML 4 / Chapter 11 Processing CGI data HTML 4 / Chapter Get and Put Methods HTML 4 / Chapter. Incorporating CGI and Forms HTML 4 / Chapter Installing perl CGI’s Lab4 / Homework. Topics. Introduction to Perl.
E N D
Web Site Design and Authoring Session 6 Scott Marino Scott Marino MSMIS Summer Session 1 - 2001
Introduction to Perl HTML 4 / Chapter 11 Processing CGI data HTML 4 / Chapter Get and Put Methods HTML 4 / Chapter Incorporating CGI and Forms HTML 4 / Chapter Installing perl CGI’s Lab4 / Homework Topics Scott Marino MSMIS Summer Session 1 - 2001
Introduction to Perl • Practical Extraction and Report Language • A Unix originated language that combines elements of Unix scripting and C • Functions well as a cgi interface between the host server and the web browser • Data is passed to the cgi from a <form> when the user clicks the submit button Scott Marino MSMIS Summer Session 1 - 2001
Introduction to Perl • Remember to change the Unix permissions to 755 (executable) • Should be in the cgi-bin directory since Apache configuration may limit which directories can execute programs or scripts • #!/usr/local/bin/perl • required first line of the script • can be slightly different depending upon the server configuration Scott Marino MSMIS Summer Session 1 - 2001
Processing CGI Data • if ($ENV{'REQUEST_METHOD'} eq "GET") { $in = $ENV{'QUERY_STRING'}; } else { $in = <STDIN>; } • This line sets the variable $in equal to the input from the form • <form> can pass data as GET or PUT Scott Marino MSMIS Summer Session 1 - 2001
Get Method • <form method=get action=“cgi-bin/script.pl> <input TYPE="TEXT" SIZE="50" NAME=”f_name"> <input TYPE="TEXT" SIZE="50" NAME=”l_name"> </form> • http://www.mysite.com/cgi-bin/script.pl?f_name=joe&l_name=smith • Passes cgi information via the url • Faster because it doesn’t open a socket connection • Less secure Scott Marino MSMIS Summer Session 1 - 2001
Post Method • <form method=post action=“cgi-bin/script.pl> <input TYPE="TEXT" SIZE="50" NAME=”f_name"> <input TYPE="TEXT" SIZE="50" NAME=”l_name"> </form> • http://www.mysite.com/cgi-bin/script.pl • Passes cgi information via unix <stdin> • Slower because it must open a socket connection • More secure because the values can’t be modified Scott Marino MSMIS Summer Session 1 - 2001
Processing CGI Data • @pairs = split(/&/, $in);foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; if ($INPUT{$name}) { $INPUT{$name} = $INPUT{$name}.",".$value; } else { $INPUT{$name} = $value; } }} • Breaks the input apart into “name/value pairs” Scott Marino MSMIS Summer Session 1 - 2001
Processing CGI Data • Name/value pairs are the named form elements from the web page along with the associated value • <input TYPE="TEXT" SIZE="50" NAME="email"> • From the web form • $email = $INPUT{'email'}; • In the perl, assigns variable $email the value passed from the form Scott Marino MSMIS Summer Session 1 - 2001
Processing CGI Data • print "Content-type: text/html\n\n"; • Required first “print” statement alerting the web browser to expect a text stream and to process it as html data • exit; • Terminates the script Scott Marino MSMIS Summer Session 1 - 2001
Incorporating CGI and Forms • print "Content-type: text/html\n\n";print "<HTML><HEAD><TITLE>Welcome to my site</TITLE></HEAD><BODY BGCOLOR=\"#FFFFFF\">"; • Use the perl script to produce the form • Especially useful for multi-part forms • Store the results of form 1 in hidden elements Scott Marino MSMIS Summer Session 1 - 2001
Incorporating CGI and Forms • <!--#exec cgi="/cgi-bin/counter.cgi"--> • SSI parsed include for displaying output of a cgi • Apache parses the page (.shtml or .shtm) and follows the directive “exec cgi” • The exec calls the counter.cgi and replaces the entire <!--…--> with the results from the cgi • Typically used to embed cgi results in an existing page Scott Marino MSMIS Summer Session 1 - 2001
Installing perl CGI’s • Many shareware and freeware scripts are available • Some require installation and others are locally hosted • http://www.resourceindex.com/ • http://www.free-scripts.net/ • http://www.perlmasters.com/ Scott Marino MSMIS Summer Session 1 - 2001
Available type of scripts counters guest books mailing lists form processing surveys shopping carts auctions search functions advertising postcards random images utility functions games quizzes log analysis chat Installing perl CGI’s Scott Marino MSMIS Summer Session 1 - 2001
Installing perl CGI’s • The “read me” file contains most instructions • Retrieve the scripts to your pc • The web browser will typically handle the ftp • Uncompress or untar as required • tar is a unix process of putting multiple files into a single file • Winzip can sometimes untar a file. If not, it must be done via the unix command prompt • If a non-tar version is offered, go for it Scott Marino MSMIS Summer Session 1 - 2001
Installing perl CGI’s • Modify the #!/usr/bin/perl (first line of the script) as required • Look in the cgi-bin for a working script and use the same line from one that already works • Make any modifications to variables as directed in the read me • Ftp the file from the PC to the cgi-bin directory of the web server in ASCII mode • Set the permissions as “chmod 755 script.cgi” Scott Marino MSMIS Summer Session 1 - 2001
Installing perl CGI’s • Point the web browser to the proper form elements or script name and execute • Error 500 detected • This means there is an error with the script • Without access to the servers error logs, this can be very tough to diagnose • Check for incorrect paths • Check the Unix permissions • Look for a missing semicolon • Double check any changes you made Scott Marino MSMIS Summer Session 1 - 2001
Installing perl CGI’s • The error log traps every error • httpd: [Thu May 10 23:18:16 2001] [error] [client 216.35.103.54] File does not exist: /www/webundies/welcome_to_webundies.htm • httpd: [Fri May 11 06:21:13 2001] [error] [client 202.9.150.40] File does not exist: /www/webundies/www.americangreetings.com/greetingcard/cardgifs/thumbs/3003443t.gif • DBI::db=HASH(0x81701f8)->disconnect invalidates 1 active statement. Either destroy statement handles or call finish on them before disconnecting. at remind.pl line 64. • Database handle destroyed without explicit disconnect at remind.pl line 59. • httpd: [Sat May 12 02:24:58 2001] [error] [client 207.108.43.2] Premature end of script headers: /www/webundies/cgi-bin/favicon.ico • Great for debugging and checking the health of the server Scott Marino MSMIS Summer Session 1 - 2001
Lab 4 / Homework • Add a feedback, guest book or other form to your site • Can be a separate page • Should have several “text” form fields • Should capture a name and some comments • Add a page of useful links • Include links to resources you used to produce your site • Update with new links as you find new useful resources Scott Marino MSMIS Summer Session 1 - 2001