You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Sam Ingarfield <sj...@gmail.com> on 2009/10/02 08:58:32 UTC

[MP2][SESSION::POSTGRES] Problems retrieving session data.

Hello;

I'm having some curious problems with sessions in my scripts. I am
using the Apache::Session::Postgres module. Mod_Perl 2 on Apache 2, on
Debian (Lenny). Sessions are being tracked using cookies.

I am setting session data using code of the following form:

 73         my $r = Apache2::RequestUtil->request;
 74         my %session;
 76         tie %session, 'Apache::Session::Postgres', undef, {
 77                 DataSource => 'dbi:Pg:dbname=database;host=localhost',
 80                 UserName   => 'username',
 81                 Password   => 'password',
 82                 TableName => 'web.session',
 83                 Commit     => 1
 84         };
 87         my $session_cookie = "SID=$session{_session_id};";
 88         $r->headers_out->add('Set-Cookie' => $session_cookie);
 89         $session{some_key} = $some_variable;
 90
 95         untie(%session);
 96         undef %session;

Now, this seems to be inserting the correct value into the session,
and is saving it properly in the database (checked by decoding the
a_session data straight from the database). The problems seem to be
happening when I go to retrieve this data - I am intermittently
getting the incorrect data back.

For example, let user A have a session associated with him. He has the
string 'cow' in $session{animal}. User B, meanwhile, has 'goose' in
$session{animal}. Now, intermittently, user A is getting 'goose' when
retrieving $session{animal}.

The session variables are being retrieved as follows (in a different
script of course):
  35         my $r = Apache2::RequestUtil->request;
  36         my $cookie = $r->headers_in->{'Cookie'};
  40         local(@rawCookies) = split (/; /,$cookie);
  41         local(%cookies);
  43         foreach(@rawCookies)
  44                 {
  45                 ($key, $val) = split (/=/,$_);
  46                 $cookies{$key} = $val;
  47                 }
  49         my $id = $cookies{'SID'};
  50         my %session;
  54          tie %session, 'Apache::Session::Postgres', $id,
  55                         {
  56                         DataSource =>
'dbi:Pg:dbname=database;host=localhost',
  59                         UserName   => 'username',
  60                         Password   => 'password',
  61                         TableName => 'web.session',
  62                         Commit     => 1
  63                         };
  71         my $some_variable = $session{some_key};
  85         untie(%session);
  86         undef %session;

Given that these are essentially text-book examples, you can
understand why the situation is perplexing me...It is almost as if
some variable is persisting between executions of the script that
shouldn't be.

Any ideas?
Sam Ingarfield

Re: [MP2][SESSION::POSTGRES] Problems retrieving session data.

Posted by Perrin Harkins <ph...@gmail.com>.
On Fri, Oct 2, 2009 at 2:58 AM, Sam Ingarfield <sj...@gmail.com> wrote:
> For example, let user A have a session associated with him. He has the
> string 'cow' in $session{animal}. User B, meanwhile, has 'goose' in
> $session{animal}. Now, intermittently, user A is getting 'goose' when
> retrieving $session{animal}.

This usually means you have a scoping problem in your code, where
something that should be cleared is not.

>   35         my $r = Apache2::RequestUtil->request;
>   36         my $cookie = $r->headers_in->{'Cookie'};
>   40         local(@rawCookies) = split (/; /,$cookie);
>   41         local(%cookies);
>   43         foreach(@rawCookies)
>   44                 {
>   45                 ($key, $val) = split (/=/,$_);
>   46                 $cookies{$key} = $val;
>   47                 }
>   49         my $id = $cookies{'SID'};
>   50         my %session;
>   54          tie %session, 'Apache::Session::Postgres', $id,
>   55                         {
>   56                         DataSource =>
> 'dbi:Pg:dbname=database;host=localhost',
>   59                         UserName   => 'username',
>   60                         Password   => 'password',
>   61                         TableName => 'web.session',
>   62                         Commit     => 1
>   63                         };
>   71         my $some_variable = $session{some_key};
>  85         untie(%session);
>  86         undef %session;

This is pretty strange code: using local(), $key and $val are globals,
no safety check to see if you got something in $id or succeeded in the
tie() call.  I'd turn on strict and warnings and put in a bunch of
debug logging and see if you can sort it out.

However, the problem may be that you're putting the result of the
session read into some other variable that has scoping issues, and not
with this code at all.

- Perrin