You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Axel Andersson <mo...@animanga.nu> on 2002/03/12 16:23:57 UTC

Cookies and redirects

Hello,
I'm having trouble with both setting a cookie and redirecting the user to
another page at the same time. It would appear the cookie is only sent
when a normal header is sent by server.

If I do the following (having baked the cookie first), where $r is the
Apache->request() object:

 	$r->content_type("text/html; charset=iso-8859-1");
 	$r->send_http_header();

I get this header:

  Connection: close
  Date: Tue, 12 Mar 2002 10:39:05 GMT
  Server: Apache/1.3.23 (Unix) mod_perl/1.26
  Content-Type: text/html; charset=iso-8859-1
  Client-Date: Tue, 12 Mar 2002 10:39:05 GMT
  Client-Response-Num: 1
  Client-Transfer-Encoding: chunked
  Set-Cookie: user=12::7c786c222596437b; domain=animanga.nu; path=/;
expires=Wed, 
  12-Mar-2003 10:39:05 GMT

Very nice and all, with cookie set. However, doing:

 	$r->method_number(M_GET);
 	$r->method("GET");
 	$r->headers_in->unset("Content-length");
 	$r->headers_out->add("Location" => "/users.pl");
 	$r->status(REDIRECT);
 	$r->send_http_header();

Which I gather is the normal way to redirect a user, I get this header:

  Connection: close
  Date: Tue, 12 Mar 2002 10:38:36 GMT
  Server: Apache/1.3.23 (Unix) mod_perl/1.26
  Content-Type: text/html; charset=iso-8859-1
  Client-Date: Tue, 12 Mar 2002 10:38:36 GMT
  Client-Response-Num: 1
  Client-Transfer-Encoding: chunked

Right, no Set-cookie there. So what's up? How do I redirect a browser,
and set a cookie at the same time?

Thanks in advance,
Axel Andersson

-- 
morris@animanga.nu
http://www.animanga.nu/morris/

38. Feel cosmos as translucent ever-living presence


Re: Cookies and redirects

Posted by Hans Poo <ha...@integranet.cl>.
El Mar 12 Mar 2002 11:23, Axel Andersson escribió:
> Hello,
> I'm having trouble with both setting a cookie and redirecting the user to
> another page at the same time. It would appear the cookie is only sent
> when a normal header is sent by server.
>
> If I do the following (having baked the cookie first), where $r is the
> Apache->request() object:
>
>  	$r->content_type("text/html; charset=iso-8859-1");
>  	$r->send_http_header();
>
> I get this header:
>
>   Connection: close
>   Date: Tue, 12 Mar 2002 10:39:05 GMT
>   Server: Apache/1.3.23 (Unix) mod_perl/1.26
>   Content-Type: text/html; charset=iso-8859-1
>   Client-Date: Tue, 12 Mar 2002 10:39:05 GMT
>   Client-Response-Num: 1
>   Client-Transfer-Encoding: chunked
>   Set-Cookie: user=12::7c786c222596437b; domain=animanga.nu; path=/;
> expires=Wed,
>   12-Mar-2003 10:39:05 GMT
>
> Very nice and all, with cookie set. However, doing:
>
>  	$r->method_number(M_GET);
>  	$r->method("GET");
>  	$r->headers_in->unset("Content-length");
>  	$r->headers_out->add("Location" => "/users.pl");
>  	$r->status(REDIRECT);
>  	$r->send_http_header();
>
> Which I gather is the normal way to redirect a user, I get this header:
>
>   Connection: close
>   Date: Tue, 12 Mar 2002 10:38:36 GMT
>   Server: Apache/1.3.23 (Unix) mod_perl/1.26
>   Content-Type: text/html; charset=iso-8859-1
>   Client-Date: Tue, 12 Mar 2002 10:38:36 GMT
>   Client-Response-Num: 1
>   Client-Transfer-Encoding: chunked
>
> Right, no Set-cookie there. So what's up? How do I redirect a browser,
> and set a cookie at the same time?
>
> Thanks in advance,
> Axel Andersson

Have you tried printing the headers_out hashref after sending the http header 
to see if the cookie is there ?.

my $headers_out = $r->headers_out;
foreach (keys %$headers_out) {
	warn "$_=$headers_out->{$_}";
}

Hans

Re: Cookies and redirects

Posted by Enrico Sorcinelli <e....@pisa.iol.it>.
On Tue, 12 Mar 2002 16:23:57 +0100
Axel Andersson <mo...@animanga.nu> wrote:

> Hello,
> I'm having trouble with both setting a cookie and redirecting the user to
> another page at the same time. It would appear the cookie is only sent
> when a normal header is sent by server.
> 
> If I do the following (having baked the cookie first), where $r is the
> Apache->request() object:
> 
>  	$r->content_type("text/html; charset=iso-8859-1");
>  	$r->send_http_header();
> 

Hi,
a common trick to solve this is to use err_header_out() request method. For example:

	use Apache::Constants qw(:common);
	$r->err_header_out("Set-Cookie" => $my_cookie );
	$r->custom_response(SERVER_ERROR, $my_custom_redir_location);
	return SERVER_ERROR;

Bye
	Enrico

=========================================================
Enrico Sorcinelli - Gruppo E-Comm - Italia On Line S.p.a.
E-Mail: e.sorcinelli@pisa.iol.it - enrico@sorcinelli.it
=========================================================

Re: Cookies and redirects

Posted by Geoffrey Young <ge...@modperlcookbook.org>.
> Geoff: I think I did this with my own module with no success... I'd end
> up with an extra set of headers, if I was _lucky_...  

perhaps that is due to a general misunderstanding of err_headers_out -
they are sent _even_ on Apache errors (of which REDIRECT is considered
one), not _only_ on errors.  so, if you were setting headers_out to
capture normal transactions and err_headers_out for errors, you might
get an extra set of headers if you were not careful in your own coding
methodology.

> Also, when I got
> it to redirect OK, even when I saw the cookie, sometimes the browser
> would not eat the cookie properly...  I don't have more specific
> details, because this was months ago and the project was not (then)
> under CVS control (now it is, of course)...

well, details are good :) this sounds like a browser issue, though -
if you populate the err_headers_out table with a cookie it will be
presented to the client on a REDIRECT response.

nevertheless, Axel emailed me privately saying that err_headers_out()
solved his issues.

--Geoff

Re: Cookies and redirects

Posted by Issac Goldstand <ma...@beamartyr.net>.
Geoffrey Young wrote:

>Axel Andersson wrote:
>
>>Hello,
>>I'm having trouble with both setting a cookie and redirecting the user to
>>another page at the same time. It would appear the cookie is only sent
>>when a normal header is sent by server.
>>
>
>this is a common problem - you have to add the cookie to the
>err_headers_out table instead of the headers_out table. 
>
>if you are using Apache::Cookie then this is done for you, otherwise
>you have to populate the correct set of headers.
>
>see 
>
>http://perl.apache.org/guide/snippets.html#Sending_Cookies_in_REDIRECT_Resp
>
>or Recipes 3.7 and 3.13 in the mod_perl cookbook
>
>--Geoff
>
Geoff: I think I did this with my own module with no success... I'd end 
up with an extra set of headers, if I was _lucky_...  Also, when I got 
it to redirect OK, even when I saw the cookie, sometimes the browser 
would not eat the cookie properly...  I don't have more specific 
details, because this was months ago and the project was not (then) 
under CVS control (now it is, of course)...

  Issac



Re: Cookies and redirects

Posted by Geoffrey Young <ge...@modperlcookbook.org>.
Axel Andersson wrote:
> 
> Hello,
> I'm having trouble with both setting a cookie and redirecting the user to
> another page at the same time. It would appear the cookie is only sent
> when a normal header is sent by server.
> 

this is a common problem - you have to add the cookie to the
err_headers_out table instead of the headers_out table. 

if you are using Apache::Cookie then this is done for you, otherwise
you have to populate the correct set of headers.

see 

http://perl.apache.org/guide/snippets.html#Sending_Cookies_in_REDIRECT_Resp

or Recipes 3.7 and 3.13 in the mod_perl cookbook

--Geoff

Re: Cookies and redirects

Posted by Issac Goldstand <ma...@beamartyr.net>.
Well, I ran into this problem a while back... I solved it by baking my 
cookies and sening a Refresh: header to the browser.  The format is the 
same as the more popularly used META tag. (eg "Refresh: $time;url=$url")
For example, to immediately redirect from /foo to /bar while setting a 
cookie, I'd do something like:

$cookie->bake;
$r->header_out("Refresh"=>"0;url=bar");
$r->send_http_header;
return OK;

VERY IMPORTANT: Make sure you type url= in lowercase.  It's usually case 
sensitive.

  Issac

Axel Andersson wrote:

>Hello,
>I'm having trouble with both setting a cookie and redirecting the user to
>another page at the same time. It would appear the cookie is only sent
>when a normal header is sent by server.
>
>If I do the following (having baked the cookie first), where $r is the
>Apache->request() object:
>
> 	$r->content_type("text/html; charset=iso-8859-1");
> 	$r->send_http_header();
>
>I get this header:
>
>  Connection: close
>  Date: Tue, 12 Mar 2002 10:39:05 GMT
>  Server: Apache/1.3.23 (Unix) mod_perl/1.26
>  Content-Type: text/html; charset=iso-8859-1
>  Client-Date: Tue, 12 Mar 2002 10:39:05 GMT
>  Client-Response-Num: 1
>  Client-Transfer-Encoding: chunked
>  Set-Cookie: user=12::7c786c222596437b; domain=animanga.nu; path=/;
>expires=Wed, 
>  12-Mar-2003 10:39:05 GMT
>
>Very nice and all, with cookie set. However, doing:
>
> 	$r->method_number(M_GET);
> 	$r->method("GET");
> 	$r->headers_in->unset("Content-length");
> 	$r->headers_out->add("Location" => "/users.pl");
> 	$r->status(REDIRECT);
> 	$r->send_http_header();
>
>Which I gather is the normal way to redirect a user, I get this header:
>
>  Connection: close
>  Date: Tue, 12 Mar 2002 10:38:36 GMT
>  Server: Apache/1.3.23 (Unix) mod_perl/1.26
>  Content-Type: text/html; charset=iso-8859-1
>  Client-Date: Tue, 12 Mar 2002 10:38:36 GMT
>  Client-Response-Num: 1
>  Client-Transfer-Encoding: chunked
>
>Right, no Set-cookie there. So what's up? How do I redirect a browser,
>and set a cookie at the same time?
>
>Thanks in advance,
>Axel Andersson
>