Network 函数
在线手册:中文  英文

long2ip

(PHP 4, PHP 5)

long2ipConverts an (IPv4) Internet network address into a string in Internet standard dotted format

说明

string long2ip ( string $proper_address )

The function long2ip() generates an Internet address in dotted format (i.e.: aaa.bbb.ccc.ddd) from the proper address representation.

参数

proper_address

A proper address representation.

返回值

Returns the Internet IP address as a string.

参见


Network 函数
在线手册:中文  英文

用户评论:

klawd at kamundo dot de (2012-01-25 10:34:24)

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));
    }
?>

Anonymous (2011-08-05 09:38:39)

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);
}
?>

php dot net at davidstockton dot com (2008-09-03 22:28:33)

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.

Gabriel Malca (2006-03-17 05:01:35)

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;
        }
    }
?>

randomize at randomizer dot com (2006-01-27 21:51:53)

Random ip address! (4294967295 is 255.255.255.255)
long2ip(rand(0, "4294967295"))

flobee (2005-02-13 14:50:57)

when importing ip-ranges to a mysql database using an INT(10) field - NOTE: that you will get problems when using intval() function!

copied from "cleong" : 02-Oct-2001 02:21
intval() handles overflow differently depending on the type of the argument.
<?php
// intval('10000000000') = 2147483647
// intval(1e10) = 1410065408
?>

Tom Crosley (2003-02-16 13:13:35)

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

易百教程