Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Problems with setting session variables in Catalyst

by LunarCowgirl (Sexton)
on Aug 11, 2014 at 22:29 UTC ( [id://1097046]=perlquestion: print w/replies, xml ) Need Help??

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

I'm having trouble with session variables in Catalyst. I'm new to Catalyst, so it's likely I don't fully understand how to use Session::Store. I've found, on all the web, only two examples of how to add items to $c->session:

push @{ $c->session->{items} }, $item_id; $c->session->{cart}{$item_id} += $quantity;

I know $c->session is a hash ref, and I've tried variations on both above. What I'm trying to do is create a hidden token in a form for validation upon POST. This is my GET routine:

sub contact_GET { my ( $self, $c ) = @_; # Using Crypt::Random my $token = makerandom( Size => 128, Strength => 0 ); $c->session->{token} = $token; $c->load_status_msgs; $c->stash( template => 'contact.tt', title => 'Contact Us', token => $token, ); }

I've also tried:

push @{ $c->session->{token} }, $token;

The contact page won't load when I use either, and it crashes the server, so I get no error or debugging messages to indicate what's happening. I have to kill the server and restart it. If I comment out the line for $c->session, everything works fine. If I uncomment it though, things crash again.

The odd thing is that it suddenly starting working with the line uncommented. I had the token set at 512 bits from a copy and paste example. Changing the size to 128 made everything start working. Then, all of a sudden, without any changes to the method, it stopped and I have to comment out the line to get the page to load. Setting the token at 12 bits made no change, so I doubt it had anything to do with the variable size. I'm really confused here. What is the proper way to push a variable into a Catalyst session? Or if I'm doing it right, why is the page crashing the server?

Replies are listed 'Best First'.
Re: Problems with setting session variables in Catalyst
by thomas895 (Deacon) on Aug 11, 2014 at 22:52 UTC

    Perhaps Catalyst::Plugin::Session does not play well with binary data? Try storing the data as text using (un)pack or MIME::Base64?

    Also try a different storage backend. Maybe that doesn't like binary data either(if you find that to be the case, you might want to file a bug report).

    ~Thomas~ 
    "Excuse me for butting in, but I'm interrupt-driven..."
Re: Problems with setting session variables in Catalyst
by LunarCowgirl (Sexton) on Aug 12, 2014 at 02:41 UTC

    Actually, you both were on the right track. Using the example from Catalyst::Plugin::Session::Store worked. I did look at the docs while I was doing this -- several times -- but not this one. This is completely different than the example given in the Catalyst tutorial and in Catalyst::Plugin::Session, which were the two examples I cited.

    Also, the generated token wasn't playing well with Memcached. I discovered Session::Token though and that works perfectly in place of Crypt::Random. It's been a while since I've done any serious Perl coding, so I'm still getting back on my feet again and relearning the ecosystem.

    Thanks for both your help.

      Actually, you both were on the right track. ...Using the example from Catalyst::Plugin::Session::Store worked. I did look at the docs while I was doing this -- several times -- but not this one....

      Yup, you normally wouldn't use this API (its the low level), but if it doesn't work using this api, then something is really wrong :)

      OTOH, have you seen ?? Catalyst::Controller::RequestToken - Handling transaction tokens across forms update: nevermind, doesn't seem to use Session::Token probably not worth investigating further :)

        I did look at that, yes, but it seemed to conflict with using Catalyst::Controller::REST, so I went with my own solution. I'm much happier with Session::Token.
Re: Problems with setting session variables in Catalyst
by Anonymous Monk on Aug 11, 2014 at 23:36 UTC

    I'm new to Catalyst, so it's likely I don't fully understand how to use Session::Store. I've found, on all the web, only two examples of how to add items to $c->session:

    Try looking at the docs?

    Yeah Catalyst::Plugin::Session::Store - Base class for session storage drivers.

    my $data = $c->get_session_data( $key ); $c->store_session_data( $key, $data );
    Where $data is a hashref , arrayref or scalar

Log In?
Username:
Password:

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

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

    No recent polls found