I know that this question has been asked quite a few times, but I could not find a final solution; so here:
How do you store IP (both IPv4 and IPv6), using best practices, in a database without knowing the database? This is the case for database abstraction purposes such as PHP PDO.
In a minor note, I have PHP 5.2.17, since I need PEAR for Windows.
People suggested storing it as varbinary (16) and using the mysql functions inet6_ntop and inet6_pton to pass IP addresses back and forth as strings; e.g. Extending MySQL 5 using IPv6 features . In PHP inet_pton () and inet_ntop () function can convert IPv4 and IPv6 in a binary format as the ThiefMaster in this question suggests , but it is unclear how to transfer binary content in SQL INSERT / UPDATE line (the php-function only available php 5.3.0 on the windows , although you can redesign them). I really like what Jake did and his results regarding integer representations of IP addresses in DBand this may come in handy in some distant, unforeseen future if I have to implement this in my database, but then I’m not sure of the cross-compatibility of the database for abstracting the database using PHP PDO. This post seems to give a close answer on storing binary values, but can't binary injection into potential danger strings be immobilized? Also, if you follow this route, how many DBs can convert varbinary (16) / int (128 bits) to a representative IP if any developer wants to do some quick searches?
It seems to me that the easiest way is to insert ip string as-is into varchar (45) . But how could those who want to follow a difficult route, in PHP (the reverse design like djmaze (AT) dragonflycms (.) Org or like MagicalTux on FF dot st offer) using the functions inet_ntop () and inet_pton (), store and retrieve IPv6 as binary files? Can someone give an example of PDO from <?php $strIP = "2001:4860:b002::68"; ?>using INSERT and SELECT statements?
As you can see, I did my research, but I don’t understand the final good practice of this IPv6.
source
share