ip2long

Converts a string containing an (IPv4) Internet Protocol dotted address into a long integer

Syntax

ip2long ( string $ip_address ) : int

Parameters

ip_address

A standard format address.

Return

Returns the long integer or FALSE if ip_address is invalid.

Examples

1

<?

$ip = gethostbyname('php.net');
$out = "The following URLs are equivalent:<br>\n";
$out .= 'http://php.net/' . "<br>\n";
$out .= 'http://' . $ip . '/' . "<br>\n";
$out .= 'http://' . sprintf("%u", ip2long($ip)) . '/';
echo $out;

?>
The following URLs are equivalent:<br>
http://php.net/<br>
http://185.85.0.29/<br>
http://3109355549/

2

<?

$ip   = gethostbyname('php.net');
$long = ip2long($ip);

if ($long == -1 || $long === FALSE) {
    echo 'Invalid IP, please try again';
} else {
    echo $ip   . "<br>\n";
    echo $long . "<br>\n"; // - on 32-bit systems, due to integer overflow
    printf("%u\n", ip2long($ip));
}

?>
185.85.0.29<br>
3109355549<br>
3109355549

PHP

Home Menu