note 94477 added to function.ip2long
| From: | f dot wiessner at smart-weblications dot net | Date: | Sat, 07 Nov 2009 14:12:01 +0000 |
| Subject: | note 94477 added to function.ip2long | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-162473@lists.php.net to get a copy of this message | ||
Here some working ip2long6 and long2ip6 functions - keep in mind that this needs php gmp-lib:
$ipv6 = "2001:4860:a005::68";
function ip2long6($ipv6) {
$ip_n = inet_pton($ipv6);
$bits = 15; // 16 x 8 bit = 128bit
while ($bits >= 0) {
$bin = sprintf("%08b",(ord($ip_n[$bits])));
$ipv6long = $bin.$ipv6long;
$bits--;
}
return gmp_strval(gmp_init($ipv6long,2),10);
}
function long2ip6($ipv6long) {
$bin = gmp_strval(gmp_init($ipv6long,10),2);
if (strlen($bin) < 128) {
$pad = 128 - strlen($bin);
for ($i = 1; $i <= $pad; $i++) {
$bin = "0".$bin;
}
}
$bits = 0;
while ($bits <= 7) {
$bin_part = substr($bin,($bits*16),16);
$ipv6 .= dechex(bindec($bin_part)).":";
$bits++;
}
// compress
return inet_ntop(inet_pton(substr($ipv6,0,-1)));
}
print $ipv6long = ip2long6($ipv6)."\n";
print $ipv6 = long2ip6($ipv6long)."\n";
outputs:
42541956150894553250710573749450571880
2001:4860:a005::68
----
Server IP: 85.214.154.209
Probable Submitter: 91.204.168.193
----
Manual Page -- http://www.php.net/manual/en/function.ip2long.php
Edit -- https://master.php.net/note/edit/94477
Del: integrated -- https://master.php.net/note/delete/94477/integrated
Del: useless -- https://master.php.net/note/delete/94477/useless
Del: bad code -- https://master.php.net/note/delete/94477/bad+code
Del: spam -- https://master.php.net/note/delete/94477/spam
Del: non-english -- https://master.php.net/note/delete/94477/non-english
Del: in docs -- https://master.php.net/note/delete/94477/in+docs
Del: other reasons-- https://master.php.net/note/delete/94477
Reject -- https://master.php.net/note/reject/94477
Search -- https://master.php.net/manage/user-notes.php