Detect IPv6 in PHP?
I am currently detecting the user's IPv4 address and using IP blocks with the v4. However I want to capture the IPv6 and have block lists for this too, so the question is: Is it the same logic to get IPv6 as IPv4 or something different in PHP? I am just not sure if every device has a IPv4 and IPv6 or is it either one of these an开发者_JAVA百科d the system will auto detect what format it is and store it?
Apache reports the REMOTE_ADDR
to PHP. If Apache listens on a v6 interface it is going to be the v6 address.
It is easy to differenciate. IPv4 addresses will always have a fullstop .
character and IPv6 addresses will always contain a colon :
.
When creating blocklists (or whitelists) you should be careful handling the v6 addresses. They can be shortened by using two consecutive colons ::
. There is no guarantee that your OS will use the shorter or the full form, so your script must handle that. Check Wikipedia's IPv6 page for details.
精彩评论