PHP function to show both ipv4 ipv6

爱⌒轻易说出口 提交于 2020-01-05 02:49:11

问题


I am configuring a server to be dual stack, allowing both ipv4 and ipv6. Then, I want to create a php page to show if the client machine is conecting via ipv4, ipv6 or both.

I have tried $_SERVER['REMOTE_ADDR'] and getenvbyhost("REMOTE_ADDR") as well, but it returns only one or another never both.

I also tried the below code

 function isIPv6($ip) {

   if(filter_var($ip, FILTER_VALIDATE_IP)) {

     if(filter_var($ip, FILTER_FLAG_IPV6)) {
       //It is IPv6 indeed.
     } else {
       //It is IPv4
     }

   } else {
     // Not a valid IP
   }
}

Is it possible to get both ips from the server?


回答1:


Each incoming request uses either IPv4 or IPv6, but not both at the same time. If you want to know both then you will need to trigger multiple connections to your server using different protocols.

The most common way to do this is to create two extra hostnames: one with only the IPv4 address of your server and one with only the IPv6 address of your server. Then for your reply (assuming HTML) you generate a unique code and you include two hidden images in the page. One using the IPv4-only hostname and one using the IPv6-only hostname. Both containing the unique code you generated in the URL so the client doesn't cache the image and you can see which image requests belong together.

It's quite a hassle, and for most purposes not worth the effort.



来源:https://stackoverflow.com/questions/33214331/php-function-to-show-both-ipv4-ipv6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!