简体   繁体   English

php,获取客户端IP地址

[英]php, get client ip address

I have two php files in a same directory of a server ( http://www.xxxx.com/php/ ), 我在服务器的同一目录( http://www.xxxx.com/php/ )中有两个php文件,

1) write_json.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 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 calls write_json.php get_ip.php调用write_json.php

if we suppose the client IP is : 76.2.35.46 and the server one is : 80.59.3.23 如果我们假设客户端IP是: 76.2.35.46 ,服务器IP是: 80.59.3.23
when I call http://www.xxxx.com/php/get_ip.php on a client browser 当我在客户端浏览器上调用http://www.xxxx.com/php/get_ip.php

It shows me the Server IP not the client IP like this : 它向我显示服务器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"
}

How can I get the client IP instead of the server one ? 如何获得客户端IP而不是服务器IP?

You are calling write_json via curl from the server... that is, the server is actually requesting the write_json file, so write_json is seeing the request come from the server. 您正在通过curl从服务器调用write_json……也就是说,服务器实际上正在请求write_json文件,因此write_json看到请求来自服务器。 Why not just use an include rather than a curl call? 为什么不只使用一个include而不是curl调用呢?

Not possible this way. 这样是不可能的。 You need to get the client ip first then send it as posted data to server via curl. 您需要首先获取客户端ip,然后将其作为已发布数据通过curl发送到服务器。

Trying to get client IP use following function that will return client IP 尝试使用以下返回客户端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;
}

After getting IP in a variable then send 在变量中获取IP之后发送

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM