http://qs321.pair.com?node_id=149454

fuzzyping has asked for the wisdom of the Perl Monks concerning the following question:

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; }