> -----Original Message-----
> From: darren chamberlain [mailto:[EMAIL PROTECTED]]
> Sent: Friday, June 30, 2000 8:48 AM
> To: Steven Wren
> Cc: [EMAIL PROTECTED]
> Subject: Re: Session Cookies:cant retrieve value
> 
> 
[snip]
> 
> Try setting the cookie in an early phase of the request 
> (PerlInitHandler or,
> PerlHeaderParserHandler, for example) and set the cookie 
> there. In addition,
> stick the value of the cookie into pnotes and retrieve it 
> during the same
> request from pnotes, and from future requests from the headers.
> 
> I do this pretty often; my PerlInitHandler parses the cookies 
> and puts them
> into pnotes, and in all other request phases I always access 
> them from pnotes,
> for consistency. That way, I know that there is always a cookie there.
> 

<plug>
  Apache::RequestNotes will parse the cookies and place them in pnotes for
you during the init phase in case you don't feel like writing your own
handler for it.  It also takes care of GET/POST requests and file uploads
while it's at it.
<\plug>

--Geoff

> An example:
> 
> package InitHandler;
> sub handler {
>     my ($r) = @_;
> 
>     # I use this line to grab cookies everywhere -- CGI::Cookie and
>     # and Apache::Cookie are too much overhead for me when this is
>     # so much simpler.
>     my %cookies = map { $1 => $2 if (/([^=]+)=(.*)/) }
>                   grep !/^$/, split /;\s*/,$r->header_in('cookie');
> 
>     # the sub generate_a_cookie creates ithe cookie
>     $cookies{'My-Cookie-Name'} ||= generate_a_cookie();
> 
>     # Set the cookie to go into the outgoing headers:
>     $r->push_handlers(PerlFixupHandler =>
>       sub {
>         my ($r) = @_;
>         my $cookies = $r->pnotes('cookies');
>         my $expires = time + 60*60*24; # 24 hours from now
>         $r->headers_out->add('Set-Cookie' => join ('; ',
>                              
> ((sprintf("My-Cookie-Name=%s",$cookies{'My-Cookie-Name'}))
>                               (sprintf("expires=%s",$expires)),
>                               ("path=/"),
>                               
> (sprintf("domain=%s",$r->get_server_name)))));
>         return OK;
>       });
>     
>     # Put the cookie into pnotes for retrieval later in the 
> same request!
>     $r->pnotes('cookies' => \%cookies);
> 
>     return DECLINED; # So any other init handlers get to run
> }
> 
> And then, in a later content handler, I do something like:
> 
> package ContentHandler;
> sub handler {
>     my ($r) = @_;
> 
>     # note this is a hashref!
>     my $cookies = $r->pnotes('cookies');
> 
>     # ... as usual.
> 
>     return OK;
> }
> 
> # In you httpd.conf:
> <Location />
>     PerlInitHandler InitHandler
>     PerlHandler     ContentHandler
>     # Other stuff
> </Location>
> 
> The nice thing about this way of doing it is that %cookies in 
> the other
> handler (or whatever later phase) is a reference to the value 
> in pnotes,
> so it is automatically updated if you modifiy it, and will be 
> sent with
> the current values (i.e., any changes made up to the headers 
> actually being
> sent will be preserved).
> 
> Hope this helps.
> 
> darren
> 
> -- 
> Absence is to love what wind is to fire. It extinguishes the small, it
> enkindles the great..
> 

Reply via email to