Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

redirection failing when cookie is set

by Anonymous Monk
on Sep 08, 2004 at 20:34 UTC ( [id://389459]=perlquestion: print w/replies, xml ) Need Help??

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

I have a page with a drop-down menu to select a region, and a check mark to allow the region to be remembered in future sessions.

When the user selects a region and clicks on "GO", the redirection works. If the user checks the "Remember my selection" box, a cookie is set, but redirection fails. The browser merely displays "Location: http://www.somelocation.com".

Also, how can I get it so that, after the cookie is set, it will automatically be retrieved when the main page is called, so that redirection will automatically take place and appear invisible to the user?

Thanks

use CGI qw/:standard/; my $query = new CGI; my $cookie_in = $query->cookie('region'); if ($cookie_in) { if ($cookie_in eq "americas") { print "Location: http://www.google.com\n\n"; } elsif ($cookie_in eq "canada") { print "Location: http://canada.gc.ca\n\n"; } else { print "Location: http://www.belimo.org\n\n"; } } else { my $checked = param(checked); my $region = param(region); 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->header(-cookie=>$cookie_out); } if ($region eq "americas") { print "Location: http://www.google.com\n\n"; } elsif ($region eq "canada") { print "Location: http://canada.gc.ca\n\n"; } else { print "Location: http://www.belimo.org\n\n"; } }

Replies are listed 'Best First'.
Re: redirection failing when cookie is set
by kutsu (Priest) on Sep 08, 2004 at 20:44 UTC

    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

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

Re: redirection failing when cookie is set
by sandfly (Beadle) on Sep 08, 2004 at 22:19 UTC
    The reason your redirection fails when you set the cookie, is that the location redirection is an HTTP message, but you've printed the header to set the cookie; so the rest of your message is interpreted as text.

    kutsu's fix works because it sets the cookie and redirects in a single command.

    If the main page and the location-setting page are in the same domain, I believe the cookie will be sent from the client, automatically with each request. You might have to specify the domain explicitly when you create the cookie, as in kutsu's example, depending on the URLs of the main and location pages.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-24 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found