php.net |  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
 [2008-05-02 15:20 UTC] anthony dot morris at bankofamerica dot com
Description:
------------
IP2Long does not handle leading zeroes

Reproduce code:
---------------
<?PHP
echo LONG2IP(IP2LONG('192.068.068.068'));
?>


OR

<?PHP
echo IP2LONG('192.068.068.068');
echo '<br>';
echo IP2LONG('192.68.68.68');
?>


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


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-05-02 15:54 UTC] felipe@php.net
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)."

- http://www.opengroup.org/onlinepubs/000095399/functions/inet_addr.html
 [2012-11-07 16:07 UTC] patrick_adrichem at hotmail dot com
What about 02.123.123.123? that should be in octal still 2 right? it returns false.

<?php

	$aIpAddresses = array("2.123.123.123", "102.123.123.123", "202.123.123.123");
	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-2019 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 10:01:22 2019 UTC