|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44892 IP2LONG doesn't handle leading zeroes
Submitted: 2008-05-02 15:20 UTC Modified: 2008-05-02 15:54 UTC
From: anthony dot morris at bankofamerica dot com Assigned:
Status: Not a bug Package: Network related
PHP Version: 5.2.6 OS: Multiple
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: anthony dot morris at bankofamerica dot com
New email:
PHP Version: OS:


 [2008-05-02 15:20 UTC] anthony dot morris at bankofamerica dot com
IP2Long does not handle leading zeroes

Reproduce code:
echo LONG2IP(IP2LONG(''));


echo IP2LONG('');
echo '<br>';
echo IP2LONG('');

Expected result:
Notice the top example should return back the same IP.  The bottom example, should produce the same LONG number.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-02 15:54 UTC]
That is expected.

"All numbers supplied as parts in IPv4 dotted decimal notation may be decimal, octal, or hexadecimal, as specified in the ISO C standard (that is, a leading 0x or 0X implies hexadecimal; otherwise, a leading '0' implies octal; otherwise, the number is interpreted as decimal)."

 [2012-11-07 16:07 UTC] patrick_adrichem at hotmail dot com
What about that should be in octal still 2 right? it returns false.


	$aIpAddresses = array("", "", "");
	foreach ($aIpAddresses as $sIpAddress)
		echo	"Ip: ",$sIpAddress,PHP_EOL,"<br />",
			"Long: ",var_export(ip2long($sIpAddress), true),PHP_EOL,"<br />";

		$aIpAddress = explode(".", $sIpAddress);
		$aIpAddress[0] = substr($aIpAddress[0], -2);
		$sIpAddress = implode(".", $aIpAddress);

		echo "Ip no int: ",var_export(ip2long($sIpAddress), true),PHP_EOL,"<br />";

		$aIpAddress[0] = (int) $aIpAddress[0];
		$sIpAddress = implode(".", $aIpAddress);
		echo "Ip int: ",var_export(ip2long($sIpAddress), true),PHP_EOL,PHP_EOL,"<br /><br />";
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Nov 30 23:03:37 2021 UTC