Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Using Sessions to save Parameters

by ikkon (Monk)
on Jul 31, 2007 at 18:32 UTC ( #629888=perlquestion: print w/replies, xml ) Need Help??
ikkon has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to save parameters being passed from Flash, Flash is sending parameters using GET and there are too many to send all of them so I must split them into 3 different GET. so I am using perl and CGI::Session to save the parameters, and then loading back up and saveing all of the parameters to a Hash. I can't seem to get it to properly work, I am not really familure with sessions, so I have been reading up on them, yet still not profiecent.

well I got the parameters being passed to save in the session but I am haveing difficulty loading the parameters up again and pushing the loaded parameters to the new parameters passed in a hash, not sure if I am totaly getting the concept, any help would be great
#!/usr/bin/perl -w BEGIN { use CGI::Carp qw(carpout); open(\*MYLOG, '>>', "sessions.log") or die("Unable to open perl.log: $!\n\n"); carpout(MYLOG); } use CGI; use CGI::Session; use strict; my $q = new CGI; print $q->header( "text/html" ); my %valueHash; GrabParams(); my $session = new CGI::Session(undef, undef, {Directory=>'tmp/sessions +'}) or die CGI::Session->errstr; my %newHash = $session->param('valueHash'); %newHash .= \%valueHash; foreach(%newHash){ print $_."<br>"; } $session->param('valueHash', \%valueHash); sub GrabParams{ my ( $paramName, $paramValue); foreach $paramName ($q->param) { foreach $paramValue ($q->param( $paramName )) { $valueHash{ $paramName } = pack 'U0A*', $paramValue; } } }

Replies are listed 'Best First'.
Re: Using Sessions to save Parameters
by Joost (Canon) on Jul 31, 2007 at 19:11 UTC
    One thing at least is wrong; you're generating a new session for each request (i.e. you're losing the old session, if there is one). You need to check if the session id is available, and get that session if so. Otherwise, create a new (empty) session.

    Also, when you've created a new session, you need to make sure the client re-uses the newly created session id in the following requests. The easiest way to do that, is probably to use cookies. See CGI::Session's header() method.

    The CGI::Session main manual page is a bit short on details, You'll probably want to take a look at CGI::Session::Tutorial

Re: Using Sessions to save Parameters
by Corion (Pope) on Jul 31, 2007 at 18:33 UTC

    If there are too many parameters for GET, maybe you should use POST instead?

      unfortunetly, Flash projector files disable POST and turn it into GET automatically as a security precaution.
Re: Using Sessions to save Parameters
by ikkon (Monk) on Aug 01, 2007 at 14:11 UTC
    I have updated my question, it seems that as of right this second I am haveing trouble pushing the loaded parameters to a exsisting hash
      I'm assuming you've got problems with this line:
      %newHash .= \%valueHash;
      I'm very surprised that didn't throw a syntax error.

      .= is the string concatenation operator. You can't just use it to combine hashes.

      You'll want something like:

      @newHash{keys %valueHash} = values %valueHash;
      Which inserts the key-value pairs in %valueHash into %newHash, overriding any pairs with the same names.

      Side note: if you're having trouble with some almost totally unrelated problem, it's probably better to post a new, trimmed-down version of the new problem instead of updating an old one.

Re: Using Sessions to save Parameters
by ikkon (Monk) on Aug 02, 2007 at 14:19 UTC
    oh I got it to work for the most part, there seemed to be alot of little things I was misses took some trial and error but I got it heres the code just incase someone might need it.
    use CGI; use CGI::Session; use strict; my $q = new CGI; print $q->header( "text/html" ); #--------------------------------------# #------------- Variables --------------# #--------------------------------------# my %valueHash; my %newHash; GrabParams(); my $session; my $SID; #--------------------------------------# #---- New/Open Session Condition ------# #--------------------------------------# if ($valueHash{"sessPass"} == 1) { $session = new CGI::Session(undef, undef, {Directory=>'tmp/ses +sions'}) or die CGI::Session->errstr; #--> creates a CGI::Session Obj +ect for use $SID = $session->id; }else{ $session = new CGI::Session(undef, $valueHash{"session"}, {Dir +ectory=>'tmp/sessions'}) or die CGI::Session->errstr; #--> Open sessi +on my %newHash = %{$session->param('valueHash')}; #--> load sessi +on hash and save it into a new hash (had to derefference it first wit +h %{}) #--> Loop through the load hash and push the values into value +Hash #--> so the new and old parameters are in one hash #------------------------------------------------------------- +------> while (my($k, $v) = each(%newHash)) { $valueHash{$k} = $v; printf("<br>key: %s value: %s", $k, $v); } #--> After loading session variables and setting them to the n +ew variables in valueHash #--> we need to clear the session hash values so we can rewrit +e the new values #------------------------------------------------------------- +--------------------------> $session->clear(); } $session->param('valueHash', \%valueHash); $session->flush(); #--------------------------------------# #------------ Functions ---------------# #--------------------------------------# sub GrabParams{ my ( $paramName, $paramValue); foreach $paramName ($q->param) { foreach $paramValue ($q->param( $paramName )) { $valueHash{ $paramName } = pack 'U0A*', $paramValue; } } }
    thanks for everyones help for pointing me in the right direction.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://629888]
Approved by Corion
[shmem]: karlgoethebier: here abouts people are sitting in big tents, gulp beer and ingest badly scorched chicken and sausages while (not) listening to afwul musik
[shmem]: *awful
[choroba]: panem et circenses
[karlgoethebier]: shmem: Yes. I didn't go to the firefighters party just 500 yards from my home this year...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2017-05-25 12:47 GMT
Find Nodes?
    Voting Booth?