php 如何重定向保留原始引用字段?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14660818/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to redirect preserving original referrer field?
提问by iTayb
I have the following PHP redirect script:
我有以下 PHP 重定向脚本:
if ($country=="IL") { header('Location: http://iquality.itayb.net/index-he.html'); }
else { header('Location: http://iquality.itayb.net/index-en.html'); }
This redirects the user to different pages, according to $country's value. The referrer becomes the redirection page itself.
这会根据$country的值将用户重定向到不同的页面。引荐来源成为重定向页面本身。
How can I preserve the original referrer field?
如何保留原始引荐来源字段?
回答by Viacheslav Dobromyslov
You can not use header('Referer: SOME_REFERER_URL')because browser will overwrite it anyway.
您不能使用,header('Referer: SOME_REFERER_URL')因为无论如何浏览器都会覆盖它。
If you own redirected targets iquality.itayb.netthen there are several ways to do this:
如果您拥有重定向的目标,iquality.itayb.net那么有几种方法可以做到这一点:
Save referer in the user session.
// 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']; }Send referer as parameter:
// 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']; }
在用户会话中保存引用者。
// 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']; }将引用作为参数发送:
// 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']; }
If you want to cheat any other server then use something like this:
如果您想欺骗任何其他服务器,请使用以下内容:
$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);
}

