note 54679 added to function.ip2long
| From: | ParaginaSilviu at osu1 dot php dot net | Date: | Tue, 12 Jul 2005 15:41:03 +0000 |
| Subject: | note 54679 added to function.ip2long | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-91820@lists.php.net to get a copy of this message | ||
Note: ip2long and long2ip do not function as the c linux functions inet_addr and inet_ntoa. They
store the long in reverse byte order (little endian vs big endian i guess).
For example you send 10.0.0.1 to inet_ntoa you take the long from the result and you pass it to
long2ip you get 1.0.0.10. You won't run into this issue unless you use a database both from c
linux programs and php scripts.
My first ideea was to reverse the long, but unfortunatley the long was stored as unsigned and i got
a lot of problems doing calculations with it (some operations would work well others not; probably
it was stored as float i do not know for sure...)
So my solution was
function str_rev_ip($str)
{
$ar=explode(".",$str);
return "$ar[3].$ar[2].$ar[1].$ar[0]";
}
and i take the result from inet_ntoa and parse it as str_rev_ip(long2ip($var))
----
Manual Page -- http://www.php.net/manual/en/function.ip2long.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+54679
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54679&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54679&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54679&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54679&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54679&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54679&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+54679&report=yes
Search -- http://master.php.net/manage/user-notes.php