I'm trying to learn the limitations of CGI's using cookies and hidden fields through the use of CGI::Session. My main goal is to create a CGI that will call itself through multiple invocations of a form, getting further along each time. I'm having incredible difficulty with the main project, so I've created this simple script to test what I do (and don't) understand about session state.
This script simply asks for the user's name and age. It should loop each time the user submits, incrementing $age by one each time. Could someone please explain to me why this doesn't work past the 2nd invocation?
-fuzzyping
#!/usr/bin/perl
use strict;
use CGI;
use CGI::Session::DB_File;
my $title = "Test Hidden Fields";
my $cgi = CGI->new;
my $sid = $cgi->cookie('session_id') || undef;
my $Session = new CGI::Session::DB_File($sid, {FileName=>'/var/www/cgi
+-bin/nobody/sessions.db', LockDirectory=>'/var/www/cgi-bin/nobody'});
if ($sid) { $Session->load_param($cgi) }
$sid ||=$Session->id;
my $cookie = $cgi->cookie(-name=>'session_id', -value=>$sid, -expires=
+>"+5m");
print $cgi->header(
-cookie=>$cookie,
-start_html=>$title);
print "<center>",
$cgi->h1($title),
"</center>";
if ($cgi->param('name')) {
$Session->param(-name=>'name', -value=>$cgi->param('name'));
$Session->param(-name=>'age', -value=>$cgi->param('age'));
$Session->save_param($cgi);
&add_age;
} elsif ($cgi->param('test')) {
&add_age;
} else {
print $cgi->start_form,
"Name: ",
$cgi->textfield('name'),
"Age: ",
$cgi->textfield('age'),
$cgi->p,
$cgi->submit('Next'), " ",
$cgi->defaults("Clear"),
$cgi->end_form;
}
print $cgi->end_html;
sub add_age {
my $age = $Session->param('age');
++$age;
print $Session->param('name'), "\'s new age is $age";
$Session->param(-name=>'age', -value=>$age);
$Session->save_param($cgi);
print $cgi->start_form,
$cgi->hidden(-name=>'test', -value=>'test'),
$cgi->submit('Next'),
$cgi->end_form;
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.