为什么我的重定向是随机的

时间:2014-11-25 07:58:29

标签: php .htaccess redirect

我正在使用php脚本重定向我站点中的用户,它在上个星期一工作正常,突然它可以正常工作,有时它会重定向到另一个实例上的所需页面,但它会出错。

重定向网址

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

有时会出现错误

  

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

当我深入研究它时,我意识到如果我用go键入http值,它将始终有效,如果没有,它会不时地工作, 任何人都可以告诉我这里有什么可能的线索

重定向脚本

$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();
    }
}

2 个答案:

答案 0 :(得分:1)

我认为您的问题是打印重定向的代码。如果您使用的是标题('位置: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://'

答案 1 :(得分:0)

如果您不使用“http://”,它将在您的网站空间(不存在)中搜索文件夹“www.bbc.co.uk”。您应该在转发符号前面向前追加“//”(这样可以让浏览器确定他是否需要https或http)