Don’t store IP addresses in a database as a string like (192.168.10.10). That is just the human readable form of that number. You want to store it in the database as an integer.
MySQL has built in functions to handle converting a dot-notation ip address to an integer equivalent.
` mysql> SELECT INET_ATON(‘192.168.0.10’) AS ipn; +————+ | ipn | +————+ | 3232235530 | +————+
mysql> SELECT INET_NTOA(3232235530) AS ipa; +————–+ | ipa | +————–+ | 192.168.0.10 | +————–+ ` But, I needed this type of functionality in PHP itself. Long story short, I’m using Doctrine to handle the relationship that PHP has to the database and I need to convert the human readable dot-notation IP address to the integer style (unsigned integer that is).
PHP’s built in function ip2long comes close, but there is a little bit more to work the magic.
$ip = long2ip(ip2long(“192.168.1.10”)); print sprintf("%u", ip2long($ip));
is used to convert the signed integer to an unsigned one.
Ready to be inserted into the DB.
Did this help you out? It took me a few days to piece together all this information together, I hope this saves you some time (who knows, maybe the future me will be thankful I wrote this down). Let me know your thoughts. [email protected]