![](/img/trans.png)
[英]PHP redirect based on HTTP_HOST/SERVER_NAME within same domain
[英]php $_SERVER['HTTP_HOST'] is always same domain no matter from which domain i request
现在,例如,我有以下域:
domain1.com domain2.com domain3.com domain4.com
我尝试使用从domain1.com,domain2.com,domain3.com 到 domain4.com的cURL并阻止cURL请求。
domain1.com上的示例代码文件:
try{
$ch = curl_init();
if (FALSE === $ch){
throw new Exception('failed to initialize');
}
curl_setopt($ch, CURLOPT_URL,"http://domain4.com/test3.php?v=1.4");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $msg);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$p_result = curl_exec($ch);
var_dump($p_result);
print_r($p_result);
if (FALSE === $p_result) {
throw new Exception(curl_error(), curl_errno());
curl_close($ch);
} else{
curl_close($ch);
return $p_result;
}
}catch(Exception $e) {
trigger_error(sprintf('Curl failed with error #%d: %s',$e->getCode(), $e->getMessage()),E_USER_ERROR);
}
domain4.com上的示例代码文件:
$domains = array("domain1.com"); //blacklisted
$domainIsValid = array_filter($domains, function ($var) use ($_SERVER) {
return strpos($var, $_SERVER['HTTP_HOST']) !== false;
});
$_SERVER['HTTP_HOST'] // is always domain1.com even if i request from domain3.com
我想念东西吗? 是Apache服务器配置吗?
主机:具有Apache服务器的DigitalOcean Ubuntu 16.04。
PHP文档说: http : //php.net/manual/en/reserved.variables.server.php
'HTTP_HOST' Contents of the Host: header from the current request, if there is one.
您可能从所有域发送相同的标头。
我认为REMOTE_ADDR或REMOTE_HOST更适合用于黑名单,因为HTTP标头很容易被欺骗。
编辑:注意:您的Web服务器必须配置为创建REMOTE_HOST变量。 例如,在Apache中,需要httpd.conf内的HostnameLookups On才能存在。 另请参见gethostbyaddr()。
我终于找到了解决自己问题的答案。
答案是向引用服务器中的真实域HTTP_HOST发送cURL请求到更新服务器,之后在更新服务器中生成令牌:
$token = md5(uniqid($domain, true));//Create token
$stmt4 = $dbh->prepare("INSERT INTO tokens (domain) VALUES (?)");//Store just to know from where the request for later use
$stmt4->bindParam(1, $dm);
// insert one row
$dm = json_encode(array('domain'=>$domain,'token'=>$token),true);
$stmt4->execute();
然后根据域的请求创建一个带有该令牌的文件,该令牌返回,然后检查该令牌(如果在请求域中找到),可以继续更新并删除该令牌。
$exists = checkRemoteFile($domain.'/'.$token);
if ($exists) {
echo "found";
} else{
/* header("HTTP/1.1 401 Unauthorized");
exit();*/
}
function checkRemoteFile($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if(curl_exec($ch)!==FALSE)
{
return true;
}
else
{
return false;
}
}
所以基本上有2个功能,
为每个请求重新生成相同的令牌,并在完成请求后删除令牌。
没有人可以用这种逻辑欺骗你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.