简体   繁体   English

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

[英]php $_SERVER['HTTP_HOST'] is always same domain no matter from which domain i request

Right now for example i have this domains: 现在,例如,我有以下域:

domain1.com domain2.com domain3.com domain4.com domain1.com domain2.com domain3.com domain4.com

I try to use cURL from domain1.com,domain2.com,domain3.com to domain4.com and block the cURL request. 我尝试使用从domain1.com,domain2.com,domain3.com domain4.com的cURL并阻止cURL请求。

Example code file on domain1.com: 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);
}

Example code file on domain4.com: 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

Do i missing something? 我想念东西吗? is it Apache server configuration? 是Apache服务器配置吗?

Host: DigitalOcean Ubuntu 16.04 with Apache server. 主机:具有Apache服务器的DigitalOcean Ubuntu 16.04。

PHP documentation says: http://php.net/manual/en/reserved.variables.server.php 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.

Potentially you are sending the same headers from all domains. 您可能从所有域发送相同的标头。

I think REMOTE_ADDR or REMOTE_HOST would be more appropriate to use for blacklisting as HTTP headers can be easily spoofed. 我认为REMOTE_ADDR或REMOTE_HOST更适合用于黑名单,因为HTTP标头很容易被欺骗。

EDIT: Note: Your web server must be configured to create REMOTE_HOST variable. 编辑:注意:您的Web服务器必须配置为创建REMOTE_HOST变量。 For example in Apache you'll need HostnameLookups On inside httpd.conf for it to exist. 例如,在Apache中,需要httpd.conf内的HostnameLookups On才能存在。 See also gethostbyaddr(). 另请参见gethostbyaddr()。

I'v finally develop a answer to my self. 我终于找到了解决自己问题的答案。

The answer is to send a cURL request to the update server with the real domain HTTP_HOST in referer, After In the update server generate a token: 答案是向引用服务器中的真实域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();

Then on request from domain create a file with that token that return,then check the token if found on the request domain its okay can continue update and delete the token. 然后根据域的请求创建一个带有该令牌的文件,该令牌返回,然后检查该令牌(如果在请求域中找到),可以继续更新并删除该令牌。

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

So basically there is 2 functions, 所以基本上有2个功能,

  1. Generate token with cURL request from the update server and update server return the token and create it as a file. 从更新服务器使用cURL请求生成令牌,然后更新服务器返回该令牌并将其创建为文件。
  2. Download from server side, but before check if the token is exist and valid on request cURL domain,Token can be used only 1 time - after used delete it immediately from database of server-side. 从服务器端下载,但在检查令牌是否存在并在请求的cURL域上有效之前,令牌只能使用1次-使用后立即从服务器端数据库中将其删除。

re-generate token the same for every request and delete the token after done request. 为每个请求重新生成相同的令牌,并在完成请求后删除令牌。

No one can fool you with this logic. 没有人可以用这种逻辑欺骗你。

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

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