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

I am having trouble with using form data and sessions in the same program. Below is sample code to show the problem I am experiencing.

use strict; use warnings; use CGI; use CGI::Carp qw/fatalsToBrowser warningsToBrowser/; use CGI::Session ( '-ip_match' ); my $cgi = CGI->new(); my @array = $cgi->param(); print "CGI Form Data-->", "@array ", $cgi->param("environment"), "<br> +"; my $session = CGI::Session->new(undef, undef, {Directory=>'e:\tmp'}); print <<HTML_BLOCK; <body> HTML_BLOCK print <<HTML_BLOCK; <form name='loginform' method='post' action='1.pl'> <input type='hidden' name='form' value='login'> <table> <tr> <td> UserName: </td> <td> <input type='text' name='username'> < +/td> </tr> <tr> <td> Password: </td> <td> <input type='password' name='password +'> </td> </tr> <tr> <td> Environment: </td> <td> <select size=1 name='environment'> <option value="env1">env1</option> <option value="env2">env2</option> </select> </td> </tr> <tr> <td colspan=2 style='text-align=center'> <input type='submit' name='submit' val +ue='Submit'> <input type='reset' name='reset' value +='Reset'> </td> </tr> </table> </form> </body> HTML_BLOCK

The code works fine if I comment out the my $session = CGI::Session->new(undef, undef, {Directory=>'e:\tmp'}); line. Each subsequent submit of the form returns CGI Form Data-->form username password environment submit env1

If I then uncomment the my $session = CGI::Session->new(undef, undef, {Directory=>'e:\tmp'}); line. The first submit of the form returns CGI Form Data-->form username password environment submit env1, however, every submit of the form after that returns CGI Form Data-->

If I then comment the my $session = CGI::Session->new(undef, undef, {Directory=>'e:\tmp'}); line back out, I continue to get CGI Form Data--> until I restart the website application pool.

It's almost like the session grabs the standard input and never lets it go.

Confussed, Thanks

Replies are listed 'Best First'.
Re: Form Data inaccessible after establishing session
by Anonymous Monk on Apr 22, 2011 at 01:03 UTC
    until I restart the website application pool.

    Until you restart the what now? Maybe you should use $cgi->Dump or $cgi->escapeHTML( Data::Dumper::Dumper( $cgi , $session ) ) to diagnose

      Application pools are a function of IIS. They allow websites to run under separate processes so that a problem in one web application does not affect another. When I say restart the application pool, it's like restarting the web server for only website on the web server.

      Results of diagnostic code (1st Form Submit):


      print $cgi->Dump; form login username password environment env1 submit Submit

      print $cgi->escapeHTML( Data::Dumper::Dumper( $cgi , $session ) ); $VAR1 = bless( { '.parameters' => [ 'form', 'username', 'password' +, 'environment', 'submit' ], 'use_tempfile' => 1, '.charset' => 'ISO- +8859-1', '.fieldnames' => {}, 'param' => { 'environment' => [ 'env1' +], 'password' => [ '' ], 'submit' => [ 'Submit' ], 'form' => [ 'login +' ], 'username' => [ '' ] }, 'escape' => 1 }, 'CGI' );

      Results of diagnostic code (2nd Form Submit):


      print $cgi->Dump;

      print $cgi->escapeHTML( Data::Dumper::Dumper( $cgi , $session ) ); $VAR1 = bless( { '.parameters' => [], 'use_tempfile' => 1, '.chars +et' => 'ISO-8859-1', '.fieldnames' => {}, 'param' => {}, 'escape' => +1 }, 'CGI' );

        Hmm, ok, not as revealing as I would have hoped because I made a mistake. Try this instead
        #!/usr/bin/perl -- use strict; use warnings; use CGI; use CGI::Carp qw/fatalsToBrowser warningsToBrowser/; use CGI::Session ( '-ip_match' ); use Data::Dumper(); Main( @ARGV ); exit( 0 ); sub Dumper { scalar Data::Dumper->new( \@_ )->Indent(1)->Useqq(1)->Dum +p; } sub Main { my $cgi = CGI->new(); my @array = $cgi->param(); print $cgi->header, $cgi->start_html; print '<table border=1><tr><td valign=top><pre>', $cgi->escapeHTML( Dumper( $cgi ) ), '</pre></td>'; my $session = CGI::Session->new(undef, undef, {Directory=>'e:\tmp' +}); print '<td><pre>', $cgi->escapeHTML( Dumper( $cgi, $session ) ), '</pre></td></tr></table>'; print <<HTML_BLOCK; <form name='loginform' method='post' action='1.pl'> <input type='hidden' name='form' value='login'> <table> <tr> <td> UserName: </td> <td> <input type='text' name='username'> < +/td> </tr> <tr> <td> Password: </td> <td> <input type='password' name='password +'> </td> </tr> <tr> <td> Environment: </td> <td> <select size=1 name='environment'> <option value="env1">env1</option> <option value="env2">env2</option> </select> </td> </tr> <tr> <td colspan=2 style='text-align=center'> <input type='submit' name='submit' val +ue='Submit'> <input type='reset' name='reset' value +='Reset'> </td> </tr> </table> </form> HTML_BLOCK print $cgi->end_html; }