note 78935 deleted from function.ip2long by cmb
| From: | cmb@php.net | Date: | Sun, 13 Oct 2019 07:47:29 +0000 |
| Subject: | note 78935 deleted from function.ip2long by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212607@lists.php.net to get a copy of this message | ||
Note Submitter: sealbreaker at email dot com
----
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