问题描述
我阅读了已经问过的一些问题,尽管我没有尝试过,但我发现这很有用
I read a few of the questions already asked, and i found this to be useful, although i have no tried it Working with IPv6 Addresses in PHP
还是说我在MySQL中有一个"bans"表.我将如何存储IPv6地址?该方法必须是通用的,即该字段必须能够包含ipv4或ipv6 addr.这也必须适用于我的用户表中的ip_addr字段.
Still, say i have a 'bans' table in MySQL. How would i go about storing the IPv6 address? The method must be universal, i.e the field must be able to contain either a ipv4 or ipv6 addr. This also must apply to my ip_addr field in my users table.
我通常会检查if(getip == $bans['ip']) { do something }
但是我的getip函数适用于ipv4 afaik,我想知道它是否可以工作.
i would usually check if(getip == $bans['ip']) { do something }
But my getip function is for ipv4 afaik and i wonder if it will work.
我使用的功能是
function getip()
{
if(isset($_SERVER['REMOTE_ADDR']))
{
$ip = $_SERVER['REMOTE_ADDR'];
}
elseif(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
{
if(preg_match_all("#[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}#s", $_SERVER['HTTP_X_FORWARDED_FOR'], $addresses))
{
foreach($addresses[0] as $key => $val)
{
if(!preg_match("#^(10|172\.16|192\.168)\.#", $val))
{
$ip = $val;
break;
}
}
}
}
if(!isset($ip))
{
if(isset($_SERVER['HTTP_CLIENT_IP']))
{
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
else
{
$ip = '';
}
}
$ip = preg_replace("#([^.0-9 ]*)#", "", $ip);
return $ip;
}
推荐答案
您可以将其存储在VARCHAR(40)的简单列中.
考虑到样本IPv6的最大值为40个字节:
You can store it in a simple column of VARCHAR(40).
Considering a sample IPv6 max is 40 byte:
2001:0DB8:0000:0000:0000:0000:1428:57ab
该链接也将能够包含IPv4
That coulmn will be able to contain IPv4 too
这篇关于在PHP中检测IPv6地址并将其正确存储在MySQL中.如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!