| |||||||||||
| |||||||||||
Web helpers for quiz demo (and others too)
Good CGI scripting. example from a Well House Consultants training course
More on Good CGI scripting. [link] Source code: whc_web.pm Module: P774
# whc_web - standrd Perl / Web routines for Well House Consultants applications
# For use on our own web site (www.wellho.net) and on training courses # You are welcome to use these routines but they come "as is" and it's up to you # to check that they're suitable and secure for your needs. ################################################################################### use strict; sub webify { $_ = $_[0]; s/&/&/g; s/</</g; s/>/>/g; s/"/"/g; return $_; } ######################## # Code to translate form inputs (GET or POST) into a hash sub collect_form { my $buffer; our %form; if ($ENV{"REQUEST_METHOD"} eq "POST") { read(STDIN,$buffer,$ENV{"CONTENT_LENGTH"}); $form{"method"} = "POST"; } else { $buffer = $ENV{QUERY_STRING}; $form{"method"} = "GET"; } my @fof = split(/&/,$buffer); my $field; foreach $field(@fof) { my ($name,$value) = split(/=/,$field); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9]{2})/pack("C",hex($1))/eg; if ($form{$name}) { $form{$name} .= "\n$value"; } else { $form{$name} = $value; } } } ######################## # Read in a session (by given name or create a new session). sub read_session { my ($which) = @_; our %session; our $sessdir; our $sesslife; my $sessname; # Read an existing session if ($which) { $sessname = $which; open (FHS,"$sessdir/$sessname.ses") or webdie(701,"Session has expired"); while (<FHS>) { chop; my ($name,$val) = split; $val =~ tr/+/ /; $val =~ s/%(..)/pack("C",hex($1))/eg; $session{$name} = $val; } } else { # Create a new session. Do bookkeeping while we're at it. $sessname = $$.time(); if (! -e $sessdir) { mkdir $sessdir,0700 or webdie(500,"Cannot create session directory"); } # Take the opportunity of a new session to clean up stale ones opendir (DH,$sessdir); my $fname; while ($fname = readdir DH) { next if ($fname =~ /^\./); if (-M "$sessdir/$fname" > $sesslife/24) { unlink "$sessdir/$fname"; } } } return $sessname; } ######################## sub save_session { our %session; our $sessdir; my ($which) = @_; open (FHS,">$sessdir/$which.ses") or webdie (702,"Cannot write session file"); foreach my $name (keys %session) { my $val = $session{$name}; $val =~ s/%/%25/g; $val =~ s/\+/%2b/g; $val =~ s/\n/%0a/g; $val =~ s/ /+/g; print FHS "$name $val\n"; } close FHS; } ######################## sub archive_session { our %session; our $sessdir; our $archivedir; my ($which) = @_; open (FHS,">$archivedir/$which.ses") or webdie (707,"Cannot archive session"); foreach my $name (keys %session) { my $val = $session{$name}; $val =~ s/%/%25/g; $val =~ s/\+/%2b/g; $val =~ s/\n/%0a/g; $val =~ s/ /+/g; print FHS "$name $val\n"; } close FHS; if (! -e $archivedir) { mkdir $archivedir,0700 or webdie(509,"Cannot create session directory"); } unlink "$sessdir/$which.ses"; } ######################## sub send_response { my ($template) = @_; our %fill; our $headersent; send_header() if (! $headersent); open (FH,$template) or webdie (703,"Cannot read template file"); while (<FH>) { s/%(\w+)%/$fill{$1}/g; print; } } ######################## sub send_header { our $headersent = 1; print "Content-type: text/html\n\n"; } ######################## # Code to generate a table row that includes a prompt and an input box # Adds error messages and old ("sticky") values in sub formbox { our %form; our %errtab; my ($text,$fieldname) = @_; my $add = ""; if ($errtab{$fieldname}) { $add="<br><font color=red>$errtab{$fieldname}</font>"; } $text = "<tr><td>".webify($text)."$add</td><td><input name=$fieldname ". "value=\"".webify($form{$fieldname})."\"></td></tr>"; return $text; } sub radioset { our %form; our %errtab; my ($text,$fieldname,@options) = @_; my $add = ""; if ($errtab{$fieldname}) { $add="<br><font color=red>$errtab{$fieldname}</font>"; } # $text = "<tr><td>".webify($text)."$add</td><td>"; $text = "<tr><td>".$text."$add</td><td>"; my $k; for ($k=0; $k<@options; $k+=2) { my $extra = ""; if ($form{$fieldname} eq $options[$k+1][0]) { $extra = " CHECKED";} $text .= "<input type=radio name=$fieldname value=".$options[$k+1][0]." $extra>". " $options[$k]<br>"; } $text .= "</td></tr>"; return $text; } ######################## # Validate a form input against a regular expression sub checkinput { my ($fieldname,$sessname,$rematch,$errmsg) = @_; our %form; our %session; my $good = 1; our $errstr; our %errtab; if ($form{$fieldname} !~ /$rematch/) { $good = 0; $errtab{$fieldname} = $errmsg; $errstr = "Please correct the errors noted and resubmit"; } else { $session{$sessname} = $form{$fieldname}; } return $good; } sub checkradio { my ($fieldname,$sessname,$errmsg) = @_; our %form; our %session; my $good = 1; our $errstr; our %errtab; if (! $form{$fieldname}) { $good = 0; $errtab{$fieldname} = $errmsg; $errstr = "Please correct the errors noted and resubmit"; } else { $session{$sessname} = $form{$fieldname}; } return $good; } ################################################################## # Error handler sub webdie { my ($number,$message) = @_; our $title; our $email; our $headersent; our $homesite; send_header() if (! $headersent); print <<"OOPS"; <html><head><title>ERROR - $title</title></head> <body bgcolor=#FF9999><h2>$title</h2> <b>Error $number - $message</b><br><br> This is an error message from $title. If you need support and assistance, or if you think this message should not have been generated, please contact <a href=mailto:$email>$email</a>.<br><br> Select <a href="?">here</a> to restart the quiz or <a href=$homesite>here</a> to go to our home page. </body> </html> OOPS exit(); } 1; Learn about this subject
This module and example are covered on our public Using Perl on the Web course. If you have a group of
three or more trainees who need to learn the subject, we can also arrange a
private or on site course for you.
Books covering this topic
Yes. We have over 700 books in our library. Books
covering Perl are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Good CGI scripting." training module. You'll find a description of the topic and some
other closely related examples on the "Good CGI scripting." module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Web site author
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |