How to get the local IP address using PHP?

Asked

Viewed 3,076 times

1

I have an application in which I want, related test, to get the IP address location of my PC. I did a brief search and saw that it is possible to rescue it using the following code:

$ip = $_SERVER["REMOTE_ADDR"]

But it didn’t work for me that I’m using the versão 5.6.26 PHP. So I ask, is it possible to get the IP address place through a PHP page? How can I do this?

  • i use 5.2.17 and it works. Put in your code phpinfo(); and see all the information in PHP Variables

  • $_SERVER['HTTP_X_REAL_IP'];

  • 1

    See this: http://answall.com/questions/143004/print-de-array-php/143010#143010

1 answer

4


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

The most that an external server (regardless of the language you use) will achieve is the ISP IP (Internet Service Provider - Internet Service Provider), ie if 100 computers share the same "internet" all will have the "same IP" if use $_SERVER["REMOTE_ADDR"], because in fact the IP is the "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

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

Rarely will any browser send this the 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.

If your goal is to use this for some "type of authentication" recommend to think of measures such as a mobile app with token (or something similar) and dispense with the use of Ips.

Note:

The tip about the ipconfig only works if PHP is on the machine you want to get the address from:

exec('ipconfig', $array);

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.