本文へスキップ

[Function] ip2long, long2ip

MintState 2008年11月17日 10:28 閲覧 15012

[Function] ip2long, long2ip

ip2long — Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address

ex)

<?php
$ip = gethostbyname('www.example.com');
$long = ip2long($ip)
if ($long == -1 || $long === FALSE) {
    echo 'Invalid IP, please try again';
} else {
	$out = "The following URLs are equivalent:<br />\n";
	$out .= 'http://www.example.com/, http://' . $ip . '/, and http://' . sprintf("%u", $long) . "/<br />\n";
	echo $out;
}
?>

 

long2ip — Converts an (IPv4) Internet network address into a string in Internet standard dotted format

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

 

$a = ip2long('192.168.0.4));
$b = long2ip($a);
echo $a . "_____________" . $b;

共有 X Facebook LINE QR コード

コメント 0 件

最初のコメントを書いてみましょう。