CakeFest 2024: The Official CakePHP Conference

long2ip

(PHP 4, PHP 5, PHP 7, PHP 8)

long2ip長整数表現のインターネットアドレスを (IPv4) インターネット標準ドット表記に変換する

説明

long2ip(int $ip): string|false

関数long2ip() は、長整数表現のアドレスからドット表記 (例:aaa.bbb.ccc.ddd)のインターネットアドレスを生成します。

パラメータ

ip

長整数で表した、正しい形式のアドレス。

戻り値

インターネットの IP アドレスを表す文字列を返します。 失敗した場合に false を返します

変更履歴

バージョン 説明
7.1.0 ip の型が string から int に変更されました。

注意

注意:

32ビットアーキテクチャでは、IPアドレスの整数表現を string から int にキャストしても PHP_INT_MAX を超える数値については正しい結果を得られません。

参考

  • ip2long() - ドット表記の (IPv4) IP アドレスを、長整数表現に変換する

add a note

User Contributed Notes 7 notes

up
5
steve at computurn dot com
5 years ago
For a 32bit safe long2ip, which can accept string or signed integer input, try:

function safelong2ip($long) {
$binStr = sprintf("%032s", decbin((float)$long));
if (strlen($binStr) != 32) {
throw new Exception("Invalid IPv4 subnet!");
}

$ipArr = [];
for ($i = 0; $i < 4; ++$i) {
$ipArr[] = bindec(substr($binStr, $i*8, 8));
}

return implode('.', $ipArr);
}
up
9
Gabriel Malca
18 years ago
If the function doesn't exist:

<?
if (!function_exists("long2ip")) {
function long2ip($long) {
// Valid range: 0.0.0.0 -> 255.255.255.255
if ($long < 0 || $long > 4294967295) return false;
$ip = "";
for ($i=3;$i>=0;$i--) {
$ip .= (int)($long / pow(256,$i));
$long -= (int)($long / pow(256,$i))*pow(256,$i);
if ($i>0) $ip .= ".";
}
return $ip;
}
}
?>
up
1
Rolf
2 years ago
an Anonymous wrote several years ago that you should cast the parameter to float to make sure that you get the right result on 32-bit machines. That probably never worked.

But the idea is on a good track: to convert a bitstring into an ip, use

$ip = long2ip((int)bindec($bits));

This works on 32- and 64-bit machines.
up
1
Tom Crosley
21 years ago
I wanted to be able to pass an IP address in a URL always as an unsigned int. I then converted it back as shown below:

$ip_addr = "192.168.100.25"; // example IP address that converts to neg #

$s32int = ip2long($ip_addr);

$us32str = sprintf("%u",$s32int); // convert to unsigned string

// display orig IP address, signed 32 bit version, unsigned 32 bit ver,
// finally converted back to IP addr

printf("%s ,%d, %s, %s", $ip_addr, $s32int, $us32str,
long2ip(-(4294967296-$us32str)));

// tested on Linux/Apache PHP 4.1.2
up
0
klawd at kamundo dot de
12 years ago
Use these two functions to convert from and to numbers compatible to MySQLs INET_ATON and INET_NTOA
<?php
function convertIpToString($ip)
{
$long = 4294967295 - ($ip - 1);
return
long2ip(-$long);
}
function
convertIpToLong($ip)
{
return
sprintf("%u", ip2long($ip));
}
?>
up
-2
Anonymous
12 years ago
If you're running a 32 bit machine you can get wrong IPs. To prevent from this just cast to float e.g.

<?php
long2Ip32bit
($ip){
return
long2ip((float)$ip);
}
?>
up
-11
php dot net at davidstockton dot com
15 years ago
Beware when processing values that are invalid, you may get values that are different based on the OS. For instance:

$ip = long2ip(pow(2,32)+1024);

On windows you get 255.255.255.255. On linux it's 0.0.4.0.

So it seems it would be important to make sure the long you're converting is in the correct range.
To Top