存储/检索IPv4& MySQL中的IPv6地址 [英] storing/retrieving IPv4 & IPv6 addresses in MySQL

查看:80
本文介绍了存储/检索IPv4& MySQL中的IPv6地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,我对此主题了解不多,但是我要寻找的是一种快速轻松的解决方案,用于在MySQL中唯一表示任何IP地址(v4/v6),因此我可以轻松地检索上一次(如果有)是一台特定的计算机已经访问了我的网站.

我不需要对地址做任何计算,只需检索即可,因此任何唯一的表示形式都可以.我确实计划存储其中的很多(尚无估计),所以空间可能会成为问题.

我已经看到了许多用于存储IP地址的解决方案,但是目前尚不清楚哪种版本都适用. MySQL的内置INET_ATON似乎不支持IPv6. PHP的inet_pton似乎很有希望,但需要先了解该地址的格式.我也不确定它的用法(MySQL字段类型和通过PHP编写插入语句).我已经看到varchar(39)用来将IPv6地址表示为字符串,并且我喜欢这种解决方案在某种程度上与服务器配置无关.但是,我对磁盘空间有些不安.这种方法对于$ _SERVER ['HTTP_CLIENT_IP']可能输出的所有地址是否足够?

我没有一个明显的通用解决方案感到惊讶.我认为这是一个非常常见的任务.对于这个问题,我犹豫不决,并希望继续进行我的项目.快速简便的解决方案不合理吗?

非常感谢您的指导...

解决方案

我会这样做:从那里引用: >

Sorry I don't know much about this subject, but all I'm looking for is a quick-and-easy solution for uniquely representing any IP address (v4/v6) in MySQL so I can easily retrieve the last time (if any) that a particular computer has visited my site.

I don't need to do any computations on the addresses, just retrieval, so any unique representation should be ok. I do plan on storing a lot of these (don't have an estimate yet), so space may become a concern.

I've seen many solutions for storing IP addresses, but it's unclear which work for both versions. MySQL's built-in INET_ATON doesn't seem to support IPv6. PHP's inet_pton seems promising but requires prior knowledge of the address's format. I'm also unsure about its usage (MySQL field type and writing the insertion statement via PHP). I've seen varchar(39) used to represent IPv6 addresses as strings, and I like that this solution is somewhat independent of server configuration; however, I'm a little uneasy about disk space. Would this approach be sufficient for all addresses that $_SERVER['HTTP_CLIENT_IP'] might output?

I'm a little surprised there isn't an obvious generic solution. I assumed this was a very common task. I'm having indecision about this single issue, and would like to move on with my project. Is a quick-and-easy solution unreasonable?

Thanks very much for any guidance...

解决方案

I would go with this: citat from there : How to store IPv6-compatible address in a relational database "Final decision taken: 2xBIGINT if the second bigint is NULL, then it means IPv4"

这篇关于存储/检索IPv4& MySQL中的IPv6地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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