| |||
Perl, CGI, sessions - complete application template second steps
State and Cookies example from a Well House Consultants training course
More on State and Cookies [link] Source code: sweeter.pl Module: P407
#!/usr/bin/env perl
=head1 Perl web application framework - sessions and hidden fields =head2 Introduction to general code This is a demonstration program showing which provides the shell framework for a web based CGI application written in Perl. This example if the second of three - known as "sweeter". The other two examples, also downloadable from http://www.wellho.net, are known as sweet and sweetest. You can learn / study sweet and sweeter to help you understand the principles involved. We suggest you work with sweetest if you're going to write an application yourself that's based on the principles illustrated here. =item 1. read any incoming form =item 2. Use the value read to pick up the session =item 3. Do any work to analyse inputs and validate them =item 4. Do work to prepare for next page =item 5. Save the session =item 6. Send out the response page =cut $fill{"now"} = localtime(); $fill{"title"} = "Application Template"; $sessdir = "../../../sessions"; print "Content-type: text/html\n\n"; # item 1. read any incoming form #------------------------------- collect_form(); # item 2. Use the value read to pick up the session #-------------------------------------------------- if ($form{plsess}) { $sessname = $form{plsess}; open (FHS,"$sessdir/$sessname.ses"); while (<FHS>) { chop; ($name,$val) = split; $val =~ tr/+/ /; $val =~ s/%(..)/pack("C",hex($1))/eg; $session{$name} = $val; } } else { $sessname = $$.time(); # Take the opportunity of a new session to clean up stale ones opendir (DH,$sessdir); while ($fname = readdir DH) { next if ($fname =~ /^\./); if (-M "$sessdir/$fname" > 0.5) { unlink "$sessdir/$fname"; } } } $fill{plsess} = $sessname; $stepnumber = $session{step}; # Inputs in %session and %form translate to %session and %fill # item 3. Do any work to analyse inputs and validate them #-------------------------------------------------------- $aok = 1; $errstr = ""; if ($stepnumber == 0) { # Initialisation # Move on to next step - prepare for page 1 $stepnumber = 1; } elsif ($stepnumber == 1) { if ($form{yourname} !~ /\w/) { $aok = 0; $errstr .= "name must include a word character<br>"; } else { $session{name} = $form{yourname}; } if ($form{youremail} !~ /@/) { $aok = 0; $errstr .= "email must include an @ character<br>"; } else { $session{email} = $form{youremail}; } $stepnumber++ if ($aok); } elsif ($stepnumber == 2) { if ($form{yourtown} !~ /\w/) { $aok = 0; $errstr .= "Town must include a letter<br>"; } else { $session{town} = $form{yourtown}; } if ($form{yourpostcode} !~ /^[A-Z]{1,2}[1-9]/) { $aok = 0; $errstr .= "Postcode invalid<br>"; } else { $session{postcode} = $form{yourpostcode}; } $stepnumber++ if ($aok); } else { } # item 4. Do work to prepare for next page #----------------------------------------- $fill{status} = "You are at page $stepnumber of 3"; $fill{stuff} = "Results will appear here"; $fill{errstr} = $errstr; if ($stepnumber == 1) { $fill{"form"} = "name: <input name=yourname value=\"".webify($form{yourname})."\"><br>"; $fill{"form"} .= "email: <input name=youremail value=\"".webify($form{youremail})."\"><br>"; } elsif ($stepnumber == 2) { $fill{"form"} = "town: <input name=yourtown value=\"".webify($form{yourtown})."\"><br>"; $fill{"form"} .= "postcode: <input name=yourpostcode value=\"".webify($form{yourpostcode})."\"><br>"; } elsif ($stepnumber == 3) { $fill{stuff} = "<b>Hello ".webify($session{name})." from ".webify($session{town})."<br>"; $fill{stuff} .= "email ".webify($session{email})." pc ".webify($session{postcode})."</b><br>"; $stepnumber = 0; } else { } # item 5. Save the session #------------------------- $session{step} = $stepnumber; open (FHS,">$sessdir/$sessname.ses"); foreach $name (keys %session) { $val = $session{$name}; $val =~ s/%/%25/g; $val =~ s/\+/%2b/g; $val =~ s/\n/%0a/g; print FHS "$name $val\n"; } close FHS; # item 6. Send out the response page #----------------------------------- # Complete the output template and send it to the user $fill{step} = $stepnumber; while (<DATA>) { s/%(\w+)%/$fill{$1}/g; print; } ################## Standard functions =head2 Standard functions Note that the following standard functions would be placed into a separate file (a module) for a live application. For the purpose of this demo, we've put everyting into the one file =cut sub webify { $_ = $_[0]; s/&/&/g; s/</</g; s/>/>/g; s/"/"/g; return $_; } sub collect_form { if ($ENV{"REQUEST_METHOD"} eq "POST") { read(STDIN,$buffer,$ENV{"CONTENT_LENGTH"}); $form{"method"} = "POST"; } else { $buffer = $ENV{QUERY_STRING}; $form{"method"} = "GET"; } @fof = split(/&/,$buffer); foreach $field(@fof) { ($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; } } } ################################################################## # After the __END__ we'll place the template for all response pages __END__ <html><head> <title>%title% - Well House Consultants demo</title> </head> <body bgcolor=#CCFFFF> <h1>Demonstration - %title%</h1> This is the introductory piece of text about this application.<hr> This is the current status:<br><br>%status%<hr> <font color=red>%errstr%</font> <form method=POST>This is the next form <input name=plsess type=hidden value=%plsess%><br> %form% <input type=submit></form><hr> Here are your results:<br><br>%stuff%<hr> Copyright statement, etc .. %now% ...<br> debug info: page no. %step%, session %plsess% </body></html> 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 "State and Cookies" training module. You'll find a description of the topic and some
other closely related examples on the "State and Cookies" 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.php4 • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |