PHP 02 Feb 2018 22:02:37
PHP Expand IPv4 to IPv6
Simple function for turning IPv6 and IPv4 into a blob that’s suitable for SQL type binary(16). This maps IPv4 to the IPv6 address space reserved for them.
function ip2binary($ip) { $rv = inet_pton($ip); if (strlen($rv) == 4) { // https://tools.ietf.org/html/rfc2765#section-2.1 $rv = inet_pton('::ffff:'.$ip); } return $rv; }