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;
}

Subscribe to the comments through RSS Feed

Leave a Reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image