我有两个web服务器,服务器A和服务器B,它们都运行PHP5 + Apache + Ubuntu环境。
服务器A通过PHP中的cURL向服务器B发送请求。我想获取请求的源服务器域。据我所知,$_SERVER['REMOTE_ADDR']可以获取到源服务器(Server )的IP。如果我想获取Server的域名,如何获取?
附注:服务器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);发布于 2011-08-18 01:29:57
<?
$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
?>只有当您通过apache调用scriptA时,超级全局$_SERVER"SERVER_NAME"才能工作,例如:"wget “。
更新:
你也可以在你的帖子数据中发送$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'];这样你就不必担心假推荐人了。
发布于 2011-08-17 19:56:41
正如stats Pelshoff在他上面的评论中所说的,你应该使用自定义的HTTP头:
https://stackoverflow.com/questions/7091947
复制相似问题