Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
laziness, impatience, and hubris
 
PerlMonks  

Is it possible to have 2 CAP::Sessions?

by bradcathey (Prior)
on Jul 05, 2008 at 23:17 UTC ( #695764=perlquestion: print w/ replies, xml ) Need Help??
bradcathey has asked for the wisdom of the Perl Monks concerning the following question:

Fellow Monasterians

I've spent the last few hours exploring CGI::Application::Plugin::Session and have run into a question of how to access session params by the name of the session.

I was able to get it working with straight CGI::Session:

use CGI::Session; #initial trip to server CGI::Session->name("SESSION_ID1"); my $session = new CGI::Session(); $session->param('user' => 'Fred'); print $session->header(); #subsequent trip CGI::Session->name("SESSION_ID2"); my $session = new CGI::Session(); $session->param('user' => 'Barney'); print $session->header(); #later... CGI::Session->name('SESSION_ID1'); my $session = new CGI::Session(); my $user_A = $session->param('user'); CGI::Session->name('SESSION_ID2'); my $session = new CGI::Session(); my $user_B = $session->param('user'); print "Content-type: text/html\n\n"; print $user_A."<br />"; print $user_B";

But when I try:

#first trip $self->session_config( DEFAULT_EXPIRY => '+8h', COOKIE_PARAMS => { -name => 'SESSION_ID1', -expires => '+8h', -path => '/', }, SEND_COOKIE => 1, ); $self->session->param('user' => 'Fred'); #later trip $self->session_config( DEFAULT_EXPIRY => '+8h', COOKIE_PARAMS => { -name => 'SESSION_ID2', -expires => '+8h', -path => '/', }, SEND_COOKIE => 1, ); $self->session->param('user' => 'Barney');

I can't figure out if there is a short cut to return:

print $self->session->param('user');

based on the name of the session. Am I stuck with using

CGI::Session->name('SESSION_ID1'); my $session = new CGI::Session(); my $user_A = $session->param('user'); CGI::Session->name('SESSION_ID2'); my $session = new CGI::Session(); my $user_B = $session->param('user');

? Or is there some cool way with CAP::Session that I'm missing? Thanks!

—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Comment on Is it possible to have 2 CAP::Sessions?
Select or Download Code
Re: Is it possible to have 2 CAP::Sessions?
by friedo (Prior) on Jul 06, 2008 at 00:30 UTC

    Unfortunately, it looks like CAP::Session does not support having multiple session configs in a single instance. Peeking at the source (which is pretty straightforward) it stores the stuff you pass to session_config in $self->{__CAP__SESSION_CONFIG} and doesn't pay attention to the session names.

    It seems like this would be a pretty easy feature to add; maybe I'll take a whack at it in the upcoming weeks or so. For now, I think you're stuck writing your own session wrapper, or stuffing everything together in one session store.

    Update: Patch submitted. I must be really bored.

      Not what I wanted to hear, but at least it confirms my suspicions.

      Thanks.

      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: Is it possible to have 2 CAP::Sessions?
by bradcathey (Prior) on Jul 06, 2008 at 13:08 UTC

    Wow, thanks!

    So, are we looking at something like:

    $self->session_config( SESSION_NAME => 'SESSION_ID1', COOKIE_PARAMS => { -path => '/', }, SEND_COOKIE => 1, ); $self->session->param('user' => 'Barney'); $self->session_config( SESSION_NAME => 'SESSION_ID2', COOKIE_PARAMS => { -path => '/', }, SEND_COOKIE => 1, ); $self->session->param('user' => 'Fred');

    And then to retrieve it later:

    $self->session_config( SESSION_NAME => 'SESSION_ID1', ); my $user = $self->session->param('user'); $self->session_config( SESSION_NAME => 'SESSION_ID2', ); my $user = $self->session->param('user');

    Two questions:
    1. So CAP::Sessions won't complain about invoking session_config twice because I'm using the specific SESSION_NAME's?
    2. How do I implement/install the patch on my server? (sorry, I'm not sure how all this works yet)

    —Brad
    "The important work of moving the world forward does not wait to be done by perfect men." George Eliot

      1. So CAP::Sessions won't complain about invoking session_config twice because I'm using the specific SESSION_NAME's?

      That's the idea, yeah. Also make sure you pass a different -name to the COOKIE_PARAMS for each. If you do that, then both cookies will be set, as long as you do both calls to session_config.

      2. How do I implement/install the patch on my server? (sorry, I'm not sure how all this works yet)

      I don't recommend you use it for anything important since it's completely untested and I wrote it in a BBQ-induced stupor around 3AM. But if you really want to try it, download the CAP::Session tarball from CPAN, unpack it, and patch the files with the .diff attached to the RT ticket. Then use the Build.PL script as usual to build it. You might want to specify a prefix to install the module somewhere else so it doesn't overwrite your "real" install of CAP::Session.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2014-04-20 11:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls