2011-08-17 24 views
2

我有2個Web服務器,服務器A &服務器B.都運行PHP5 + Apache + Ubuntu環境。通過PHP中的cURL請求源服務器域

服務器A通過PHP中的cURL向服務器B發送請求。我想獲取請求的源服務器域。據我所知,$_SERVER['REMOTE_ADDR']可以得到源服務器(服務器A)的IP。如果我想獲得服務器A的域名,我該如何獲取它?

p.s.服務器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); 
+2

你可以添加它作爲一個HTTP標題也許?或者作爲數據字段之一?我不認爲普通的HTTP請求將域添加到標題。 – Pelshoff 2011-08-17 11:19:35

回答

2
<? 
$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

UPDATE:

您也可以在您的文章發送數據$domain = $_SERVER["SERVER_NAME"]

$icomefrom = $_POST['icomefrom']; 

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

,並在http://ServerB/handler.php用得到它

這樣你不必擔心假冒推薦人。

+0

這是完美的!但如何防止某人僞造相同的請求? – Raptor 2011-08-18 06:35:39