in reply to Setting Cookies First and Redirecting Page

As per the docs, a 'normal' header and a redirect header are mutually exclusive:

"The redirect() function redirects the browser to a different URL. If you use redirection like this, you should not print out a header as well."


No good deed goes unpunished. -- (attributed to) Oscar Wilde
  • Comment on Re: Setting Cookies First and Redirecting Page

Replies are listed 'Best First'.
Re^2: Setting Cookies First and Redirecting Page
by Anonymous Monk on Sep 15, 2006 at 18:42 UTC
    I know, but how would I setup the cookies?

      Looking at the code of redirect() in CGI.pm, it looks to me as though the redirect() header can take a $cookie as an argument:

      sub redirect { my($self,@p) = self_or_default(@_); my($url,$target,$status,$cookie,$nph,@other) = rearrange([[LOCATION,URI,URL],TARGET,STATUS,['COOKIE','COOKIE +S'],NPH],@p); $status = '302 Found' unless defined $status; $url ||= $self->self_url; my(@o); foreach (@other) { tr/\"//d; push(@o,split("=",$_,2)); } unshift(@o, '-Status' => $status, '-Location'=> $url, '-nph' => $nph); unshift(@o,'-Target'=>$target) if $target; unshift(@o,'-Type'=>''); my @unescaped; unshift(@unescaped,'-Cookie'=>$cookie) if $cookie; return $self->header((map {$self->unescapeHTML($_)} @o),@unescaped +); }

      Why not build your cookie, and then pass it to the redirect() header just as you did for the 'normal' header? (I haven't tried this.)

        I am using:
        print $query->redirect(-URL => $url,-COOKIE => [$cookie1,$cookie2]);

        It redirects, but the cookie doesn't get set.