#!/usr/bin/perl -w # $Id: subscriptions.cgi 121 2005-02-09 08:31:01Z sherzodr $ use strict; use CGI; use CGI::Carp 'fatalsToBrowser'; use URI::Escape; use vars qw($SELF_URL); use lib '/home/sherzodr/perllib'; # Check for some non-standard Perl modules my @required = qw(MIME::Lite HTML::Template CGI::Session); for my $mod ( @required ) { eval "require $mod"; if ( $@ ) { print "Content-Type: text/html\n\n"; print "$mod is required. If it's installed in a non-standard path, " . "please 'use lib' line in $0"; exit(0); } } my $cgi = new CGI(); my $session = CGI::Session->load() or die CGI::Session->errstr; if ( $session->is_expired ) { print $session->header(); print "Your session expired, inevitably!"; exit(0); } elsif ( $session->is_empty ) { $session = $session->new(); } $session->expire("+30s"); my $cmd = $cgi->param('cmd') || $session->param("last_cmd") || 'directions'; $SELF_URL = $cgi->url() || $0; # save the last executed command: $session->param(last_cmd => $cmd); if ( $cmd eq "directions" ) { print directions($cgi, $session); } elsif ( $cmd eq 'step1' ) { print step1($cgi, $session); } elsif ( $cmd eq 'step2' ) { print step2($cgi, $session); } elsif ( $cmd eq 'step3' ) { print step3($cgi, $session); } elsif ( $cmd eq 'finish') { print finish($cgi, $session); } elsif ( $cmd eq 'clear' ) { print clear($cgi, $session); } elsif ( $cmd eq "show-dump" ) { print show_dump($cgi, $session); } else { print "Error: CMD: $cmd is not valid"; } #-------------------------------------------------------------------- # functions start here #-------------------------------------------------------------------- sub directions { my ($cgi, $session) = @_; my $dirver = ref($session); my $version = $session->VERSION(); my $HTML = <Welcome to CGI::Session Demo Script
The tricks are endless! This script is to demonstrate basic usage of CGI::Session in CGI applications.
Test consists of a single multi-page mailing list subscription form. First form asks to fill in personal information, the second screen asks to choose subscriptions you are interested in. The 3rd page is a confirmation/summary of your subscriptions. Once you click on "Finish" button, the program sends your subscription details to your email you provided during subscriptions, and also attaches the source code of this script.
During the process, application provides "Back", "Next" and"Clear Form" buttons so that you can go back to previous forms to fill in/correct the details. While going back, notice how the script remembers all the data you have previously submitted, and presents pre-filled/pre-selected form elements.
While somewhere in the middle of subscription, close the browser intentionally, and reopen the page: $SELF_URL. Notice how the script remembers which form you were filling out before you closed the browser, and displays respective form, instead of taking you to the default page. Your previously filled in form data are also kept.
In each page, the script also provides you with "show-dump" link at the bottom of each screen. You can click on the link to view internal _DATA table of CGI::Session, and see what kind of information are stored in the object at each step.
Should you have any suggestions or comments, feel free to send me an email: sherzodr\@cpan.org.
HTML return template(\$HTML, $cgi, $session); } sub step1 { my ($cgi, $session) = @_; my $HTML = <<'HTML';Hi %name%! Please fill out your personal information below
HTML return template(\$HTML, $cgi, $session); } sub step2 { my ($cgi, $session) = @_; $session->save_param($cgi); $session->load_param($cgi, ["subscriptions"]); my $HTML = <Step 2 out of 3Dear %name%.
Following are available newspaper subscriptions we offer.
Choose the subscriptions you are interested in, and click "Next >>"
button. To select more than one subscription, press and hold [CTRL] key while selecting.
Should you wish to update your profile, click "<<Back" button.
Dear %name%.
Before you finalize your subscription, you need to review the following
information you have submitted. Update if necessary.
When you are down, click on "Finish" button. Voila!