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

This is my first time using cookies and I came across a few problems (I've already read CGI.pm on cpan)

1) Cookies are to be used with the header request. Does that mean I print cookies on the line: print header, start_html; ? Or does it mean I can use them anytime after the request is sent?

2) What type of information is stored in cookies? Ie..let's say we have a user:pass login script, do we set both values in a single cookie or use separate cookies or..?

3) If it is to be used/set on that line above, how does that work? I mean, let's say we have a form to ask for the user:pass info, if you already set the cookies before the form the values will come back undefined.

4) After a login the cookie is stored, how do you remove a cookie using a 'log out' button?

Replies are listed 'Best First'.
Re: Cookies
by The Mad Hatter (Priest) on Jun 30, 2003 at 06:03 UTC
    1. You have to print them with the rest of the header. For example, if $cookie contains the cookie generated by CGI->cookie:
      print header(-cookie => $cookie), start_html;
      should work
    2. As far as I know (and would someone correct me if I'm wrong), you can store pretty much whatever you want, as long as it is ASCII. If you specify "multiple values" to CGI, when you fetch it again (as an array), it'll have the multiple values. Or you could use seperate cookies. It really is up to you.
    3. You have the form send the info the script, the script parses that data and otherwise munges it, then sticks it into a cookie, and returns a header with that cookie.
    4. Have the button call a script that either sets the cookie's value to nothing, or sets its expiration date to a negative time, which should cause the browser to delete the cookie.
    Hope that helps!
•Re: Cookies
by merlyn (Sage) on Jun 30, 2003 at 16:01 UTC