如何使用php将IP地址(v4或v6)作为int存储在任何数据库中? [英] How do you store an IP Address (v4 or v6) as an int in any database using php?

查看:903
本文介绍了如何使用php将IP地址(v4或v6)作为int存储在任何数据库中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以最紧凑的方式存储IP地址,搜索不是一个问题。这还需要在Windows,Linux和Mac上使用任何DB(MySQL,SQLite,Postgre等)兼容。



感谢 ip2long()和long2ip()函数在PHP我可以将IP4地址转换为小int字段而不是varchar(15)或任何。问题是,我们处于IP6的过渡阶段,这意味着大多数现有的最佳实践都失败了 - 包括那些不能与IP6协同工作的功能。此外,如INET_ATON()和INET_NTOA()这样的数据库特定函数在这里不是选项。



那么如何将IP地址存储为INT(或任何其他紧凑格式)可以在任何系统上工作?



编辑:
线程王朝指出使用inet_ntop()和inet_ptop()打包和解压缩IP4或IP6。问题是,他们只工作在Linux / Mac的PHP +5.1(这不是一个太大的问题)和窗口与+5.3(这是)。



如果这不是一个问题,他们的替代varbinary(16)字段是推荐在每个数据库?

::

::

::

::

::

96 - 这是一个96位零值
前缀,最初称为
IPv4兼容地址。这个类
的地址用于表示IPv6
转换技术中的
IPv4地址。这样的IPv6
地址的前96位被设置为
零,而其最后32位是被表示的
IPv4地址。
互联网工程任务组(IETF)
已弃用使用
IPv4兼容地址与
出版物RFC 4291.唯一的
剩余使用此地址格式
表示固定大小为

表或数据库中的IPv4地址,还必须能够
存储IPv6地址。


(来自 http://en.wikipedia .org / wiki / IPv6



我认为两个BIGINT UNSIGNED是一个合理的存储方式,但我想你可以使用二进制数组)。



此问题似乎已经在在PHP中使用IPv6地址如何在关系数据库中存储IPv6兼容地址


I need to store IP address in the most compact way possible, searching is not a concern. This also needs to be compatible on Windows, Linux, and Mac using any DB (MySQL, SQLite, Postgre, etc).

Thanks to the ip2long() and long2ip() functions in PHP I can convert a IP4 address to a small int field rather than a varchar(15) or whatever. The problem is that we are in a transition stage to IP6 which means that most of the existing best-practices are failing - including those functions which don't work with IP6. Further more, DB specific functions like INET_ATON() and INET_NTOA() aren't options here.

So how do you store IP address as INT's (or any other compact format) in a way that will work on any system?

EDIT: The thread Wrang-wrang pointed mentions the use of inet_ntop() and inet_ptop() to pack and unpack either IP4 or IP6. The problem is that they only work on Linux/Mac with PHP +5.1 (which isn't a too big of a problem) and on windows with +5.3 (which is).

If that was not a problem are their replacements for the varbinary(16) field that was recommended in each database?

解决方案

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

这篇关于如何使用php将IP地址(v4或v6)作为int存储在任何数据库中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆