我想从中获取IP地址$_SERVER['REMOTE_ADDR']和其他一些$_SERVER变量,哪种数据类型是正确的呢?
$_SERVER['REMOTE_ADDR']
$_SERVER
是VARCHAR(n)吗
VARCHAR(n)
由于IPv4地址的长度为4个字节,因此可以使用正好为4个字节的INT(UNSIGNED):
INT
UNSIGNED
`ipv4` INT UNSIGNED
而INET_ATON与INET_NTOA它们转换:
INET_ATON
INET_NTOA
INSERT INTO `table` (`ipv4`) VALUES (INET_ATON("127.0.0.1")); SELECT INET_NTOA(`ipv4`) FROM `table`;
对于IPv6地址,您可以BINARY改用:
BINARY
`ipv6` BINARY(16)
并使用PHPinet_pton和inet_ntop进行转换:
inet_pton
inet_ntop
'INSERT INTO `table` (`ipv6`) VALUES ("'.mysqli_real_escape_string(inet_pton('2001:4860:a005::68')).'")' 'SELECT `ipv6` FROM `table`' $ipv6 = inet_pton($row['ipv6']);