Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

page not redirecting when cookie is set

Status
Not open for further replies.

Ito2233

MIS
Sep 2, 2003
77
US
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:
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

Code:
use CGI qw/:standard/;
my $query = new CGI;

my $cookie_in = $query->cookie('region');
if ($cookie_in) {
    if ($cookie_in eq "americas") {
        print "Location: [URL unfurl="true"]http://www.americas.com\n\n";[/URL]
    }
    elsif ($cookie_in eq "canada") {
             print "Location: [URL unfurl="true"]http://canada.gc.ca\n\n";[/URL]
         }
         else {
               print "Location: [URL unfurl="true"]http://www.un.org\n\n";[/URL]
         }
    
}

else {
      my $checked = param(checked);
      my $region = param(region);
      if ($checked eq "yes") {
          my $cookie_out = $query->cookie(-name=>'region',-value=>$region,-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: [URL unfurl="true"]http://www.americas.com\n\n";[/URL]
      }
      elsif ($region eq "canada") {
               print "Location: [URL unfurl="true"]http://canada.gc.ca\n\n";[/URL]
           }
           else {
                 print "Location: [URL unfurl="true"]http://www.un.org\n\n";[/URL]
           }
}
 
Here, this is what I was talking about. You can't send the cookie header with the header method and then send the redirect header. You have to use the redirect method to print the redirect header and also have it send the cookie.
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(-location=>"[URL unfurl="true"]http://www.americas.com");[/URL]
    }
    elsif ($cookie_in eq "canada") {
             print $query->redirect(-location=>"[URL unfurl="true"]http://canada.gc.ca");[/URL]
         }
         else {
                print $query->redirect(-location=>"[URL unfurl="true"]http://www.un.org");[/URL]
         }

}

else {
      my $checked = param(checked);
      my $region = param(region);
      my $cookie_out;
      if ($checked eq "yes") {
          $cookie_out = $query->cookie(-name=>'region',-value=>$region);
      }

      if ($region eq "americas") {
          print redirect(-cookie=>$cookie_out, -location=>"[URL unfurl="true"]http://www.americas.com");[/URL]
      }
      elsif ($region eq "canada") {
               print redirect(-location=>"[URL unfurl="true"]http://canada.gc.ca",[/URL] -cookie=>$cookie_out);
           }
           else {
                 print $query->redirect(-location=>"[URL unfurl="true"]http://www.un.org",[/URL] -cookie=>$cookie_out);
           }
}
 
It's interesting. Now redirection works when you check the "Remember my location" box and click GO, but a cookie is never set! Any ideas as to why?
Thanks
 
Make sure you're not declaring your cookie variable inside the 'if' statement as you were doing before. Notice how I had to move "my $cookie_out" to just above the 'if'. That had me stumped for a few minutes when I was working on my previous post.

If that's not it, repost your code.

 
my $cookie_out" is above the if statement, as you posted in your code...but the cookie isn't being created. Here is the code. Thanks

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('[URL unfurl="true"]http://www.google.com');[/URL]
    }
    elsif ($cookie_in eq "canada") {
             print $query->redirect('[URL unfurl="true"]http://canada.gc.ca');[/URL]
         }
         else {
               print $query->redirect('[URL unfurl="true"]http://www.un.org');[/URL]
         }
    
}

else {
      my $checked = param(checked);
      my $region = param(region);
      my $cookie_out;

      if ($checked eq "yes") {
          $cookie_out = $query->cookie(-name=>'region',-value=>$region,-expires=>'Saturday, 31-Dec-04 16:00:00                          GMT',-path=>'/',-domain=>'194.69.170.21');
      }

      if ($region eq "americas") {
          print redirect(-cookie=>$cookie_out, -location=>"[URL unfurl="true"]http://www.americas.com");[/URL]
      }
      elsif ($region eq "canada") {
             print redirect(-cookie=>$cookie_out, -location=>"[URL unfurl="true"]http://canada.gc.ca");[/URL]
      }
      else {
            print $query->redirect(-cookie=>$cookie_out, -location=>"[URL unfurl="true"]http://www.un.org");[/URL]
      }
}
 
It looks like it should work, and it does for me. But I had to change the Domain from the IP address you had to something else. I'm running Mozilla's Firefox and because of security it doesn't store a cookie if the cookie's domain doesn't match the domain in the url that sent it. If I put in the domain for the cookie the IP address of my web server, I had to get to the page via the IP address for the cookie to be set. You can also try leaving the domain argument out of the cookie function and let it get set automatically by the browser.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top