Perl Cookies

S

superman183

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
 
S

Scott Bryce

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?
 
E

Eric Schwartz

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
 
S

superman183

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
 
S

superman183

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
 
E

Eric Schwartz

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
 
S

superman183

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
 
S

superman183

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.
 
E

Eric Schwartz

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
 
E

Eric Schwartz

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
 
T

Tad McClellan

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.
 
A

A. Sinan Unur

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*
 
S

superman183

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
 
G

Gunnar Hjalmarsson

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.
 
G

Gunnar Hjalmarsson

Eric said:
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
 
J

Jürgen Exner

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

Jane

Jürgen Exner said:
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
 
E

Eric Schwartz

Gunnar Hjalmarsson said:
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.

What am I supposed to be looking for?

-=Eric
 
G

Gunnar Hjalmarsson

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

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.
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top