【问题标题】:Why does my redirect works randomly为什么我的重定向随机工作
【发布时间】:2014-11-25 07:58:29
【问题描述】:

我正在使用 php 脚本重定向我网站中的用户,它在上周一之前一直运行良好,突然它只能随机运行,有时它会重定向到另一个实例上的所需页面,它会出错。

重定向网址

devaltrack.dailyusers.com/rd?track=130&go=www.bbc.co.uk

有时会出现的错误

在此服务器上找不到请求的 URL /www.bbc.co.uk。

当我深入研究它时,我意识到如果我用http 输入go 值,它将始终有效,否则它会不时有效, 任何人都可以给我这里发生的事情的可能线索

重定向脚本

$redirect_page= '/rccp.php';
$trusted_domain = $_GET['go'];
$track_id = $_GET['track'];

    $strsql = ("CALL select_whitelisturls($track_id)");
    $result = mysql_query($strsql);
    $row = mysql_fetch_array($result);

if($row==""){
    redirect_cookie_user($redirect_page);
}

if($row["redirect"] == 1 ){

    if (false === strpos($trusted_domain, '://')) {
        $trusted_domain = 'http://' . $trusted_domain;
        //$trusted_domain = strtolower($trusted_domain);
    }

    if ($i = strpos($trusted_domain, '/', strpos($trusted_domain, '//')+2)){
        $trusted_domain = strtolower(substr($trusted_domain, 0, $i)). substr($trusted_domain, $i);
    }else{
        $trusted_domain = strtolower($trusted_domain);
    }

    $domainarray = explode("\n", $row['domainname']);
    if ($domainarray[0] == null ){
        exit(); 
    }

    foreach ($domainarray as $value) {
        if (false === strpos($value , '://')) {
            $value = "http://".$value;
            $value = strtolower($value);    
        }else
        {
            $value = strtolower($value);
        }
        $pos = strrpos($trusted_domain, $value);
        $domain_string =parse_url($trusted_domain);
        $value_string =parse_url($value);

        if(($pos !== false) &&($pos == 0) && ($value_string['host'] == $domain_string['host'])){
            if (isset($_COOKIE["id"])){
                $_GET['id'] = $_COOKIE["id"];
                $_GET['go'] = $trusted_domain;
                header('Location: '.$redirect_page.'?'.http_build_query($_GET));
                exit();
            }else{
                header('Location: '.$trusted_domain);
                exit();
            }
        }
    }
}else{
    exit();
}
function redirect_cookie_user($redirect_page){
    if (isset($_COOKIE["id"])){
        $_GET['id'] = $_COOKIE["id"];
        header('Location: '.$redirect_page.'?'.http_build_query($_GET));
    }
    else{
        exit();
    }
}

【问题讨论】:

  • 它被解释为本地 URL 路径,而不是 URL,除非您编写了包含协议前缀的正确 URL。
  • 如果您需要有关实施的帮助(以防它之前确实如您所声称的那样工作),您将不得不显示所述代码。
  • 对不起,这是我的本地跟踪 url.. 等一下编辑它..
  • 任何人都知道是什么导致了错误!!!

标签: php .htaccess redirect


【解决方案1】:

我确实认为您的问题是打印重定向的代码。如果您使用的是 header('Location: xxxx');那么 xxxx 应该始终具有架构(http、https 等),否则浏览器会尝试相对导航。

编辑: 你可以这样做:

// For testing
$go = 'www.bbc.co.uk';                                                                                                                                                                                                                             
//$go = 'http://www.bbc.co.uk';                                                                                                                                                                                                                    

$uriInfo = parse_url($go);                                                                                                                                                                                                                   

if (!isset($uriInfo['scheme'])) {                                                                                                                                                                                                            
  $go = 'http://' . $go;                                                                                                                                                                                                                     
}

header('Location: ' . $go)

现在您将检查架构是否已定义,如果没有,您将附加 'http://'

【讨论】:

  • 等待生病添加代码..你可以检查它的任何缺陷
  • 据我比较,我的代码没有缺陷,不是吗,我想我在上面已经做到了,顺便说一句,你能检查一下我上面的代码吗..谢谢
  • @user3584871 我没有在代码中看到你的错误......我唯一看到的是如果 go 是 'bbc.com://' 它将失败,因为你只需检查是否'://' 在字符串中。您可能想像我在示例中那样进行检查。
  • @user3584871 如果 go 设置为“/bbc.com”,您将重定向到“http:///bbc.com”,这可能在某些浏览器中导致您发布的错误.
【解决方案2】:

如果您不使用“http://”,它将在您的网站空间(不存在)上搜索文件夹“www.bbc.co.uk”。你应该让转发在 url 之前附加一个“//”(让浏览器决定他想要 https 还是 http)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-21
    • 2021-02-07
    • 2014-04-16
    • 1970-01-01
    • 1970-01-01
    • 2013-12-25
    • 2020-07-13
    • 2015-06-16
    相关资源
    最近更新 更多