You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Steven Wren <st...@launch.server101.com> on 2000/06/30 09:20:25 UTC

Session Cookies:cant retrieve value

Hey

I am setting up shopping facilities for an online purchases and thought I
would try modperl.  Problem is I am trying to set a cookie and then read
from it in the same handler, by setting the cookie then redirecting to the
same page.  This is because the script will need to be called from many
different places, just passing different store_ids and product_ids.

Currently it sets the cookie and appears to redirects but i can then not
read the cookie value.

This might be a stoopid question but your help is appreciated.

The "logic":-) of my program goes like this...
Handler runs routine which checks if cookie exists - if it does it returns
the value, if cookie not set - it sets up the cookie(name,md5 value etc)
then redirects to itself.  Then when it reloads it will check for a
cookie, which should be now set, then return the value.

Surely there is a modperl shopping cart already??!!  Where does one go to
find it, I have been searching the net for a few days now.

Thanks Heaps!!

P.S. Please reply directly as I am not in the list yet.

stevenw@server101.com

A genius makes no errors,
his mistakes are portals of discovery - James Joyce


Re: Session Cookies:cant retrieve value

Posted by darren chamberlain <da...@boston.com>.
Steven Wren (stevenw@launch.server101.com) said something to this effect:
> Hey
> 
> I am setting up shopping facilities for an online purchases and thought I
> would try modperl.  Problem is I am trying to set a cookie and then read
> from it in the same handler, by setting the cookie then redirecting to the
> same page.  This is because the script will need to be called from many
> different places, just passing different store_ids and product_ids.
> 
> Currently it sets the cookie and appears to redirects but i can then not
> read the cookie value.
> 
> This might be a stoopid question but your help is appreciated.
> 
> The "logic":-) of my program goes like this...
> Handler runs routine which checks if cookie exists - if it does it returns
> the value, if cookie not set - it sets up the cookie(name,md5 value etc)
> then redirects to itself.  Then when it reloads it will check for a
> cookie, which should be now set, then return the value.
> 
> Surely there is a modperl shopping cart already??!!  Where does one go to
> find it, I have been searching the net for a few days now.
> 
> Thanks Heaps!!
> 
> P.S. Please reply directly as I am not in the list yet.
> 
> stevenw@server101.com
> 
> A genius makes no errors,
> his mistakes are portals of discovery - James Joyce

Why are you redirecting? That is a very CGI-ish thing to do, and unnecessary
if you use the full Apache API.

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.

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.