繁体   English   中英

php $ _SERVER ['HTTP_HOST']始终是同一个域,无论我从哪个域请求

[英]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个功能,

  1. 从更新服务器使用cURL请求生成令牌,然后更新服务器返回该令牌并将其创建为文件。
  2. 从服务器端下载,但在检查令牌是否存在并在请求的cURL域上有效之前,令牌只能使用1次-使用后立即从服务器端数据库中将其删除。

为每个请求重新生成相同的令牌,并在完成请求后删除令牌。

没有人可以用这种逻辑欺骗你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM