in reply to retaining values among server requests

The keyword you want is "session". There's a lot of literature on managing sessions with Perl, and quite a few modules in the CPAN to deal with it. The basics are that you need to either come back to the same process that served the previous hit (rare), send all the intermediate values down to the client as hidden fields (bandwidth intensive and security issues abound) or save the information into a server side persistent data store and use some session ID sent to the client as a cookie or hidden field or mangled URL to identify the session.
  • Comment on Re: retaining values among server requests

Replies are listed 'Best First'.
Re^2: retaining values among server requests
by need_help21 (Novice) on Dec 04, 2007 at 17:39 UTC
    I have already used CGI::session in my application but as soon as another server request is made for the other page, the session value get re-initialized.
    #! /usr/bin/perl -w use strict; use CGI; use CGI::Carp 'fatalsToBrowser'; use File::Listing qw(parse_dir); use CGI::Session; my $htm = new CGI; my %vars = $htm->Vars(); print $htm->header({ -type => "text/html"}); print $htm->start_html( -title => "Welcome", -style=>{'src'=>'../stdbstyle.css'}); if(defined($vars{'file_creation'}) and $vars{'file_creation'} eq 'FILE + CREATION') { fnShowFileCreationForm(); }elsif(defined($vars{'submit_login'}) and $vars{'submit_login'} eq 'Se +nd') { fnSubmitLogin(); }else { fnLoginForm(); } #-------Login Page-------------------------------- sub fnLoginForm { #FORM TO SHOW LOGIN PAGE----- print $htm->start_form({-method=>'post'}), $htm->start_table({-width=>'50%',-height=>'50%',-length=>' +50%',-align=>'center'}); print $htm->Tr({-bgcolor=>'#888888'}, $htm->th({-colspan=>2,-align=>'center'},"LOGIN")); print $htm->Tr({-bgcolor=>'#A8A8A8',-align=>'center'}, $htm->th("Enter username:", $htm->textarea({-name=>'uname',-cols=>'20',-rows=>'1',-default +=>'',-override=>'1'}))); print $htm->Tr({-bgcolor=>'#A8A8A8',-align=>'center'}, $htm->th("Enter Password:", $htm->textarea({-name=>'passwd',-cols=>'20',-rows=>'1',-defaul +t=>'',-override=>'1'}))); print $htm->Tr({-bgcolor=>'#505050'}, $htm->th( $htm->submit({-name=>'submit_login',-label=>'Send'}))); } #-----------------START FUNCTION 'FNSubmitLogin'----------- sub fnSubmitLogin { my $cgi = new CGI; my $session = new CGI::Session(undef,undef,{Directory=>'/tmp/sessions' +}); my $cookie = $cgi->cookie(CGISESSID => $session->id); print $cgi->header( -cookie=>$cookie ); $session->param('user_id',$username ); fnShowMainMenu(); } #----------------------Login Page ends here---------- #-----------------------Main Menu Form--------------------- sub fnShowMainMenu { print $htm->start_form({-method=>'post'}), $htm->start_table({-width=>'100%',-length=>'100%',-align= +>'center'}); print $htm->Tr({-bgcolor=>'#20B2AA', -fontsize=>+50}, print $htm->Tr({-bgcolor=>'#20B2AA'}, $htm->th({-colspan=>3,-align=>'center'},"Select the Task b +y pressing button given below::")); my $username = $vars{'uname'}; print "UserName = $username"; $htm->end_form(); } #--------Main Menu Form ends here--------------------- #-----------File Creation Form starts here-------# sub fnShowFileCreationForm { print "Content-type: text/html\n\n"; my $cgi = new CGI; my $sid=$cgi->cookie("CGISESSID"); my $session = new CGI::Session(undef,$sid,{Directory=>'/tmp/sessions'} +); my $username = $session->param('user_id'); print "Username = $username";