Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Setting Cookies First and Redirecting Page

by Anonymous Monk
on Sep 15, 2006 at 18:19 UTC ( #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$buddy&name=$name\">"; print $query->redirect('$bu +ddy&name=$name'); my $url = "$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"...).

    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, the cookies are not sent by the browser with the redirected request.


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, 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?

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

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (3)
As of 2022-09-27 05:27 GMT
Find Nodes?
    Voting Booth?
    I prefer my indexes to start at:

    Results (118 votes). Check out past polls.