Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^3: Setting Cookies First and Redirecting Page

by ptum (Priest)
on Sep 15, 2006 at 18:52 UTC ( [id://573225]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Setting Cookies First and Redirecting Page
in thread Setting Cookies First and Redirecting Page

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.)

Replies are listed 'Best First'.
Re^4: Setting Cookies First and Redirecting Page
by Anonymous Monk on Sep 15, 2006 at 19:14 UTC
    I am using:
    print $query->redirect(-URL => $url,-COOKIE => [$cookie1,$cookie2]);

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

      Works for me:

      #!/usr/bin/perl use CGI qw( :standard ); $cookie = cookie( -name => 'sessionID', -value => 'xyzzy', -expires => '+1h', -path => '/', -domain => '.foo.com', ); print redirect( -url => 'http://login.foo.com', -cookie => $cookie );
      Do you have some other portion of the code outputting header stuff that would interfere? Why do you say it's not working. What client? IE? Firefox?

      -derby

      As I said earlier, I haven't tried it, so I cannot offer any guarantees ... but that is a bit strange. Are you redirecting within the same domain as your cookie? Can you show the code you are using to determine if the cookie is set in the destination script?

      Is it essential that you set the cookies on the first page ... if not, it might be the path of least resistance to set them on the destination page ... ?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://573225]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (4)
As of 2024-03-29 00:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found