How to find both IPv6 and IPv4 with PHP?

105 Views Asked by At

When I look up my ip address on a website, it offers both my IPv6 and IPv4 address. When I use a PHP-script it only return the IPv6 address (both $_SERVER['REMOTE_ADDR'] and getenv('REMOTE_ADDR')).

How do these websites get my IPv4 address?

I have looked into all the $_SERVER variables but cannot find it.

1

There are 1 best solutions below

0
Sander Steffann On

You’ll need to run the website on three different host names:

  • the normal one which resolves to both IPv4 and IPv6
  • one that only resolves to IPv4
  • one that only resolves to IPv6

On the last two, put a script that returns or stores the client’s IP address. Make the client visiting the website on the normal host name connect to the other two and include an identifier so the requests can be linked together in some way, and collect the resulting data.