Re: Retrieving Cookies does not return all domain values

[email protected] (Gunnar Hjalmarsson) Thu, 26 Feb 2009 19:50:42 +0100
Newsgroups perl.beginners.cgi
Message-ID <[email protected]>
Thomas Hilbig wrote:
> I am in the process of changing my cookies (stored on my users’
> browsers) so they can be shared across multiple servers under my
> domain.  So, instead of writing cookies with domain
> ‘www.mydomain.com’, I am writing them to ‘.mydomain.com’ so they can
> be read by www.mydomain.com, www2.mydomain.com etc.    During the
> transition, I need to be able to retrieve cookie values stored for
> the main domain and the subdomain.
> 
> Browsers will store cookies from ‘www.mydomain.com’ and
> ‘.mydomain.com’ separately, and return multiple values if the target
> servername matches the hierarchy.  However, I cannot find a way under
> CGI.pm or CGI::Cookie to retrieve these multiple values – only one
> value is ever returned.

<snip>

> Is there a method to get CGI.pm to fetch all the values in the
> request, or am I forced to use the environment variable?

I would guess that the problem is that they share the same name. I don't 
know if you can have CGI.pm help you, but OTOH it's not that difficult 
to grab the values:

     my @cookie_values;
     for my $pair ( split /;\s*/, $ENV{HTTP_COOKIE} ) {
         my ($name, $value) = split /=/, $pair;
         s/%(..)/chr(hex $1)/eg for $name, $value;
         push @cookie_values, $value if $name eq 'var';
     }

-- 
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl