You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Trevor Phillips <ph...@central.murdoch.edu.au> on 2000/08/14 09:05:21 UTC

Redirects issues...

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     -           T.Phillips@murdoch.edu.au : 
| 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)                          /

Re: Redirects issues...

Posted by Roger Espel Llima <es...@iagora.net>.
Trevor Phillips <ph...@central.murdoch.edu.au> wrote:
> 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.
>
> 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:
> [ $r->status(302); ... return OK ]

I've come across the same problem, and I like to set URI along with
Location too.

I don't like the workaround solution, because it makes Apache log a
'200' return code.  What I did was something like this:

	$r->header_out(Location => $url);
	$r->err_header_out(URI => $url);
	err_cookies($r);
	return REDIRECT;

where err_cookies is:

sub err_cookies {
  my $r = shift;
  my @cookies = $r->headers_out->get("Set-Cookie");
  $r->err_headers_out->add("Set-Cookie" => \@cookies);
}

That seems to do the trick for me.

-- 
Roger Espel Llima, espel@iagora.net
http://www.iagora.com/~espel/index.html