Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

CGI::App session params losing values between redirects

by bradcathey (Prior)
on Feb 14, 2009 at 16:35 UTC ( [id://743836]=perlquestion: print w/replies, xml ) Need Help??

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

Fellow Monasterians,

I'm having a hard time getting my head around sessions.

Background: using CGI::Application, CGI::Application::Session, and CGI::Application::Redirect on a dedicated Linux server.

Scenerio:

  1. I set a session param in one of my modules and then...
  2. redirect to an instance script in the web root that...
  3. calls another module that uses the value in the session param

Problem: The session param in Members.pm is undef.

So, what am I not getting/understanding about sessions? Thank you!

File Layout

/opt/myapps/-----+ | | | Super.pm | | | /Acmecorp/---+ | | | Login.pm | | | Members.pm | | /var/www/acmecorp/--+ | home.html | /manage/----+ | login.cgi | members.cgi

Super.pm

package Super; use warnings; use strict; use base 'CGI::Application'; use CGI::Application::Plugin::Redirect; use CGI::Application::Plugin::Session; (my $http_host = $ENV{'HTTP_HOST'}) =~ s/(www.)([a-zA-Z0-9\-\.]+)/$2/ +; $self->session_config( COOKIE_PARAMS => { -name => 'MY_SESSID', -expires => '+8h', -path => '/', -domain => ".".$http_host, }, SEND_COOKIE => 1, );

Login.pm

package Login; use base qw(Super); ****login codes goes here and if successful: ***** $self->session->param( 'member_id' => $member_id ); return $self->redirect('/manage/members.cgi'); # I could pass it with return $self->redirect('/manage/members.cgi?id= +'.$member_id), but clunkier

members.cgi

#!/usr/local/bin/perl -T use lib "/opt/myapps/"; use Acmecorp::Members; my $app = Acmecorp::Members->new(); $app->run();

Members.pm

package Members; use base qw(Super); my $member_id = $self->session->param( 'member_id' ); **** query the DB with the member's id *****
—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Replies are listed 'Best First'.
Re: CGI::App session params losing values between redirects
by almut (Canon) on Feb 14, 2009 at 18:40 UTC

    Try flushing ($self->session->flush()) after you've set the parameter.

    Virtually any time I've ever had problems with CGI::Session (which, as I understand, CGI::Application::Plugin::Session is just a wrapper around), it was because I had forgotten to flush a parameter... so chances aren't too bad it's the same problem here :)

Re: CGI::App session params losing values between redirects
by samtregar (Abbot) on Feb 14, 2009 at 17:15 UTC
    Can you post actual working code? Obviously what you posted is just a sketch of your code and it's hard to know what's going on. If not, here's how I would debug:

    1. Is the cookie being sent back to the server by the client? You can check this with FireBug or by using a network analysis tool like WireShark.

    2. Is the session setup code in your super class running? Maybe you have that session call in a method that's being overridden in the sub-classes and you're not calling $self->SUPER::whatever().

    3. One way to test if it's #2 - duplicate the session config down into the two sub-classes. Does it start working?

    -sam

      Thank you samtregar

      1. Is the cookie being sent back to the server by the client?

      Yes it is.

      2. Is the session setup code in your super class running?

      Here's the entire Super class. I don't believe there could be anything in another module that would be overwriting it (there's nothing that resets the session cookie--it's all done in Super):

      package Super; use warnings; use strict; use base 'CGI::Application'; use CGI::Application::Plugin::Redirect; use CGI::Application::Plugin::Session; use HTML::Template; sub cgiapp_init { my $self = shift; (my $http_host = $ENV{'HTTP_HOST'}) =~ s/(www.)([a-zA-Z0-9\-\.]+)/ +$2/; #--- Sessions & Cookies $self->session_config( COOKIE_PARAMS => { -name => 'MY_SESSID', -expires => '+8h', -path => '/', -domain => ".".$http_hos +t, }, SEND_COOKIE => 1, ); } 1;

      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
        What's up with the -domain stuff? I think that could be your problem. Try removing it or replacing it with something static. Also, check that the clock on your webserver isn't more than 8h off (I've seen it happen!) since you're setting a pretty short expiration.

        Are you sure you don't have a cgiapp_init() defined in one of your sub-classes? Maybe add a warn() in the super class one and see that it actually prints when you run both sub-classes.

        -sam

Re: CGI::App session params losing values between redirects
by bradcathey (Prior) on Feb 28, 2009 at 19:49 UTC

    Update: solved

    After another very careful reading of the docs and looking at the source code of the plugin, I added the line CGI::Session->name('MY_SESSID'); and voila. Turns out if you change the name of the cookie in your configuration block, you have to let CGI::Session know about it or it creates another cookie/session, wiping out the old one. As soon as I added the line, all was good. I'm curious where it stores the name, because I'm not finding it in the actual /tmp/sessionfile.

    package Super; use warnings; use strict; use base 'CGI::Application'; use CGI::Application::Plugin::Redirect; use CGI::Application::Plugin::Session; (my $http_host = $ENV{'HTTP_HOST'}) =~ s/(www.)([a-zA-Z0-9\-\.]+)/$2/ +; $self->session_config( COOKIE_PARAMS => { -name => 'MY_SESSID', -expires => '+8h', -path => '/', -domain => ".".$http_host, }, SEND_COOKIE => 1, ); CGI::Session->name('MY_SESSID'); #Added this line

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-18 02:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found