Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Lossing session value

by Anonymous Monk
on Dec 13, 2017 at 15:46 UTC ( [id://1205433]=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks!

When I am using "Location:" to redirect the page to its original state and to prevent re-sending the upload again.
I am losing the value stored in the session, I need this value to display to the user the name of the file just uploaded.
I cant understand why this happens? Why isn't the value in the session available once the Location gets called? Any suggestion why or a better why to do this?
Here is a code prototype that shows what I am trying to do:

#!/usr/bin/perl use strict; use warnings; use CGI; use HTML::Template; use Time::Piece; use CGI::Session; use Data::Dumper; my $date_ntime = localtime->strftime('%Y%m%d%H%M%S'); my $cgi = CGI->new(); # Create new session my $session = new CGI::Session("driver:File", undef, {Directory => 'se +sh'}) or die CGI::Session->errstr;; my $tmpl = HTML::Template->new(filename => 'templates/mini.tmpl', die_ +on_bad_params => 0, associate => $session); my $got_file_name = $cgi->param( 'doc_upload' ) || ''; # Store file name to use later. $session->param("doc_uploaded", $got_file_name); my $filename_uploaded = $session->param("doc_uploaded"); # This is the return from the redirect 'Location:' my $conf_msg = $cgi->param( 'conf' ) || ''; process_request($got_file_name, $date_ntime) if $got_file_name ; # To prevent re-entering data twice by refreshing page. confirmation($conf_msg) if $conf_msg; print $cgi->header, $tmpl->output; exit; sub process_request { my ($file_name, $date_ntime ) = @_; return unless $file_name ; my $file_renamed .=$date_ntime.".txt"; my ($ext) = $file_name =~ /((\.[^.\s]+)+)$/; if ($ext =~ m/\.txt|\.csv$/) { $cgi->upload( 'doc_upload' ); my $tmp_file = $cgi->tmpFileName( $file_name ); rename( $tmp_file, $file_renamed ); chmod 0664, $file_renamed; print 'Location: https://myserver/mini.pl?conf=1', "\r\n\r\n"; }else { $tmpl->param( ERROR => 1, ); } } sub confirmation { my ($message) = @_; return unless $message; my $text = $session->param("doc_uploaded"); #my $text = $session->param(-name=>'doc_uploaded'); $tmpl->param( MSG_OK => 1, #FILE_NAME => $text, FILE_NAME => $filename_uploaded, ) if $message; }

Here is the template file:
<!DOCTYPE html> <html lang="en"> <head> <title>Mini</title> </head> <body> <p><b>UP-load.</b></p> <form action="mini.pl" method="post" enctype="multipart/form-data"> <input type="file" size="40" name="doc_upload"> <input type="submit" name="doc_submit" value="Upload"> </p> </form> <br> <br> <TMPL_IF NAME="MSG_OK"> <p>Transaction was successful!</p> <p>Filename: <TMPL_VAR NAME="FILE_NAME"></p> </TMPL_IF> <TMPL_IF NAME="MSG"> <p>WARNING: file <TMPL_VAR NAME="FILE_NAME"> already exists.</p> <p>Check and try again.</p> </TMPL_IF> <TMPL_IF NAME="ERROR"> <p>Wrong file type.</p> </TMPL_IF> </body> </html>

Thanks for looking!

Replies are listed 'Best First'.
Re: Losing session value
by hippo (Bishop) on Dec 13, 2017 at 16:36 UTC
    Why isn't the value in the session available once the Location gets called?

    Because you are over-writing it with an empty string here:

    my $got_file_name = $cgi->param( 'doc_upload' ) || ''; # Store file name to use later. $session->param("doc_uploaded", $got_file_name);
      No, that didn't work! Even removing the check for empty value my $got_file_name = $cgi->param( 'doc_upload' ); from the code I am still losing the session value.

        You are not checking for an empty value, you are setting one. And if you don't do that, you are setting it to undef which is just as bad. Here is what I think you should be doing:

        my $got_file_name = $cgi->param( 'doc_upload' ); # Store file name to use later but only if it is defined. $session->param("doc_uploaded", $got_file_name) if $got_file_name; my $filename_uploaded = $session->param("doc_uploaded");
Re: Lossing session value
by poj (Abbot) on Dec 13, 2017 at 19:01 UTC

    Add in $cgi or you get a new session each time. Get the session id

    # Create new session my $session = new CGI::Session("driver:File", $cgi, {Directory => 'ses +'}) or die CGI::Session->errstr;; my $sid = $session->id();

    Add the session parameter in here. Use .= to ensure a string and not a file handle.

    my $got_file_name .= $cgi->param( 'doc_upload' ) || $session->param( 'doc_uploaded' ) || '';

    Write the session to disk with flush

    # Store file name to use later. $session->param("doc_uploaded", $got_file_name); $session->flush;

    Store the session id in a cookie

    my $cookie = $cgi->cookie(CGISESSID => $sid); print $cgi->header(-cookie=>$cookie ), $tmpl->output;

    Add sessionid to redirect

    print "Location: https://myserver/mini.pl?conf=1;CGISESSID=$sid\r\n +\r\n";
    poj
      I added this new code you suggested, plus the others:

         print "Location: https://myserver/mini.pl?conf=1;CGISESSID=$sid\r\n\r\n";

      Now I get this in my browser:

      "This page isn’t working redirected you too many times."

      And nothing gets displayed!

        I'm not exactly sure what you are trying to achieve but revise the logic like this

        # To prevent re-entering data twice by refreshing page. if ($conf_msg){ confirmation($conf_msg) } else { process_request($got_file_name, $date_ntime); }
        poj
Re: Lossing session value (cgi101)
by Anonymous Monk on Dec 14, 2017 at 02:18 UTC

    Hi

    Did you skip all the documentation of CGI::Session?

    # Send proper HTTP header with cookies: print $session->header();

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1205433]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-26 06:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found