note 78935 added to function.ip2long
| From: | sealbreaker at email dot com | Date: | Sat, 03 Nov 2007 08:47:54 +0000 |
| Subject: | note 78935 added to function.ip2long | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131959@lists.php.net to get a copy of this message | ||
As a note : if you are using (PHP 4, PHP 5) and are looking to get the integer value of an IP
address, i have found that the following works flawlessly for converting to and from IPv4 and
it's integer equivalent. I must give credit elsewhere for this portion of the code
(ip2long($ip) & 0x7FFFFFFF) + 0x80000000). I looked but was unable to find the comment where it
was included.
$ip = "127.0.0.0"; // as an example
$integer_ip = (substr($ip, 0, 3) > 127) ? ((ip2long($ip) & 0x7FFFFFFF) + 0x80000000) :
ip2long($ip);
echo $integer_ip; // integer value of IP address
echo long2ip($integer_ip); // convert to an IPv4 formatted address
-----------------------
Results are as follows:
-----------------------
2130706432
127.0.0.0
-----------------------
255.255.255.255 (converts to) 4294967295 (and back to) 255.255.255.255
209.65.0.0 (converts to) 3510697984 (and back to) 209.65.0.0
12.0.0.0 (converts to) 201326592 (and back to) 12.0.0.0
1.0.0.0 (converts to) 16777216 (and back to) 1.0.0.0
----
Server IP: 64.71.164.2
Probable Submitter: 68.54.37.105
----
Manual Page -- http://www.php.net/manual/en/function.ip2long.php
Edit -- https://master.php.net/note/edit/78935
Del: integrated -- https://master.php.net/note/delete/78935/integrated
Del: useless -- https://master.php.net/note/delete/78935/useless
Del: bad code -- https://master.php.net/note/delete/78935/bad+code
Del: spam -- https://master.php.net/note/delete/78935/spam
Del: non-english -- https://master.php.net/note/delete/78935/non-english
Del: in docs -- https://master.php.net/note/delete/78935/in+docs
Del: other reasons-- https://master.php.net/note/delete/78935
Reject -- https://master.php.net/note/reject/78935
Search -- https://master.php.net/manage/user-notes.php