Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Is CGI::Session compatible with CGI::Lite::Request ?

by Anonymous Monk
on Apr 26, 2015 at 18:34 UTC ( [id://1124774]=perlquestion: print w/replies, xml ) Need Help??

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

Hello monks, today I replaed CGI.pm with CGI::Lite::Request for my website
Since then login functionality became not working.
Documentation of CGI::Session says it is compatible with any CGI.pm like modules which have param() method.
But CGI::Lite::Request do have param() method.
Especially the problem comes from here:

$session = CGI::Session->load(undef, $request, {Directory => "$LOCAL_PATH/admin/session"}, {name => 'sid'}); #returns undef

Other things such as setting cookie, creating session worked fine, but that method didn't work.
so.. is it possible to use CGI::Lite::Request with CGI::Session? if not, is there any alternative to CGI::Session for CGI::Lite::Request?

Replies are listed 'Best First'.
Re: Is CGI::Session compatible with CGI::Lite::Request ?
by afoken (Chancellor) on Apr 26, 2015 at 19:24 UTC
    didn't work

    Details? The relevant part of your code, messages found in the error log?

    Documentation of CGI::Session says it is compatible with any CGI.pm like modules which have param() method. But CGI::Lite::Request do have param() method.

    Did you notice that load() in CGI::Session calls the cookie() method of $request (called $query in the CGI::Session source)? According to the documentation, cookie('somename') in CGI returns the cookie value (a string, a hash, or an array), but cookie('somename') in CGI::Lite::Request returns an instance of CGI::Lite::Cookie (an object).

    The current version of CGI::Session on CPAN also documents that the cookie() method is called, but it does not document what it is expected to return. The code seems to expect it to behave like CGI's cookie() method, i.e. return the value of the cookie, not a cookie object.

    Your options:

    • Extract the session ID from $request in your code, then pass the session ID, not $request, as second parameter to load().
    • Patch CGI::Session to call the value() method on the return value of cookie()
    • Create a helper object that implements param() and cookie() as expected by CGI::Session based on data from $request. Pass that object instead of $request to load(). (See below)

    The helper class, quick and dirty and not tested:

    sub new { my ($class,$lite_request)=@_; return bless [ $lite_request ],$class; } sub param { my ($self,$name)=@_; return $self->[0]->param($name); } sub cookie { my ($self,$name)=@_; return $self->[0]->cookie($name)->value(); }

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Thanks a lot for the detailed reply
      I simply changed the parameters as you said and worked fine

      $session = CGI::Session->load(undef, $q->cookie('sid')->value(), {Dire +ctory => "$LOCAL_PATH/admin/session"});

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-04-25 20:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found