I'm revisiting a routine I have which in the ContentHandler phase, redirects to
another URI (status 302). While redirecting, I'd like to also set a cookie.

The example in the Eagle Book is fairly straightforward - you set the content
type, set a Location header, then return REDIRECT. Expanding on this, I coded
the following routine:

sub handler
{
    my $r = shift;
    $r->content_type('text/html');
    $r->header_out(URI=>$uri);
    $r->header_out(Location=>$uri);
   
$r->header_out('Set-Cookie'=>CGI::Cookie->new(-name=>$CookieName,-value=>$CookieVal,-path=>'/'));
    return REDIRECT;
}

I set the URI header as well, due to habit. Didn't there used to be a browser
which required URI instead of Location? Is it safe to only use Location??

However, in my output from the above, I don't see the URI heading, and I don't
see the cookie heading. So I came up with the following alternative:

sub handler
{
    my $r = shift;
    $r->status(302);
    $r->content_type('text/html');
    $r->header_out(URI=>$uri);
    $r->header_out(Location=>$uri);
   
$r->header_out('Set-Cookie'=>CGI::Cookie->new(-name=>$CookieName,-value=>$CookieVal,-path=>'/'));
    $r->send_http_header;
    $r->print("Redirecting to <A HREF=\"$uri\">$uri</A>...\n");
    return OK;
}

This works as I want; Gives all the headers, including the all-important
Cookie.

My query is, why didn't the first one work, and is the way I ended up doing it
the best way, or is there a neater solution?

-- 
. Trevor Phillips             -           http://jurai.murdoch.edu.au/ . 
: CWIS Systems Administrator     -           [EMAIL PROTECTED] : 
| IT Services                       -               Murdoch University | 
 >------------------- Member of the #SAS# & #CFC# --------------------<
| On nights such as this, evil deeds are done. And good deeds, of     /
| course. But mostly evil, on the whole.                             /
 \      -- (Terry Pratchett, Wyrd Sisters)                          /

Reply via email to