-------- Original Message --------
From: Jonathan <[EMAIL PROTECTED]>
To: Jeff <[EMAIL PROTECTED]>
CC: mod_perl List <modperl@perl.apache.org>
Subject: Re:Apache2::Cookies - getting all names
Date: 26/12/2005 19:19
This is what I do:
my $cookiejar = Apache2::Cookie::Jar->new(
$this->{'ApacheRequest'} );
my @names = $cookiejar->cookies();
DEBUG_COOKIE >0 && print STDERR "-| Found These Cookies : " .
(join " , ",@names) . "\n";
if ( $cookiejar->cookies( $this->{'CookieDefaults'}->{'names'}->
{'Session'} ) )
{
DEBUG_COOKIE >0 && print STDERR "-| Handling cookie: {$this->
{'CookieDefaults'}->{'names'}->{'Session'}} \n";
DEBUG_COOKIE >0 && print STDERR "-| >> Validating... \n";
my %c_cookies = Apache2::Cookie->fetch(
$this->{'ApacheRequest'} );
my $c_value = $c_cookies{
$this->{'CookieDefaults'}->{'names'}-> {'Session'} }->value;
DEBUG_COOKIE >0 && print STDERR "-| \%c_cookies => \{\} ;
\ $c_cookies\{{$this->{'CookieDefaults'}->{'names'}->{'Session'}}\}-
>value = '", $c_value , "'\n";
DEBUG_COOKIE >0 && print STDERR "-| Validating c_value
$c_value \n";
}
Thanks for the code, interesting, but doesn't answer the question about
invalid @names - I guess that your debug message also lists out names
that don't actually exist in the jar?
you've got
my @vals = $cookie->value();
as far as i know, cookies only have a single scalar value. you'll need
to serialize/deserialize any other data structure into it.
The docs state that a cookie can have multiple values, and I have tested
and confirmed this. Ditto multiple cookies with the same name!
Run the following cookies through your code - I think you will find that
you miss the second value on the first cookie, and not see the second
cookie at all:
# -------------------------
# Calling page created two cookies with the same name,
# and each cookie contains multiple values
# Create multiple cookies with same name and multiple values
my $c1 = Apache2::Cookie->new($r,
-name => 'my_session',
-value => ['session1', $r->mtime(), ],
-expires => '+365d',
-domain => '127.0.0.1',
-path => '/',
-secure => 0,
);
$c1->bake($r);
my $c2 = Apache2::Cookie->new($r,
-name => 'my_session',
-value => ['session2', $r->mtime(), ],
-expires => '+365d',
-domain => '127.0.0.1',
-path => '/',
-secure => 0,
);
$c1->bake($r);
# ----------------------------
# In the receiving page... list all the cookies/values
my $jar = Apache2::Cookie::Jar->new( $r )->cookies;
my @cookies = values %$jar;
print "<pre>\n";
for my $cookie ( @cookies ) {
my $name = $cookie->name();
my @vals = $cookie->value();
print "cookie: $name value: @vals\n";
}
print "</pre>\n";
From this behaviour, it seems that $jar is a class presenting as a tied
hash - but hey, that's ok, as values %$jar gets em all!
Any other examples of cookie infra appreciated.
Regards & thanks,
Jeff