简体   繁体   中英

reading ipv6 instead of IPv4 in PHP

When my PHP website reads my visitor IP address it shows in the IPv4 format.

However when I go to a third party site (such as https://www.whatismyip.com/ ) my visitor address shows an IPv6 format.

How do I get my PHP website to read/listen for/display the longer IPv6 format of the visitor IP address?

If you get IPv4 addresses in IPv6 format this is probably because your webserver is listening for all requests (IPv4 and IPv6) on a single IPv6 interface. In that case when it reads the peer address it always gets an address in IPv6 format resulting IPv4 addresses to be mapped by prepending ::ffff: to the IPv4 address.

So this is a configuration of your webserver and not of PHP.

If you want to have the IPv4 addresses in that format, you can just prepend the IPv4 address with the fixed string "::ffff:". But why would you want to do this?

Also you will always get the IP address for the IP version you used to connect to the server. So when you wonder why you don't get a real (not a mapped) IPv6 address it's probably because your browser connected to the server using IPv4. Either because your server is not able to listen to IPv6 requests or because its IPv6 connectivity is much slower than the IPv4 connectivity. In that case browsers often fall back to use IPv4 to provide better performance to their user.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM