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

后端 未结 7 493
北海茫月
北海茫月 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:08

    What about counting the number of '.' and/or ':' in $_SERVER["REMOTE_ADDR"] ?

    If there is more than 0 ':', and no '.' symbol in $_SERVER["REMOTE_ADDR"], I suppose you can consider you user is connected via IPv6.


    Another solution might be to use the filter extension : there are constants (see the end of the page) that seem to be related to IPv4 and IPv6 :

    FILTER_FLAG_IPV4 (integer)
    Allow only IPv4 address in "validate_ip" filter.

    FILTER_FLAG_IPV6 (integer)
    Allow only IPv6 address in "validate_ip" filter.

提交回复
热议问题