How show IPv4 only with REMOTE_ADDR in PHP?

2019-02-27 01:31发布

问题:

I have a question with variable : $_SERVER['REMOTE_ADDR']. I wish he recovers ONLY IP addresses in IPv4 format . It extracted me a few times in IPv6 format .

How to do?

回答1:

$_SERVER['REMOTE_ADDR'] always contains the address of the visitor. If it contains an IPv6 address then the visitor used IPv6 and there is no IPv4 address. And vice versa of course. These days you have to be able to deal with both.

Some visitors will have only IPv4, some will have only IPv6 and some will have both. The browser decides what is available and what it will use, and that's all you'll see. Note that a browser that has both might even switch between IPv4 and IPv6 between requests if it deems that necessary for good connectivity.



标签: php ip ipv4