How to get the local IP address using PHP?

1

I have an application in which I want, like test, to get the local IP address from my PC. I did a little research and I saw that it is possible to rescue it using the following code:

$ip = $_SERVER["REMOTE_ADDR"]

But it did not work for me because I'm using PHP's versão 5.6.26 fault. So I ask, is it possible to get the local IP address through a PHP page? How can I do this?

    
asked by anonymous 14.02.2017 / 23:37

1 answer

3

It is not possible for any external server to access the "local IP" (IP of an internal network), I got to comment something about it here:

The maximum that an external server (regardless of the language it uses) will achieve is the IP of the Internet Service Provider (ISP), that is, if 100 computers share the same " internet "will all have the same" IP "if you use $_SERVER["REMOTE_ADDR"] , because the IP is actually" connection ".

There is no practical way around this, the example they cited in the comments does not work "natively":

$_SERVER['HTTP_X_REAL_IP'];

It is not a PHP variable but a variable generated by the headers of a "client" (browser, bot, etc.) via http, for example:

GET /pagina.php HTTP/1.1
User-Agent: MyBotFooBar1.0
X-Real-Ip: 10.0.0.105

In other words, all "variables" that start with HTTP_ are generated from the headers and this is not a safe way to verify authenticity, since headers can be easily manipulated.

Rarely will any browser send this header X-Real-Ip , you would have to configure them to do this using an add-on / extension or a proxy, a very large job that may not be worth the effort. p>

If your goal is to use this for some "authentication type" I recommend thinking of measures like a token-enabled mobile app (or something like that) / p>

  

Note:

     

The tip about ipconfig only works if PHP is on the machine that wants to get the address:

     

exec('ipconfig', $array);

     

Using other commands will also not work, such as arp , because PHP would need to be on the same local network as the computers, which I believe is not your goal.

    
19.02.2017 / 03:29