我在服务器的同一目录中有两个php文件(http://www.xxxx.com/php/),
1) write_json.php
$address['http_client'] = $_SERVER['HTTP_CLIENT_IP']; $address['http_x_forwarded_for'] = $_SERVER['HTTP_X_FORWARDED_FOR']; $address['http_x_forwarded'] = $_SERVER['HTTP_X_FORWARDED']; $address['http_forwarded_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_url = $_SERVER['SERVER_NAME'] . '/php/write_json.php'; $json_string = ''; $ch = curl_init($json_url); $options = array(CURLOPT_RETURNTRANSFER => true, CURLOPT_HTTPHEADER => array('Content-type: application/json'), CURLOPT_POSTFIELDS => $json_string); curl_setopt_array($ch, $options); $json_string = curl_exec($ch); echo $json_string;
get_ip.php 调用 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_forwarded_for: null, http_x_forwarded: null, http_forwarded_for: null, http_forwarded: null, remote_addr: "80.59.3.23" }
如何获取客户端IP而不是服务器IP?
答案 0 :(得分:2)
您正在通过curl从服务器调用write_json ...也就是说,服务器实际上正在请求write_json文件,因此write_json看到请求来自服务器。为什么不使用包含而不是卷曲调用?
答案 1 :(得分:0)
这种方式不可能。您需要首先获取客户端IP,然后通过curl将其作为发布的数据发送到服务器。
答案 2 :(得分:0)
尝试使用以下将返回客户端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后,发送