1

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...

Dusty Bray
  • 31
  • 1
  • 4

2 Answers2

2

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"

Community
  • 1
  • 1
Xavjer
  • 8,838
  • 2
  • 22
  • 42
  • This sounds like an excellent solution. But is there any code for going from a string address to the properly formatted insertion statement in PHP? – Dusty Bray Oct 02 '11 at 19:26
2

It sounds like your main concern is about space. If that's the case, then you could use the fact that IPv4 addresses are (essentially) 32-bit numbers and IPv6 are 128. An IPv4 address can be stored in an INT column, but IPv6 would require two BIGINT columns in MySQL. This is likely to be much more space-efficient than storing strings.

The cost of doing this is that you need to do the conversion from address -> number before inserting the value into the database. That will (slightly) increase CPU load on your web server, so you need to figure out where your bottleneck is going to be and optimise for that.

An added benefit of storing the addresses as numbers is that you can have a very efficient index on the column(s) so looking up an address will be lightning fast. Indexing varchar columns is very expensive.

Cameron Skinner
  • 51,692
  • 2
  • 65
  • 86
  • I don't think he's planning on indexing the addresses, and I'm not clear on what that would accomplish on 2 bigints. But I expect he'll index other data in the row. – dlamblin Oct 11 '11 at 17:31
  • @Dlamblin: The OP stated that he/she wants to look up the last time a particular IP address accessed the site. An index on the address columns will make this lookup very fast. Not having an index will mean a full table scan, which will be very slow. – Cameron Skinner Oct 11 '11 at 22:52
  • yeah I don't think I was thinking straight when I wondered what it accomplishes. – dlamblin Oct 12 '11 at 08:25