如何重定向保留原始引荐来源字段?

时间:2013-02-02 10:12:39

标签: php redirect url-redirection

我有以下PHP重定向脚本:

if ($country=="IL") { header('Location: http://iquality.itayb.net/index-he.html'); }
else { header('Location: http://iquality.itayb.net/index-en.html'); }

根据$country的值,将用户重定向到不同的页面。引荐者成为重定向页面本身。

如何保留原始引荐来源字段?

1 个答案:

答案 0 :(得分:10)

您无法使用header('Referer: SOME_REFERER_URL'),因为无论如何浏览器都会覆盖它。

如果您拥有重定向目标iquality.itayb.net,则有几种方法可以执行此操作:

  1. 在用户会话中保存引用。

    // in your first script save real referer to session
    $_SESSION['REAL_REFERER'] = $_SERVER['HTTP_REFERER'];
    
    // in the redirected script extract referer from session
    $referer = '';
    if (isset($_SESSION['REAL_REFERER'])) {
        $referer = $_SESSION['REAL_REFERER'];
        unset($_SESSION['REAL_REFERER']);
    }
    else {
        $referer = $_SERVER['HTTP_REFERER'];
    }
    
  2. 将referer作为参数发送:

    // in your first script
    header('Location: http://iquality.itayb.net/index-he.html?referer=' . $_SERVER['HTTP_REFERER']);
    
    // in your refered script extract from the parameter
    $referer = '';
    if (isset($_REQUEST['referer'])) {
        $referer = $_REQUEST['referer'];
    }
    else {
        $referer = $_SERVER['HTTP_REFERER'];
    }
    
  3. 如果您想欺骗任何其他服务器,请使用以下内容:

    $host = 'www.yourtargeturl.com';
    $service_uri = '/detect_referal.php';
    $vars ='additional_option1=yes&additional_option2=un';
    
    $header = "Host: $host\r\n";
    $header .= "User-Agent: PHP Script\r\n";
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
    $header .= "Referer: {$_SERVER['HTTP_REFERER']} \r\n";
    $header .= "Content-Length: ".strlen($vars)."\r\n";
    $header .= "Connection: close\r\n\r\n";
    
    $fp = fsockopen("".$host,80, $errno, $errstr);
    if (!$fp) {
      echo "$errstr ($errno)<br/>\n";
      echo $fp;
    } else {
        fputs($fp, "POST $service_uri  HTTP/1.1\r\n");
        fputs($fp, $header.$vars);
        fwrite($fp, $out);
    
        while (!feof($fp)) {
            echo fgets($fp, 128);
        }
        fclose($fp);
    }