Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Setting Cookies First and Redirecting Page

by Anonymous Monk
on Sep 15, 2006 at 18:19 UTC ( [id://573216]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!

All that I am trying to do here is to set these cookies (the cookies part work if) and redirect to another page after the cookies get set, but if I use any of the methods here the cookies will not work. Why?
Can someone give me a hand on that please?
I tried all of them.
Here is the code:

#!/usr/bin/perl use CGI qw/:standard/; use CGI::Carp qw(fatalsToBrowser); use strict; use DBI; use CGI qw(:all); my $query = new CGI; my $name="Joe X"; my $buddy = "more Numbers XY"; my $cookie1 = $query->cookie(-name=>'name', -value=>"$name", -expires=>'+4h', -path=>'/'); my $cookie2 = $query->cookie(-name=>'buddy', -value=>"$buddy", -expires=>'+4h', -path=>'/'); print $query->header(-cookie=>[$cookie1,$cookie2]); my $got_cookie1 = $query->cookie('name'); my $got_cookie2 = $query->cookie('buddy'); if($f_name eq ""){$name = $got_cookie1;} if($buddy eq ""){$buddy = $got_cookie2;} print "<meta http-equiv=\"refresh\" content=\"2\";\ url=http://www.myp +age.com/cgi-bin/per.pl?buddy=$buddy&name=$name\">"; print $query->redirect('http://www.mypage.com/cgi-bin/per.pl?buddy=$bu +ddy&name=$name'); my $url = "http://www.mypage.com/cgi-bin/per.pl?buddy=$buddy&name=$nam +e"; print "Location: $url\n\n"; print $query->end_html;

Thanks a lot!!!!

Replies are listed 'Best First'.
Re: Setting Cookies First and Redirecting Page
by chargrill (Parson) on Sep 15, 2006 at 18:24 UTC

    Which way do you really want to redirect a user? You've got at least three:

    1. meta http-equiv="refresh"
    2. $query->redirect
    3. print "Location: $url\n\n";

    I'd suggest picking one, and then send all your headers at the same time. And certainly don't spit out HTML while the client thinks you're sending headers (print "<meta http-equiv"...).



    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
      It's that way just to show different was that I've tried already. I just don't know how to anymore.
Re: Setting Cookies First and Redirecting Page
by robartes (Priest) on Sep 15, 2006 at 21:19 UTC
    As some people answering your query have already hinted at, possibly your problem is the domain in the cookie. You don't seem to be explicitely setting the domain for your cookies, so they are only valid for the domain they are set from. If your script is not running on www.mypage.com, the cookies are not sent by the browser with the redirected request.

    CU
    Robartes-

Re: Setting Cookies First and Redirecting Page
by ptum (Priest) on Sep 15, 2006 at 18:24 UTC

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (5)
As of 2024-04-16 11:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found