您的当前位置:首页>全部文章>文章详情

如何判断IP是国内IP还是国外Ip

发表于:2023-10-14 00:33:06浏览:957次TAG: #国际Ip #屏蔽国内IP

直接上干货
如何判断是国内IP还是国际IP地址

function check_is_china_ip($ip)
{
    $ip_addr = explode('.', $ip);

    if (count($ip_addr) < 4)
        return false;

    $a1 = (int)$ip_addr[0];
    $a2 = (int)$ip_addr[1];
    $a3 = (int)$ip_addr[2];
    $a4 = (int)$ip_addr[3];

    $s_china = file_get_contents(storage_path('ips.txt'));

    $tb_china = json_decode($s_china, 1);

    unset($s_china);

    if (!isset($tb_china[$a1][$a2]) || count($tb_china[$a1][$a2]) == 0)
        return false;

    $a = $a3 * 256 + $a4;

    foreach ($tb_china[$a1][$a2] as $d) {
        if ($a >= $d['s'] && $a <= $d['e']) {
            return true;
        }
    }

    return false;
}

上面的函数中 ips.txt的下载地址
下载地址

0.094546s