PHP函数同时显示ipv4 ipv6 [英] PHP function to show both ipv4 ipv6

查看:133
本文介绍了PHP函数同时显示ipv4 ipv6的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将服务器配置为双协议栈,同时允许ipv4和ipv6.然后,我要创建一个php页面,以显示客户端计算机是通过ipv4,ipv6还是同时通过ipv6连接.

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.

我也尝试过$ _SERVER ['REMOTE_ADDR']和getenvbyhost("REMOTE_ADDR"),但它只会返回一个或另一个,而不会同时返回.

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

我也尝试了以下代码

 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
   }
}

是否可以从服务器获取两个IP?

Is it possible to get both ips from the server?

推荐答案

每个传入请求都使用IPv4或IPv6,但不能同时使用两者.如果您想两者都知道,那么您将需要使用不同的协议触发与服务器的多个连接.

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.

最常见的方法是创建两个额外的主机名:一个仅包含服务器的IPv4地址,另一个仅包含服务器的IPv6地址.然后,为您的答复(假设HTML)生成一个唯一的代码,并在页面中包含两个隐藏的图像.一种使用仅IPv4主机名,另一种使用仅IPv6主机名.两者都包含您在URL中生成的唯一代码,因此客户端不会缓存图像,并且您可以看到哪些图像请求属于同一内容.

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.

这篇关于PHP函数同时显示ipv4 ipv6的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆