简体   繁体   English

将 PHP 中的 IPv6 地址转换为 IPv4 地址

[英]Converting IPv6 to IPv4 address in PHP

I need to convert IPv6 addresses to IPv4 addresses.我需要将 IPv6 地址转换为 IPv4 地址。 To do this, I used code from pedmillon's answer to a related question :为此,我使用了pedmillon 对相关问题的回答中的代码:

$ipv6 = $_SERVER['REMOTE_ADDR'];
$ipv4 = hexdec(substr($ipv6, 0, 2)). "." . hexdec(substr($ipv6, 2, 2)). "." . hexdec(substr($ipv6, 5, 2)). "." . hexdec(substr($ipv6, 7, 2));

I tried it in my localhost and::1 gets converted to 0.1.0.0.我在我的本地主机上试过了,::1 被转换为 0.1.0.0。 Is this code correctly working?此代码是否正常工作?

I believe it should be showing 127.0.0.1 instead of 0.1.0.0.我相信它应该显示 127.0.0.1 而不是 0.1.0.0。

IPv4 and IPv6 are two entirely different mutually incompatible.network addressing schemes. IPv4 和 IPv6 是两种完全不同的互不兼容的网络寻址方案。 There is no way to "translate" from one into the other.没有办法从一个“翻译”到另一个。 An IPv4 address does not correspond to a particular IPv6 address or vice versa. IPv4 地址不对应于特定的 IPv6 地址,反之亦然。 The large majority of nodes on the inte.net still use IPv4 addresses exclusively at this point, some small percentage run a dual-stack of IPv4 and IPv6 simultaneously, and a vanishingly small number may be IPv6 exclusively. inte.net 上的绝大多数节点此时仍然只使用 IPv4 地址,一小部分节点同时运行 IPv4 和 IPv6 双栈,极少数可能只使用 IPv6。 IPv4 and IPv6 nodes cannot talk to one another directly. IPv4 和 IPv6 节点无法直接相互通信。 In the long run everyone should be moving to IPv6 exclusively, but that's a long way off.从长远来看,每个人都应该完全转向 IPv6,但这还有很长的路要走。

can you explain how can I use IPv6 address to block countries你能解释一下我如何使用 IPv6 地址来阻止国家吗

The same way you block specific regions using IPv4: get a database that maps IPs to geographic locations.与使用 IPv4 阻止特定区域的方式相同:获取将 IP 映射到地理位置的数据库。 The only difference is that you need to find a database or service which does that for IPv6 addresses.唯一的区别是您需要找到一个为IPv6地址执行此操作的数据库或服务。

As mentioned above - IPV6 and IPv4 are completely different addressing systems, one does not convert from one to another.如上所述 - IPV6 和 IPv4 是完全不同的寻址系统,一个不会从一个转换为另一个。 However if you look at the issue another way you should be able to get the IPV4 address that may be used by the client.但是,如果您以另一种方式看待这个问题,您应该能够获得客户端可能使用的 IPV4 地址。 I used PHP and JavaScript to achieve this.我用 PHP 和 JavaScript 来实现这个。 Here is my sample code - it works for me.这是我的示例代码 - 它适用于我。 Your output will look something like this:您的 output 看起来像这样:

IP Address: xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx IPv4: IP 地址:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx IPv4:

xx.xx.xxx.xxx xx.xx.xxx.xxx

<? // Grab the IP address of the user
$ipaddress = getenv("REMOTE_ADDR") ; echo 'IP Address: '.$ipaddress;

// Check if we need to try and get the IPv4 address
if(filter_var($ipaddress, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
?>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js">
    </script>

      <script>

    /* Add "https://api.ipify.org?format=json" statement
               this will communicate with the ipify servers in
               order to retrieve the IP address $.getJSON will
               load JSON-encoded data from the server using a
               GET HTTP request */

    $.getJSON("https://api.ipify.org?format=json", function(data) {

        // Setting text of element P with id gfg
        $("#gfg").html(data.ip);
    })
    </script>

       <p id="gfg"></p>
<?
}
else {
}

?>

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

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