Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Multiple cookies in Catalyst, one longlasting and one session cookie

by martell (Hermit)
on Jan 29, 2017 at 13:45 UTC ( #1180556=perlquestion: print w/replies, xml ) Need Help??

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

Dear Monks

In Europe websites should ask the visitors if they allow storing of cookies on their browsers. So before you can send a cookie, you are obliged to give a notice with the explanation of what your cookie does and how information is used. The famous cookie-law. It is under review, but still applicable at this moment. Because my website is European based, it should be conform with this.

My site, using Catalyst, is using session cookies according the book via the plugins:

Session Session::Store::FastMmap Session::State::Cookie

Today I use a variable in the session that tracks if the user already has given his/her permission to use the cookie. However because a session only last an hour, this is annoying for the user to every time he visits my site to give permission again. This leads me to following question:

How can I set an additional long lasting cookie beside my standard session cookie?

This long lasting cookie would have a life span of 3 months and indicate that the user already had been given the notice and it shouldn't be given again until he deletes the cookie or has not been on my site for 3 months.

Because Catalyst handles sessions transparent for me, I have no clue how to create and check this additional cookie. And searching for cookie and Catalyst returns me always back to Session::State::Cookie without addressing the two cookie problem.

So any ideas or links to tutorials that I can use to realise this?

Kind Regards

Replies are listed 'Best First'.
Re: Multiple cookies in Catalyst, one longlasting and one session cookie
by trwww (Priest) on Jan 29, 2017 at 19:45 UTC

    I haven't used it, but it appears that you'd get what you need with Catalyst::Plugin:Session::PerUser and disabling the default 'migrate' functionality. Seems like that would give you an alternate, permanent session that is still seamlessly tied to your user.

    Even if it doesn't do exactly what you want it looks like a good place to start for inspiration.

Re: Multiple cookies in Catalyst, one longlasting and one session cookie
by martell (Hermit) on Aug 27, 2020 at 19:33 UTC

    Responding to my own post because i've found a solution for this problem. It is actually straightforward:

    Set a extra cookie by adding in your function:

    $c->response->cookies->{'cookie_consent'} = { 'name' => 'cookie_consent', 'value' => '1', 'expires' => '+3M', 'max-age' => '+3M', 'domain' => 'yoursite', 'path' => '/', 'secure' => 0, 'samesite' => 'Lax', };

    This will automatically add a cookie when the response is send.

    You can test for the presence of a cookie by testing if the request has a cookie with the given name:

    my $consent_cookie = $c->request->cookies->{'cookie_consent'};

    If the value is set, the cookie exists. You can use the value to record the type of consent the user is given. It is now up to you to react appropriately on the presence or absence of the cookie.

    Hopes this help other. I've been looking for the answer quite some time and had given up. Found the answer after searching for a different problem.

    Kind regards

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1180556]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (10)
As of 2021-05-12 20:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (132 votes). Check out past polls.

    Notices?