How do you store an IP Address (v4 or v6) as an int in any database using php?

会有一股神秘感。 提交于 2019-12-05 16:44:44
Jonathan Graehl

You just need to choose a way of storing 128 bits:

::/96 — This is a 96-bit zero-value prefix originally known as IPv4-compatible addresses. This class of addresses were used to represent IPv4 addresses within an IPv6 transition technology. Such an IPv6 address has its first 96 bits set to zero, while its last 32 bits are the IPv4 address that is represented. The Internet Engineering Task Force (IETF) has deprecated the use of IPv4-compatible addresses with publication RFC 4291. The only remaining use of this address format is to represent an IPv4 address in a table or database with fixed size members that must also be able to store an IPv6 address.

(from http://en.wikipedia.org/wiki/IPv6)

I think two BIGINT UNSIGNED would be a reasonable way to store them, but I guess you could use a binary (byte array).

This question appears to have been answered already at Working with IPv6 Addresses in PHP and How to store IPv6-compatible address in a relational database

LONGMAN

You can use this universal php library: its ip2long and long2ip methods work with both IPv4 and IPv6.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!