我在服务器的同一目录中有两个 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'); 回声 json_encode($address);
2)get_ip.php
$json_url = $_SERVER['SERVER_NAME'] 。'/php/write_json.php'; $json_string = ''; $ch = curl_init($json_url); $options = 数组(CURLOPT_RETURNTRANSFER => 真, CURLOPT_HTTPHEADER => array('Content-type: application/json'), CURLOPT_POSTFIELDS => $json_string); curl_setopt_array($ch, $options); $json_string = curl_exec($ch); 回声 $json_string;
get_ip.php 调用 write_json.php
如果我们假设客户端 IP 是:76.2.35.46 而服务器一个是: 80.59.3.23 ,当我 在客户端浏览器上
调用http://www.xxxx.com/php/get_ip.php时
它向我显示服务器 IP 而不是客户端 IP,如下所示:
{ http_client:空, http_x_forwarded_for:空, http_x_forwarded:空, http_forwarded_for:空, http_forwarded:空, 远程地址:“80.59.3.23” }
如何获取客户端 IP 而不是服务器 IP?