note 117398 added to function.inet-ntop
| From: | marcus at synchromedia dot co dot uk | Date: | Wed, 03 Jun 2015 16:27:12 +0000 |
| Subject: | note 117398 added to function.inet-ntop | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-202737@lists.php.net to get a copy of this message | ||
PHP's inet_ntop function is not compatible with the binary representation used by MySQL's
INET6_ATON function, assuming you are using the recommended method of storing both IPv4 and IPv6
addresses in a VARBINARY(16) field. You need to convert it like this:
/**
* Convert a MySQL binary v4 (4-byte) or v6 (16-byte) IP address to a printable string.
* @param string $ip A binary string containing an IP address, as returned from MySQL's
INET6_ATON function
* @return string Empty if not valid.
*/
function inet6_ntop($ip) {
$l = strlen($ip);
if ($l == 4 or $l == 16) {
return inet_ntop(pack('A' . $l, $ip));
}
return '';
}
You don't need a function going the other way because MySQL's INET6_NTOA is already
compatible with PHP's inet_pton function.
----
Server IP: 72.52.91.14
Probable Submitter: 77.192.203.13
----
Manual Page -- http://php.net/manual/en/function.inet-ntop.php
Edit -- https://master.php.net/note/edit/117398
Del: integrated -- https://master.php.net/note/delete/117398/integrated
Del: useless -- https://master.php.net/note/delete/117398/useless
Del: bad code -- https://master.php.net/note/delete/117398/bad+code
Del: spam -- https://master.php.net/note/delete/117398/spam
Del: non-english -- https://master.php.net/note/delete/117398/non-english
Del: in docs -- https://master.php.net/note/delete/117398/in+docs
Del: other reasons-- https://master.php.net/note/delete/117398
Reject -- https://master.php.net/note/reject/117398
Search -- https://master.php.net/manage/user-notes.php