I read that the best way to store IP addresses in a database is to make an Unsigned Int(10) field. How do I convert the IP addresses using PHP? I’ve tried using
$this->ip = long2ip($_SERVER['REMOTE_ADDR']);
But this doesn’t seem to work. I found the way to convert it back to an IP address using
$this->ip = sprintf("%u", ip2long($result['ip']));
How do I convert the IP Address initially? Should I be using PHP for this? Or would it be better to integrate into the MySQL query?
long2ipconverts an integer into the IP format andip2longdoes the inverse.So use
ip2longto convert$_SERVER['REMOTE_ADDR']into an integer for storing it in the database and uselong2ipafter reading the integer from the database: