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

Hi Monks!! Is there any way using which the value of variables can be retained between the different HTTP server requests are done. Any help appreciated!! Thanks.

Replies are listed 'Best First'.
Re: retaining values among server requests
by merlyn (Sage) on Dec 04, 2007 at 16:15 UTC
    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.
      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";
Re: retaining values among server requests
by leocharre (Priest) on Dec 04, 2007 at 16:15 UTC
    CGI::Session

    It sounds like you are talking about data persistence.

    Consider also CGI::Session::Tutorial, and if you would use CGI::Application, CGI::Application::Plugin::Session is beautiful.

Re: retaining values among server requests
by sgifford (Prior) on Dec 04, 2007 at 17:31 UTC
      Thanks to all!! The things are working now, Whenever the web page is made and you need to pass some information from that page onto more pages, you can use CGI::session. For eg. Username has to be passed from login page to rest of the pages of the site.
      ##-- code for making a session and passing the information 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 ); #--do not print header first +if you redirecting your URL as given below $session->param('user_id',$username ); #--information passed my $url = "http://localhost/cgi-bin/somesite.cgi"; print "Location: $url "; #--redirecting the URL ##-- code for retrieving the information 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";