简体   繁体   English

将IPV6和IPV4存储在MySql中并读取它

[英]Store IPV6 and IPV4 in MySql and also read it

I have a php site. 我有一个php网站。 I want to store an last ip for each user. 我想为每个用户存储最后一个ip。 I want it to work for both IPV4 and IPV6 users. 我希望它适用于IPV4和IPV6用户。 Any idea how? 知道怎么样?

Simply convert them to a binary representation using inet_pton() and later back to a string using inet_ntop() . 只需用它们转换为二进制表示inet_pton()使用,后来回字符串inet_ntop()

Another solution would be storing the IP string without any transformations in the DB. 另一种解决方案是存储IP字符串而不在DB中进行任何转换。 A VARCHAR(39) field will do the job as the highest/longest IPv6 is ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff VARCHAR(39)字段将完成工作,因为最高/最长的IPv6是ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff

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

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