#!/usr/bin/perl -Tw use strict; $|++; use CGI qw(:all); ## set up the cache use File::Cache; my $cache = File::Cache->new({namespace => 'surveyonce', username => 'nobody', filemode => 0666, expires_in => 3600, # one hour }); unless ($cache->get(" _purge_ ")) { # cleanup? $cache->purge; $cache->set(" _purge_ ", 1); } my $SCRIPT_ID = join ".", (stat $0)[0,1,10]; print header, start_html("Survey"), h1("Survey"); if (param) { ## returning with form data ## verify first submit of this form data, ## and from the form generated by this particular script only my $session = param('session'); if (defined $session and do { my $id = $cache->get($session); $cache->remove($session); # let this be the only one $id and $id eq $SCRIPT_ID; }) { ## good session, process form data print h2("Thank you"); print "Your information has been processed."; my $name = param('name'); $name = "(Unspecified)" unless defined $name and length $name; my ($color) = grep $_ ne '-other-', param('color'); $color = "(Unspecified)" unless defined $color and length $color; print p, "Your name is ", b(escapeHTML($name)); print " and your favorite color is ", b(escapeHTML($color)), "."; } else { print h2("Error"), "Hmm, I can't process your input. Please "; print a({href => script_name()}, "start over"),"."; } } else { ## initial invocation -- print form ## get unique non-guessable stamp for this form require MD5; param('session', my $session = MD5->hexhash(MD5->hexhash(time.{}.rand().$$))); ## store session key in cache $cache->set($session, $SCRIPT_ID); ## print form print hr, start_form; print "What's your name? ",textfield('name'), br; print "What's your favorite color? "; print popup_menu(-name=>'color', -values=>[qw(-other- red orange yellow green blue purple)]); print " if -other-: ", textfield('color'), br; print hidden('session'); print submit, end_form, hr; } print end_html;