我在服务器的同一目录中有两个php文件(http://www.xxxx.com/php/),
1) write_json.php
$address['http_client']=$_SERVER['http_client_IP'];$address['http_x_forwardd_for']=$_SERVER['http_x_forwarded_for'];$address['http_x_forwarded']=$_SERVER['http_x_forwarded'];$address['http_forwardd_for']=$_SERVER['http_forwarded_for'];$address['http_forwarded']=$_SERVER['http_forwarded'];$address['remote_addr']=$_SERVER['remote_addr'];header('Content-Type:application/json');echo json_encode($address);
2) get_ip.php
$json_ul=$_SERVER['SERVER_NAME']'/php/write_json.php’;$json_string=";$ch=curl_init($json_url);$options=数组(CURLOPT_RETURNTTRANSFER=>true,CURLOPT_HTTPHEADER=>数组("内容类型:application/json"),CURLOPT_POSTFIELDS=>$json_string);curl_setopt_array($ch,$options);$json_string=curl_exec($ch);echo$json_string;
get_ip.phpnbsp 电话 write_json.php
如果我们假设客户端IP为:76.2.35.46,服务器IP为:80.59.3.23
当我打电话时http://www.xxxx.com/php/get_ip.php在客户端浏览器上
它向我显示了服务器IP,而不是客户端IP,如下所示:
{http_client:null,http_x_forwardd_for:null,http_x_forwarded:null,http_forwardd_for:null,http_forwarded:null,remote_addr:"80.59.3.23"}
如何获取客户端IP而不是服务器IP?
您正在通过curl从服务器调用write_json。。。也就是说,服务器实际上正在请求writejson文件,所以writejson看到来自服务器的请求。为什么不使用include而不是curl调用呢?
这样做是不可能的。您需要先获取客户端ip,然后通过curl将其作为发布数据发送到服务器。
尝试获取客户端IP使用以下函数,该函数将返回客户端IP
// Function to get the client IP address
function get_client_ip() {
$ipaddress = '';
if (getenv('HTTP_CLIENT_IP'))
$ipaddress = getenv('HTTP_CLIENT_IP');
else if(getenv('HTTP_X_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_X_FORWARDED_FOR');
else if(getenv('HTTP_X_FORWARDED'))
$ipaddress = getenv('HTTP_X_FORWARDED');
else if(getenv('HTTP_FORWARDED_FOR'))
$ipaddress = getenv('HTTP_FORWARDED_FOR');
else if(getenv('HTTP_FORWARDED'))
$ipaddress = getenv('HTTP_FORWARDED');
else if(getenv('REMOTE_ADDR'))
$ipaddress = getenv('REMOTE_ADDR');
else
$ipaddress = 'UNKNOWN';
return $ipaddress;
}
在变量中获取IP后,发送