How to know which version of the Internet Protocol (IP) a client is using when connecting to my server?

后端 未结 7 472
北海茫月
北海茫月 2020-12-04 19:38

I want to check via php if someone connects to my site via IPv4 or IPv6.

The client address can be found in $_SERVER[\"REMOTE_ADDR\"] but

相关标签:
7条回答
  • 2020-12-04 20:23

    Since the highest voted answer has a rather significant problem, I'm going to share my own.

    This returns true if an address which appears to be IPv6 is passed in, and false if an address which appears to be IPv4 (or IPv4-mapped IPv6) is passed in. The actual addresses are not further validated; use filter_var() if you need to validate them.

    function is_ipv6($address) {
        $ipv4_mapped_ipv6 = strpos($address, "::ffff:");
        return (strpos($address, ":") !== FALSE) &&
               ($ipv4_mapped_ipv6 === FALSE || $ipv4_mapped_ipv6 != 0);
    }
    
    0 讨论(0)
提交回复
热议问题