4

I am trying to get ip address of my localhost via a php function,

$user_ip = $_SERVER['REMOTE_ADDR'];
echo $user_ip;

currently it is returning **::1** I want to return ipv4 **(127.0.0.1)** instead of ipv6 **(::1)**. It maybe a silly question I have searched alot but does't find any help.

Biswas
  • 598
  • 2
  • 16
  • 34
  • http://superuser.com/a/668008 – Alive to die - Anant Sep 28 '16 at 04:50
  • 1
    exact duplicate question http://stackoverflow.com/questions/10517371/ip-address-of-the-machine-in-php-gives-1-but-why – JYoThI Sep 28 '16 at 05:05
  • $_SERVER['REMOTE_ADDR'] is the IP address of the client. $_SERVER['SERVER_ADDR'] is the IP address of the server. – JYoThI Sep 28 '16 at 05:20
  • @JYoThI thank you very much. Your link saved me. This is it what I am looking for. [detail link](http://stackoverflow.com/questions/3699454/should-a-mamp-return-1-as-ip-on-localhost/13649642#13649642) – Masab Jamal Sep 28 '16 at 05:27
  • google it the queries first with right key words . it will give solution if exists @MasabJamal – JYoThI Sep 28 '16 at 05:30

1 Answers1

3

Windows like most OS's now are both old IPV4 and new IPV6 aware.

Both have whats called a loopback address

IPV4 is 127.0.0.1
IPV6 is ::1

As WAMPServer and your browser are both on the same PC your remote ip address will be the your local ip address i.e. the loopback address

WAMPServer's Apache is configured to also be IPV4 and IPV6 aware.

Now its the browser that decides to use either IPV4 or IPV6, and I dont know what rules it uses to decide. So in your case for this connection it has decided to use the IPV6 network.

Therefore $_SERVER['REMOTE_ADDR'] is reporting ::1

If you want to prove this you can force the browser to use the IPV4 network by entering the url as http::/127.0.0.1 and you will see 127.0.0.1 reported as the remote ip address.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149