Request source server domain via cURL in PHP
I have 2 web servers, Server A & Server B. Both running PHP5 + Apache + Ubuntu environment.
Server A sends a request via cURL in PHP to Server B. I would like to get the source server domain of the request. As far as I know, $_SERVER['REMOTE_ADDR']
can get the IP of the source server (Server A). If I want to get the domain of Server A, how can I get it?
p.s. Server A hosts multiple domains, thus reverse IP resolve does not work in this case.
Here are the codes :
$data = array('user' => $user, 'pass' => $pass);
$ch = curl_init();
curl_setopt开发者_JAVA技巧($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);
<?
$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
?>
The super global $_SERVER["SERVER_NAME"] will only work if you call scriptA via apache, ex: "wget http://serverA/scritptA.php"
UPDATE:
You can also send $domain = $_SERVER["SERVER_NAME"]
in your post data:
$domain = $_SERVER["SERVER_NAME"]
$data = array('user' => $user, 'pass' => $pass, 'appid' => 'pukeko', 'icomefrom' => $domain);
and in http://ServerB/handler.php get it with:
$icomefrom = $_POST['icomefrom'];
This way you don't have to worry with fake referers.
As stats Pelshoff in his comment above, you should use custom HTTP header:
Custom HTTP headers : naming conventions
精彩评论