views:

74

answers:

1

Hi. I am trying to redirect my visitors to for example http://localhost/site/test.php?lang=en_USbased on the country code in the cookie like this:

if (isset($_COOKIE['country'])) 
{
        $country = $_COOKIE['country'];
        header('Location: test.php?lang=en_US');  

        if ($country == "NO"){              
        header('Location: test.php?lang=no_NO');    
        }                       
}
else
{
    header('Location: test.php?lang=en_US');  
}

But i get this weird error in firefox: The page isn't redirecting properly

Found a solution:

if (!isset($_GET['lang']))
{

        if (isset($_COOKIE['country'])) 
        {
                $country = $_COOKIE['country'];
                $redirect = "en_US";

                if ($country == "NO"){              
                $redirect = "no_NO";    
                header('Location: crime.php?lang='.$redirect); 
                }   

                if ($country == "EN"){              
                $redirect = "en_US";    
                header('Location: crime.php?lang='.$redirect); 
                }   

        }
        else
        {
            header('Location: crime.php?lang=en_US'); 
        }


}       
+2  A: 

The problem is that it is unconditionally redirecting, and always to itself, causing an infinite loop which Firefox detects and stops. You need to add conditions to prevent redirects once the final page has been reached.

goffrie
Sounds logic. How do I stop it from redirecting once the correct page has loaded ? What cause the infinite loop? Is it the if (isset($_COOKIE['country']))?
ganjan
@ganjan It's the very nature of that code. No matter what, you're redirecting. There is no condition that doesn't redirect. Step through the code, and you'll see it. If the cookie is set it redirects, if it isn't set it also redirects.
George Marian