【问题标题】:Request source server domain via cURL in PHP在 PHP 中通过 cURL 请求源服务器域
【发布时间】:2011-08-19 16:15:12
【问题描述】:

我有 2 个网络服务器,服务器 A 和服务器 B。两者都运行 PHP5 + Apache + Ubuntu 环境。

服务器 A 通过 PHP 中的 cURL 向服务器 B 发送请求。我想获取请求的源服务器域。据我所知,$_SERVER['REMOTE_ADDR']可以获取源服务器(服务器A)的IP。如果想获取Server A的域,如何获取?

附言服务器 A 托管多个域,因此反向 IP 解析在这种情况下不起作用。

代码如下:

$data = array('user' => $user, 'pass' => $pass);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://ServerB/handler.php');
curl_setopt($ch, CURLOPT_PORT, 80);
curl_setopt($ch, CURLOPT_VERBOSE, 0); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 

$ans_xml = curl_exec($ch);

【问题讨论】:

  • 您可以将其添加为 http 标头吗?还是作为数据字段之一?我认为常规 HTTP 请求不会将域添加到标头中。

标签: php curl


【解决方案1】:
<?  
$data = array('user' => $user, 'pass' => $pass, 'appid' => 'pukeko');
$domain = $_SERVER["SERVER_NAME"]; // user the super global $_SERVER["SERVER_NAME"] or set it  manually to, ex: http://www.myserver.com 

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://ServerB/handler.php');
curl_setopt($ch, CURLOPT_PORT, 80);
curl_setopt($ch, CURLOPT_VERBOSE, 0); 
curl_setopt($ch, CURLOPT_HEADER, 0); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_REFERER, $domain); // USE CURLOPT_REFERER to set the referer 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 

$ans_xml = curl_exec($ch);
?> 

<?
// ServerB - http://ServerB/handler.php
$referer = $_SERVER['HTTP_REFERER'];  // http://www.myserver.com 
?>

超级全局 $_SERVER["SERVER_NAME"] 仅在您通过 apache 调用 scriptA 时才有效,例如:"wget http://serverA/scritptA.php"

更新:

您也可以在帖子数据中发送$domain = $_SERVER["SERVER_NAME"]

$domain = $_SERVER["SERVER_NAME"]
$data = array('user' => $user, 'pass' => $pass, 'appid' => 'pukeko', 'icomefrom' => $domain);

http://ServerB/handler.php 中获取:

$icomefrom = $_POST['icomefrom'];

这样您就不必担心虚假推荐人。

【讨论】:

  • 太完美了!但是如何防止有人伪造相同的请求?
  • 你能用密码保护他脚本所在的文件夹吗?
  • 当然可以,但是您在问题中没有提到任何问题,另外,我问您是否可以密码保护脚本所在的文件夹,但您没有回答,所以,我不确定我可以如何为您提供更多帮助。
  • 我已经更新了答案,因此您不必担心虚假推荐人,请查看。
  • 阅读更新后的答案,但人们也可以使用参数提出相同的请求。
【解决方案2】:

作为 Pelshoff 在上面评论中的统计数据,您应该使用自定义 HTTP 标头:

Custom HTTP headers : naming conventions

【讨论】:

    猜你喜欢
    • 2016-05-14
    • 2017-11-21
    • 2017-04-23
    • 1970-01-01
    • 2011-11-19
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    相关资源
    最近更新 更多