简体   繁体   English

PHP函数同时显示ipv4 ipv6

[英]PHP function to show both ipv4 ipv6

I am configuring a server to be dual stack, allowing both ipv4 and ipv6. 我将服务器配置为双堆栈,同时允许ipv4和ipv6。 Then, I want to create a php page to show if the client machine is conecting via ipv4, ipv6 or both. 然后,我想创建一个php页面,以显示客户端计算机是否正在通过ipv4,ipv6或两者进行连接。

I have tried $_SERVER['REMOTE_ADDR'] and getenvbyhost("REMOTE_ADDR") as well, but it returns only one or another never both. 我也尝试了$ _SERVER ['REMOTE_ADDR']和getenvbyhost(“ REMOTE_ADDR”),但它只返回一个或另一个,而不返回两者。

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? 是否可以从服务器获取两个IP?

Each incoming request uses either IPv4 or IPv6, but not both at the same time. 每个传入请求都使用IPv4或IPv6,但不能同时使用两者。 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. 执行此操作的最常见方法是创建两个额外的主机名:一个仅包含服务器的IPv4地址,另一个仅包含服务器的IPv6地址。 Then for your reply (assuming HTML) you generate a unique code and you include two hidden images in the page. 然后,为您的答复(假设HTML)生成一个唯一的代码,并在页面中包含两个隐藏的图像。 One using the IPv4-only hostname and one using the IPv6-only hostname. 一种使用仅IPv4主机名,另一种使用仅IPv6主机名。 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. 两者都包含您在URL中生成的唯一代码,因此客户端不会缓存图像,并且您可以看到哪些图像请求属于同一类。

It's quite a hassle, and for most purposes not worth the effort. 这是一件很麻烦的事情,在大多数情况下都不值得付出努力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM