代码之家  ›  专栏  ›  技术社区  ›  ganjan

在firefox中,使用基于cookie的if statment重定向时使用header('location:')会产生奇怪的错误

  •  0
  • ganjan  · 技术社区  · 14 年前

    例如,我试图将我的访客重新引导到 http://localhost/site/test.php?lang=en_US

    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');  
    }
    

    但我在firefox中发现了一个奇怪的错误: The page isn't redirecting properly

    找到了解决方案:

    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'); 
            }
    
    
    }       
    
    1 回复  |  直到 14 年前
        1
  •  3
  •   goffrie    14 年前

    问题是,它总是无条件地重定向到自身,导致了一个无限循环,Firefox会检测并停止这个循环。您需要添加条件,以防止在到达最终页面后重定向。