in reply to redirection failing when cookie is set

I had problems with Location redirection, and came up with this solution, note that it sends a single header:

my $c = $query->cookie( -name => 'somecookie', -expires => '+3h', -domain => "$ENV{HTTP_HOST}", ...blahblahblah... ); my $rediret_uri = "http://www.perlmonks.com"; #for example ;) print $query->redirect(-cookie => $c, -uri => $redirect_uri);

hope this helps

Update: changed variable $turi to $redirect_uri...that's what I get for copy and paste.

"Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

Replies are listed 'Best First'.
Re^2: redirection failing when cookie is set
by bradcathey (Prior) on Oct 05, 2004 at 15:44 UTC

    Just found this solution via Super Search and it solved *my* problem. Thanks much. Note: At first it didn't work because for debugging purposes I had print "Content-type: text/html\n\n";at the very start of my script--another printing of the header!


    —Brad
    "Don't ever take a fence down until you know the reason it was put up." G. K. Chesterton
Re^2: redirection failing when cookie is set
by Anonymous Monk on Sep 09, 2004 at 13:17 UTC
    Thank you Kutsu, I'll try your code. I just have one question....what's "-uri" and "$turi"? What are they supposed to be?

    Thanks

      From CGI.pm doc:
      Most CGI.pm routines accept several arguments, sometimes as many as 20 optional ones! To simplify this interface, all routines use a named argument calling style that looks like this:
      print $q->header(-type=>'image/gif',-expires=>'+3d');
      Each argument name is preceded by a dash. Neither case nor order matters in the argument list. -type, -Type, and -TYPE are all acceptable. In fact, only the first argument needs to begin with a dash. If a dash is present in the first argument, CGI.pm assumes dashes for the subsequent ones.
Re^2: redirection failing when cookie is set
by Anonymous Monk on Sep 09, 2004 at 14:44 UTC
    Well, now redirection works when you check the "Remember my selection box", but the cookie is never set....I'm not sure why. Before, the cookie was being set, but redirection would not work in this case.

    Here is the modified code:

    use CGI qw/:standard/; my $query = new CGI; my $cookie_in = $query->cookie('region'); if ($cookie_in) { if ($cookie_in eq "americas") { print $query->redirect('http://www.google.com'); } elsif ($cookie_in eq "canada") { print $query->redirect('http://canada.gc.ca'); } else { print $query->redirect('http://www.un.org'); } } else { my $checked = param(checked); my $region = param(region); if ($region eq "americas") { $redirect_uri = "http://www.google.com"; } elsif ($region eq "canada") { $redirect_uri = "http://canada.gc.ca"; } else { $redirect_uri = "http://www.un.org"; } if ($checked eq "yes") { my $cookie_out = $query->cookie(-name=>'region',-value=>$reg +ion,-expires=>'Saturday, 31-Dec-04 16:00:00 GMT',-path=>'/',-domain=>'194.69.170.21'); print $query->redirect(-cookie => $cookie_out, -uri => $redi +rect_uri); } else { print $query->redirect($redirect_uri); } }

      You will proably need to set the domain, see my eariler code, to get the cookie to work, see sandfly's explaination on why.

      "Cogito cogito ergo cogito sum - I think that I think, therefore I think that I am." Ambrose Bierce

        I changed the domain from '194.69.170.21' to "$ENV{HTTP_HOST}", and the results are the same :(
        I changed the domain from '194.69.170.21' to "$ENV{HTTP_HOST}", and the results are the same :(

        Any other ideas? Thanks