2

我知道问题,但没有在stackoverflow上找到真正的答案。这不是X_FORWARDED_FORSERVER_NAME或者SERVER_REMOTE_ADDR,我想获取连接到我的服务器的远程客户端的本地 IP 地址,以检测谁真正在本地远程网络上连接。

解释一下:

ISP  <---->  ROUTER <----> LOCAL NETWORK <----> LOCAL PC

我想知道什么?

  1. 已连接远程客户端的公共 IP 地址$_SERVER["REMOTE_ADDR"],可以,但是!...
  2. 公网连接客户端的本地IP地址(192.168.xx、10.xxx、172.xxx)

如何解决这个问题呢?我有答案,所以我认为如果想知道本地 IP 地址,每个人都应该知道:

你应该使用CURLcurl_getinfo()功能。然后,指向您想要的任何人的 URL 地址(您的主服务器 ip 或其他),例如:

<?php
    $ch = curl_init();

    $opt = curl_setopt($ch, CURLOPT_URL, "YOUR_SOME_URL_ADDRESS"); 

    curl_exec($ch);

    $response = curl_getinfo($ch);

    $result = array('client_public_address' => $response["primary_ip"],
                    'client_local_address' => $response["local_ip"]
            );

    var_dump($result);

    curl_close($ch);

?>

Focus on $response["primary_ip"] which responses your Public address and $response["local_ip"] which reponses local address. Now this is example:

ISP  <---->  ROUTER <----> LOCAL NETWORK <----> LOCAL PC
 /\                                              /\
 ||                                              ||
 \/                                              \/
$response["primary_ip"]                  <----> $response["local_ip"]
213.x.x.x  (for example)                        192.168.1.3 (for example)

Result:

array (size=2)
  'client_public_address' => string '213.xxx.xxx.xxx' (length=14)
  'client_local_address'  => string '192.168.1.3' (length=11)

This will NOT be giving a REAL local IP address!

Thank you.

4

4 回答 4

1

This will never get to work.
First, you need an HTTP server on a client to make it response to CURL.
Even if ther would be - it is not guarantted to return whatever "local IP". Next, an HTTP server which pings it's clients back looks suspicious and most likely gets banned.

Anyway, I tried your code on a remote IP:

array(2) {
  ["client_public_address"]=>
  string(7) "8.8.8.8"
  ["client_local_address"]=>
  string(0) ""
}

it returned nothing

While when I tried my local router it works okay

array(2) {
  ["client_public_address"]=>
  string(13) "XXX"
  ["client_local_address"]=>
  string(12) "192.168.1.37"
}

It works only for your own local router, but it cannot get whatever local address from a remote system.
Frankly, it shows you just local IP address assigned to your PC.

于 2013-02-23T19:23:27.157 回答
1

The local IP addressed is not exposed to the application layer by default. You need lower level tools that can extract this information from the network packets.

于 2013-02-23T19:26:38.040 回答
0

You can use HTML5 features (CORS and WebSocket) for determining the range (xxx.xxx.xxx.xxx/24) see this live example:

JS-RECON

How its work? You can scan range of IP addresses (192.168.1.0/24, 192.168.0.1/24, etc) and find the range that used by user, however, finding exact IP address is still difficult.

Result: Doesn't work. IP is 192.168.1.200 returns 192.168.1.2; site invalid, tools don't really work.

于 2013-02-23T19:46:01.843 回答
-3
    function getRealIpAddr(){ 
        //check ip from share internet 
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) { 
            $ip=$_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
         //to check ip is pass from proxy 
            $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
        } else { 
            $ip=$_SERVER['REMOTE_ADDR']; 
        }

       return $ip; 
    }
于 2013-07-31T11:44:54.080 回答