使用PHP存储在MySQL数据库中的IP地址 [英] IP address storing in MySQL database using PHP
问题描述
mysql 中 IP 地址的正确字段类型是什么?使用 PHP 存储它的正确方法是什么?
what is the right field type for IP address in mysql? and what is the right way of storing it using PHP?
推荐答案
本教程 可能对你有帮助.
保存 IPv4 地址的最有效方法是使用 INT 字段(不是您可能期望的 VARCHAR).您可以使用 PHP 的 ip2long
转换它们,然后使用 MySQL 的 INET_NTOA
函数或 PHP 的 <一个 href="http://www.php.net/manual/en/function.long2ip.php" rel="noreferrer">long2ip
函数.
The most efficient way of saving IPv4 addresses is with an INT field (not VARCHAR as you might expect). You convert them using PHP's ip2long
and back using either MySQL's INET_NTOA
function or PHP's long2ip
function.
如果您需要存储 IPv6,则需要使用 BINARY 字段和 PHP 的 inet_pton
函数.
If you need to store IPv6, you'll want to use a BINARY field instead and PHP's inet_pton
function.
这篇关于使用PHP存储在MySQL数据库中的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!