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

重定向到正常工作php时出现问题

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

    我对重定向有点问题。注册用户遵循此链接 site.com/reg.php?passkey=1234 但是第一个用户被重定向到基于cookie的正确语言。当用户被重定向时,我需要保留passkey变量。这样地 ?lang=en_US&passkey=1234

    到目前为止,我的代码如下所示:

    if (!isset($_GET['lang']))
    {
    
            if (isset($_COOKIE['country'])) 
            {
    
                    $country = $_COOKIE['country'];
    
                    (...)
    
                    elseif  ( $country == "US" ){       
    
                    $variables = $_GET;
                    $variables['lang'] = "en_US";
    
                    header('Location: ?' . http_build_query($variables));
    
                    exit();
                    }   
    

    这工作:

    reg.php
    reg.php?lang=en_US
    reg.php?lang=en_US&passkey=test
    reg.php?passkey=test&lang=en_US
    

    但这给了 The page isn't redirecting properly 错误

    reg.php?passkey=test
    

    我不明白为什么当所有其他的组合看起来都很完美的时候这就不起作用了。

    2 回复  |  直到 14 年前
        1
  •  0
  •   Dubas    14 年前

    http 1.1规范要求位置必须是绝对uri (见 RFC2616 14.30位置)

    位置 header('Location: ?' . http_build_query($variables)) ;不包含绝对uri。

    你需要这样的东西:

    header('Location: /folder/file.php?'.http_build_query($variables));
    

    如果你需要在不同的地方这样做,你可以使用 $_SERVER['PHP_SELF'] 将当前文件设置为重定向位置。例如

    header('Location: '.$_SERVER['PHP_SELF'].'?'.http_build_query($variables));
    
        2
  •  0
  •   Nándor Kiss    11 年前

    我想,你应该改变 http_build_query($variables) http_build_query($variables, null, '&')

    我希望我的回答有用。