Perl Cookies

Discussion in 'Perl Misc' started by superman183@nowhere.com, Sep 1, 2005.

  1. Guest

    Hi:

    I'm trying, with zero success, to set multiple values in a single
    cookies ... which I know shouldn't be that difficult at all. However,
    whatever I try, it still seems to store only the first value. I'm using
    a freebie piece of code from Bewley.net, which is in common usage, and
    works fine, except for the fact I don't seem able to store multiple
    values:

    Here's the piece of code that does the cookie writing:

    print "Set-Cookie: ";
    print ($name1, "=", $value1, "; expires=", $expiration, "; path=",
    $path, "; domain=", $domain, "; ", $secure, "\n");

    print "Content-type: text/html\n\n";

    However, if I try inserting $name2/$value2, separated from the first
    value with a semi-colon, it just doesn't store it ... I even tried just
    using plain text values to test it (ie. n=testvalue), but still nothing
    .... The first name/value is stored perfectly.

    Any ideas appreciated ... I would like to use the same code, rather
    than a module, simply because of lack of overhead, although I'm sure
    there are a number of modules out there ... I'm really more interested
    in why I can't get the code above to work with multiple values.

    Thanks in advance!
    Superman183 aka. Susan
    , Sep 1, 2005
    #1
    1. Advertising

  2. Scott Bryce Guest

    wrote:

    > I'm trying, with zero success, to set multiple values in a single
    > cookies


    Am I mistaken, or can a cookie only store one value? What happens when
    you try to set multiple cookies?
    Scott Bryce, Sep 1, 2005
    #2
    1. Advertising

  3. "" <> writes:
    > Here's the piece of code that does the cookie writing:
    >
    > print "Set-Cookie: ";
    > print ($name1, "=", $value1, "; expires=", $expiration, "; path=",
    > $path, "; domain=", $domain, "; ", $secure, "\n");
    >
    > print "Content-type: text/html\n\n";


    Ick.

    #!/usr/bin/perl
    use warnings;
    use strict;
    use CGI qw/:standard/;
    use CGI::Cookie;

    my $cookie = new CGI::Cookie(-name => $name1,
    -value => $value1,
    -expires => $expiration,
    -path => $path,
    -domain => $domain,
    -secure => $secure);
    print header(-cookie => $cookie);
    __END__

    > However, if I try inserting $name2/$value2, separated from the first
    > value with a semi-colon, it just doesn't store it ... I even tried just
    > using plain text values to test it (ie. n=testvalue), but still nothing
    > ... The first name/value is stored perfectly.


    Your problem here is not with Perl; it's with your misunderstanding
    about how cookies work. There's nothing wrong with that, but learning
    how to partition your problems properly will aid you in finding
    answers to your questions about it. In this case, you might try
    posting your question to comp.infosystems.www.authoring.cgi, as they
    will have more people who know more about that sort of thing.

    > Any ideas appreciated ... I would like to use the same code, rather
    > than a module, simply because of lack of overhead,


    Once you've decided to use Perl, you have already used enough overhead
    that the additional use of a module is nearly irrelevant, and they
    will save you from all sorts of problems you haven't even considered
    yet, such as escaping special characters in your cookie's name or
    value. It's really worth it.

    > although I'm sure
    > there are a number of modules out there ... I'm really more interested
    > in why I can't get the code above to work with multiple values.


    Cookies don't work that way, but that's not Perl's fault; you need to
    learn more about how cookies operate.

    -=Eric
    Eric Schwartz, Sep 1, 2005
    #3
  4. Guest

    A single cookie can certainly store multiple values, but I may be going
    about it the wrong way, but separating the values with a semi-colon, as
    per the rest of the values seems pretty logical. The rest of the
    values, (ie. path, expiration etc,) when I'm trying to set multiple
    values, are set correctly.

    I did try setting multiple cookies, but it's not really what I want to
    do, and - for other reasons - screwed up ...

    Thx,
    Susan
    , Sep 1, 2005
    #4
  5. Guest

    Eric,

    The question explicitly said, for good reason, that I don't wish to use
    modules. I don't expect you to understand all of my reasons for that,
    but if you can only reply with a reponse which explicitly includes
    modules, and laced with plenty of attitude to boot, then I suggest you
    don't bother answering at all.

    Ok, so "... cookies don't work that way ..." ... That's fine, but I
    need to know, if that's the case, how they do work, so I can modify my
    code to do what I need it to. If someone is able to assist with /
    explain that, then that would be truly appreciated.

    Thanks.
    Susan
    , Sep 1, 2005
    #5
  6. "" <> writes:
    > The question explicitly said, for good reason, that I don't wish to use
    > modules. I don't expect you to understand all of my reasons for that,
    > but if you can only reply with a reponse which explicitly includes
    > modules, and laced with plenty of attitude to boot, then I suggest you
    > don't bother answering at all.


    I'm sorry, did I miss the part where you get to dictate what sort of
    response I make? This is USENET, not superman183's personal free
    support desk. In any event, I have yet to receive your cheque, so
    until then, I'm responding on my own time, however I care to. You
    claim to have good reasons not using modules, but I may not agree
    they're good, and in any event, someone finding this discussion later
    on via Google will have a choice of solutions available to them even
    if you don't like my solution.

    > Ok, so "... cookies don't work that way ..." ... That's fine, but I
    > need to know, if that's the case, how they do work, so I can modify my
    > code to do what I need it to. If someone is able to assist with /
    > explain that, then that would be truly appreciated.


    And my point was that *that* information is entirely irrelevant to
    Perl, and in fact will be the same information regardless of what
    language you choose to implement your solution in, so you're better
    off asking in a newsgroup which is actually about such things, and not
    in a newsgroup which isn't.

    In summary, I commented on the Perl-related aspect of your problem,
    which comment you are free to ignore as you like, and told you where
    to find help on the non-Perl-related aspect of your problem. I really
    don't see why you're getting so upset about this.

    -=Eric
    Eric Schwartz, Sep 1, 2005
    #6
  7. Guest

    Not upset in the slightest Eric, but if you were to use your time
    actually answering questions accurately and helpfully, and ignoring
    those questions to which you're unable to provide a useful response,
    then I know from experience that this newsgroup would be well-served.

    Don't get all upset now ... lol.

    Susan
    , Sep 1, 2005
    #7
  8. Guest

    Oh look ... Eric still hasn't been able to provide a useful or accurate
    response ... Never mind, maybe he's gone off to play with his toys.

    Best,
    Susan.
    , Sep 1, 2005
    #8
  9. "" <> writes:
    > Not upset in the slightest Eric, but if you were to use your time
    > actually answering questions accurately


    What information did I provide that was inaccurate?

    > and helpfully,


    I pointed out a different solution, which I personally prefer, to your
    Perl problem. You're not obligated to use it, but I'm not writing
    just for you, I'm writing to anyone who has a similar problem now or
    in the future. I also told you where to find the information about
    your problem that was not Perl-related. What, exactly, was not
    helpful about that? (Hint: "You didn't write my code for me" is not a
    valid answer.)

    I would note, by the way, that if you used CGI::Cookie, you could have
    used ';', or any other character, to separate the multiple values in
    your cookie, and it would have escaped everything properly.

    > and ignoring those questions to which you're unable to provide a
    > useful response, then I know from experience that this newsgroup
    > would be well-served.


    I'm not only entirely able to provide a useful response, I did. That
    you don't wish to use it does not in the slightest affect its
    usefulness in general.

    > Don't get all upset now ... lol.


    Gosh, I was going to get upset, but then I saw the 'lol', so now
    everything's all cheery and hunky-dory! Golly gee whillikers, that's
    lucky!

    -=Eric
    Eric Schwartz, Sep 1, 2005
    #9
  10. "" <> writes:
    > Oh look ... Eric still hasn't been able to provide a useful or accurate
    > response ... Never mind, maybe he's gone off to play with his toys.


    Oh look ... Susan won't be getting nearly as many helpful responses in
    the future.

    *PLONK*

    -=Eric
    Eric Schwartz, Sep 1, 2005
    #10
  11. <> wrote:


    > I suggest you
    > don't bother answering at all.



    I think you will probably get lots of that.


    > Ok, so "... cookies don't work that way ..." ... That's fine, but I
    > need to know, if that's the case, how they do work,



    But that would be off-topic in a Perl newsgroup.


    > If someone is able to assist with /
    > explain that, then that would be truly appreciated.



    Asking in a place where it is on-topic will greatly increase
    your chances of finding such a someone.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Sep 1, 2005
    #11
  12. Gunnar Hjalmarsson, Sep 1, 2005
    #12
  13. "" <> wrote in
    news::

    > The question explicitly said, for good reason, that I don't wish to
    > use modules. I don't expect you to understand all of my reasons for
    > that, but if you can only reply with a reponse which explicitly
    > includes modules, and laced with plenty of attitude to boot, then I
    > suggest you don't bother answering at all.


    Hmmmm ...

    > Ok, so "... cookies don't work that way ..." ... That's fine, but I
    > need to know, if that's the case, how they do work,


    Then go find out. We discuss Perl here, not HTTP.

    Anyway, *PLONK*

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Sep 2, 2005
    #13
  14. Guest

    Ok, so let me get that straight:

    List of jerks who have replied so far: [ Eric Schwartz, A. Sinan Unur,
    Tad McClellan ] ...

    List of useful & helpful people: [ Gunnar Hjalmarsson ]

    Many thanks Gunnar! Your short and succinct reply is the most helpful
    by far. It took three other people a lot more time and a lot more lines
    of text to be completely hopeless and ignorant.

    Best,
    Susan
    , Sep 2, 2005
    #14
  15. wrote:
    > Ok, so let me get that straight:
    >
    > List of jerks who have replied so far: [ Eric Schwartz, A. Sinan Unur,
    > Tad McClellan ] ...
    >
    > List of useful & helpful people: [ Gunnar Hjalmarsson ]
    >
    > Many thanks Gunnar! Your short and succinct reply is the most helpful
    > by far. It took three other people a lot more time and a lot more lines
    > of text to be completely hopeless and ignorant.


    Well, thanks, kind of. Since I had done just what you asked about, it
    was easy to point you to an example.

    Nevertheless, I acknowledge your appreciation with mingled feelings.
    Why? Because I see nothing wrong with the other replies. The posters,
    who you refer to as "jerks", are three of the most skilled and helpful
    participants in this group.

    You _did_ not have a Perl problem. Your problem was a misconception as
    regards how cookies work, and that _is_ off topic in this group.

    This is Usenet, so any attempt to dictate what kind of responses you
    want or don't want to see is deemed to fail.

    IMNSHO, you owe "the jerks" an apology. Even if I'm not sure that they
    will see it, since they may have filtered out posts from you by now, I
    still think you should post it.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 2, 2005
    #15
  16. Eric Schwartz wrote:
    > if you used CGI::Cookie, you could have
    > used ';', or any other character, to separate the multiple values in
    > your cookie, and it would have escaped everything properly.


    What if the values include ';' characters? ;-)

    I've concluded that modules are not very suitable tools if you really
    want multiple values in the same cookie, since the values should better
    be URI escaped before the concatenation. See the bottom of the source of
    http://search.cpan.org/perldoc?CGI::ContactForm::MHonArc

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 2, 2005
    #16
  17. wrote:
    > Not upset in the slightest Eric, but if you were to use your time
    > actually answering questions accurately and helpfully, and ignoring
    > those questions to which you're unable to provide a useful response,
    > then I know from experience that this newsgroup would be well-served.


    If you would learn how to quote properly, how to partition your problem
    correctly, and where to ask a question that is not related to Perl in the
    first place, then this group would be served even better.

    Bye bye, superman

    jue
    Jürgen Exner, Sep 2, 2005
    #17
  18. Jane Guest

    Jürgen Exner wrote:
    > wrote:
    > > Not upset in the slightest Eric, but if you were to use your time
    > > actually answering questions accurately and helpfully, and ignoring
    > > those questions to which you're unable to provide a useful response,
    > > then I know from experience that this newsgroup would be well-served.

    >
    > If you would learn how to quote properly, how to partition your problem
    > correctly, and where to ask a question that is not related to Perl in the
    > first place, then this group would be served even better.
    >
    > Bye bye, superman
    >
    > jue



    Jürgen, you really should have responded earlier, and then you could
    also have been put onto the list of other jerks, where you so obviously
    belong.

    Have a great day!
    Susan
    Jane, Sep 2, 2005
    #18
  19. Gunnar Hjalmarsson <> writes:
    > Eric Schwartz wrote:
    >> if you used CGI::Cookie, you could have
    >> used ';', or any other character, to separate the multiple values in
    >> your cookie, and it would have escaped everything properly.

    >
    > What if the values include ';' characters? ;-)


    Then as, I said, it works fine-- all ';' characters in values are
    properly escaped for you.

    #!/usr/bin/perl
    use warnings;
    use strict;
    use CGI::Cookie;

    my $cookie = new CGI::Cookie(-name => 'id', -value => "1;2;3");

    print "Cookie: [$cookie]\n";
    __END__

    prints:

    Cookie: [id=1%3B2%3B3; path=/]

    > I've concluded that modules are not very suitable tools if you really
    > want multiple values in the same cookie, since the values should
    > better be URI escaped before the concatenation.


    CGI::Cookie handles this for you, which is one of the main reasons I
    cited for using it in the first place.

    > See the bottom of the
    > source of
    > http://search.cpan.org/perldoc?CGI::ContactForm::MHonArc


    What am I supposed to be looking for?

    -=Eric
    Eric Schwartz, Sep 6, 2005
    #19
  20. Eric Schwartz wrote:
    > Gunnar Hjalmarsson <> writes:
    >> Eric Schwartz wrote:
    >>> if you used CGI::Cookie, you could have
    >>> used ';', or any other character, to separate the multiple values in
    >>> your cookie, and it would have escaped everything properly.

    >>
    >> What if the values include ';' characters? ;-)

    >
    > Then as, I said, it works fine-- all ';' characters in values are
    > properly escaped for you.


    <snip>

    > use CGI::Cookie;
    >
    > my $cookie = new CGI::Cookie(-name => 'id', -value => "1;2;3");


    None of those values includes a ';' char. I rather meant:

    my $val1 = 'abc;def';
    my $val2 = 'someothervalue';
    my $cookie = new CGI::Cookie(-name => 'id', -value => "$val1;$val2");

    Guess that's enough to clarify my point.

    >> I've concluded that modules are not very suitable tools if you really
    >> want multiple values in the same cookie, since the values should
    >> better be URI escaped before the concatenation.


    >> See the bottom of the source of
    >> http://search.cpan.org/perldoc?CGI::ContactForm::MHonArc

    >
    > What am I supposed to be looking for?


    I was hoping you would study the handrolled code for setting respective
    getting a multiple value cookie, which, unlike CGI or CGI::Cookie, does
    handle this case properly.

    my $val1 = 'abc;def';
    my $val2 = 'someothervalue';
    my $cookie = 'id=' . ( join ':', map
    { (my $elem = $_) =~
    s/([^-\w.!~*'()])/sprintf '%%%02X', ord $1/eg; $elem }
    $val1, $val2 );

    my ($values) = $cookie =~ /id=([^;]*)/;
    ($val1, $val2) = map { s/%(..)/chr(hex $1)/eg; $_ }
    split /:/, $values;

    print "$cookie\n\n";
    print "$val1\n$val2\n";

    Outputs:
    id=abc%3Bdef:someothervalue

    abc;def
    someothervalue

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Sep 6, 2005
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex Nitulescu

    Response.Cookies vs Request.Cookies

    Alex Nitulescu, Feb 3, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    8,454
    Hans Kesting
    Feb 3, 2005
  2. Andy Fish
    Replies:
    3
    Views:
    6,504
    Fredrik Lindner
    Nov 6, 2003
  3. user
    Replies:
    3
    Views:
    647
    =?ISO-8859-1?Q?G=F6ran_Andersson?=
    Mar 31, 2007
  4. archana
    Replies:
    1
    Views:
    499
  5. _Who
    Replies:
    7
    Views:
    2,630
Loading...

Share This Page