tags:

views:

542

answers:

4

What's the best practise to store IP's with PHP in MySQL database? There's a function called ip2long - but this is just for IPv4. But what about IPv6?

I know a php function that is for IPv6 IP's, but it doesn't work on Windows with PHP < Version 5.3

A: 

On second comment at ip2long function manual you have a function called ip2long6 for IPv6 (and there are more below).

hsz
be aware you need gmp-lib for that function
The Guy Of Doom
A: 

You could just store it as a string in a CHAR I suppose

The Guy Of Doom
+2  A: 

The dotted-decimal IPv4 address can be converted to an integer, with a maximum size of 32 bits. IPv6 addresses are 128 bits. Since 128 bits do not fit in a PHP int, this will be a pain to work with in PHP.

If you just want to connect and use IPv6 addresses, save yourself the trouble and save them as text. If you want to apply netmasks and calculate subnets, then you need to convert them.

Sjoerd
A: 

there's the php function inet_pton, it will turn an ip address string into its binary representation (for both ipv4 and ipv6). you can then store it as binary(16) in your mysql database.

to get a human readable address again, use inet_ntop

knittl