Thread: [Cgi-session-user] CGI::Sesion works incorrectly
Brought to you by:
sherzodr
From: Sergey B. <si...@gm...> - 2006-07-21 11:37:52
|
Hi guys. I have a very small trouble; I have 2 files index.pl and SessionSingleton.pm. If I use this construction my $session = CGI::Session->new( undef, undef, { Directory => "/tmp/sessions" } ); it works fine But if I use this CGI::Session->new( undef, undef, { Directory => "/tmp/sessions" } ); it doesn't work, sessions are not created. Why ? And what should I correct in SessionSingleton file to made it works properly ? file SessionSingleton.pm ---------------------------------------------------------------------------- package SessionSingleton; use CGI::Session; my $session; sub getInstance { defined $session or $session = CGI::Session->new( undef, undef, { Directory => "/tmp/session" } ); return $session; } 1; ~ ---------------------------------------------------------------------------- file index.pl ---------------------------------------------------------------------------- use SessionSingleton; use CGI; use CGI::Session; my $cgi = CGI->new; my $session = SessionSingleton->getInstance(); #my $session = CGI::Session->new( undef, undef, { Directory => "/tmp/sessions" } ); $cgi->cookie( CGISESSID => $session->id ); print $session->header; print $session->id; exit 0; |
From: Mark S. <ma...@su...> - 2006-07-21 12:24:17
|
Sergey Brutsky wrote: > Hi guys. > I have a very small trouble; > I have 2 files index.pl and SessionSingleton.pm. > > If I use this construction my $session = CGI::Session->new( undef, undef, { Directory => "/tmp/sessions" } ); > it works fine > > But if I use this CGI::Session->new( undef, undef, { Directory => "/tmp/sessions" } ); > it doesn't work, sessions are not created. > > Why ? And what should I correct in SessionSingleton file to made it works properly ? Sergey, The subject says that the module is not working correctly, but I'm not sure exactly which bug you reporting. Could just share a failing Test::More-style test case that clearly illustrates the issue? As for your singleton package, you may which to model it after CGI::Application::Plugin::Session, which does this successfully: http://search.cpan.org/src/CEESHEK/CGI-Application-Plugin-Session-1.02/lib/CGI/Application/Plugin/Session.pm Mark |
From: Mark S. <ma...@su...> - 2006-07-21 13:22:47
|
Sergey Brutsky wrote: > Hi, mark. I can't write Test::More test. Why not? The module is free, easy to use and well documented. Here's a first test to get you started: use Test::More 'no_plan'; my $session; { ok((not defined $session), "session starts out undefined"); } > The problem is: > If I use this construction --> my $session = CGI::Session->new( undef, > undef, { Directory => "/tmp/sessions" } ); > it works fine. > > But if I use this --> my $session = SessionSingleton->getInstance(); > it doesn't work. It doesn't create session and doesn't store it. > > Why ? I can't say for sure, but it's not clear that's a bug in CGI::Session, either. Why not study the working singleton example code in the module I mentioned below? Clearly it's possible to write one. Mark >> >> >> Sergey, >> >> The subject says that the module is not working correctly, but I'm not >> sure exactly which bug you reporting. Could just share a failing >> Test::More-style test case that clearly illustrates the issue? >> >> As for your singleton package, you may which to model it after >> CGI::Application::Plugin::Session, which does this successfully: >> >> http://search.cpan.org/src/CEESHEK/CGI-Application-Plugin-Session-1.02/lib/CGI/Application/Plugin/Session.pm |
From: Matt L. <mle...@cp...> - 2006-07-21 16:14:01
|
On 7/21/06, Sergey Brutsky <si...@gm...> wrote: > sub getInstance > { > defined $session or $session = CGI::Session->new( undef, undef, { Directory => "/tmp/session" } ); > return $session; *snip* > my $session = SessionSingleton->getInstance(); The definition of getInstance looks to be incorrect. It should look something more like: sub getInstance { defined $session ? $session : ($session = CGI::Session->new( undef, undef { Directory => "/tmp/session" })) } |
From: Sergey B. <si...@gm...> - 2006-07-23 11:15:53
|
I've changed my code in agree to your code, it doesn't work. I think that problem, probably, in initializing session instance. It initializes in two steps: creating session instance and then reinitializing session (reading CGISESSID and creating session file). It seems that my getInstance() method returns "half-initialized" session object. I mean to use condition "defined $session" is not enough. getInstance() should initialize sesssion, reinitialize and only after that returns instance. Is any ideas how to change condition (defined $session) to condition which will check if sesion object "full-initialized" ? > On 7/21/06, Sergey Brutsky <si...@gm...> wrote: > >> sub getInstance >> { >> defined $session or $session = CGI::Session->new( undef, undef, { Directory => "/tmp/session" } ); >> return $session; >> > > *snip* > > >> my $session = SessionSingleton->getInstance(); >> > > The definition of getInstance looks to be incorrect. It should look > something more like: > sub getInstance { > defined $session ? $session : ($session = CGI::Session->new( > undef, undef { Directory => "/tmp/session" })) > } > > ------------------------------------------------------------------------- > Take Surveys. Earn Cash. Influence the Future of IT > Join SourceForge.net's Techsay panel and you'll get the chance to share your > opinions on IT & business topics through brief surveys -- and earn cash > http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV > _______________________________________________ > Cgi-session-user mailing list > Cgi...@li... > https://lists.sourceforge.net/lists/listinfo/cgi-session-user > > |
From: Sergey B. <si...@gm...> - 2006-07-23 13:27:26
|
Hi, Mark. I've run your tests. The results is ok 1 - session starts out undef ok 2 - session is true after getInstance not ok 3 - multiple call to getInstance return the same session ID 1..3 As I see the problem not in CGI::Session and SessionSingleton. Problem is in initizliaing session. After first call session object has internal status = 1, after the second call internal status is changing to 2. Session object with status 2 is normal session object. My SessionSingleton always returns session object with status 1, it never richs status 2. To use CGI::Application::Plugin::Session I have to create separate package, but I wouldn't like it. I just want to use SessionSingleton in simple script (not in package). > Sergey, > > It's frustrating that you haven't followed up on the advice given to > you of writing some tests. Here are some tests I wrote for the case. > Do they pass for you when you run these tests in a file? > > If they do, your problem is elsewhere, or you need to write a new test > to share with us that illustrates your problem. If the tests don't pass, > check that you are using the latest version of CGI::Session. > > Finally, consider using CGI::Application and > CGI::Application::Plugin::Session instead! They are a lightweight, > proven solution to provide a singleton for a CGI::Session object. > > Mark > |
From: Mark S. <ma...@su...> - 2006-07-23 13:39:28
|
Sergey Brutsky wrote: > Hi, Mark. > I've run your tests. > The results is > ok 1 - session starts out undef > ok 2 - session is true after getInstance > not ok 3 - multiple call to getInstance return the same session ID 1..3 Thanks running the test. What CGI::Session version are you using? I used 4.13. Please upgrade to the latest version if you are using an older version. > My SessionSingleton always returns session object with status 1, it > never reaches status 2. > To use CGI::Application::Plugin::Session I have to create separate > package, but I wouldn't like it. > I just want to use SessionSingleton in simple script (not in package). Using a simple script with a custom singleton has taken days to debug. CGI::Application and CGI::Application::Plugin::Session can be used in a script, it just not recommended. You can declare the package at the top of the script: package My::CGIApp::Subclass; use base 'CGI::Application'; use CGI::Application::Plugin::Session; and at the bottom: My::CGIApp::Subclass->new->run(); ##### However, considering our different test results, but the problem now doesn't appear to be with the code, but something in the environment, like your CGI::Session version. Mark |
From: Mark S. <ma...@su...> - 2006-07-23 12:49:50
|
Sergey Brutsky wrote: > I've changed my code in agree to your code, it doesn't work. > > I think that problem, probably, in initializing session instance. > It initializes in two steps: creating session instance and then > reinitializing session (reading CGISESSID and creating session file). > It seems that my getInstance() method returns "half-initialized" session > object. > I mean to use condition "defined $session" is not enough. > getInstance() should initialize sesssion, reinitialize and only after > that returns instance. Sergey, It's frustrating that you haven't followed up on the advice given to you of writing some tests. Here are some tests I wrote for the case. Do they pass for you when you run these tests in a file? If they do, your problem is elsewhere, or you need to write a new test to share with us that illustrates your problem. If the tests don't pass, check that you are using the latest version of CGI::Session. Finally, consider using CGI::Application and CGI::Application::Plugin::Session instead! They are a lightweight, proven solution to provide a singleton for a CGI::Session object. Mark -- http://mark.stosberg.com/ #!/usr/bin/perl package SessionSingleton; my $session; sub getInstance { defined $session or $session = CGI::Session->new(); return $session; } use Test::More 'no_plan'; use CGI::Session; my $session1; is($session1,undef, "session starts out undef"); $session1 = SessionSingleton->getInstance; ok($session1, "session is true after getInstance"); my $sid = $session->id; $session2 = SessionSingleton->getInstance; is($sid,$session2->id, "multiple call to getInstance return the same session ID"); |