首页 > 编程语言 >PHP转换ip地址

PHP转换ip地址

时间:2024-09-21 15:36:03浏览次数:1  
标签:bin gmp return ip 地址 res PHP bits

ip转换

/**
 * ip转换整型
 * @param int|string|null $ip ip地址
 * @return int|string|null
 */
function my_ip2long($ip)
{
    $res = false;
    if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
        $res = sprintf('%u', ip2long($ip));
    } else if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
        $ip_n     = inet_pton($ip);
        $bits     = 15;
        $ipv6long = '';
        while ($bits >= 0) {
            $bin      = sprintf('%08b', (ord($ip_n[$bits])));
            $ipv6long = $bin . $ipv6long;
            $bits--;
        }
        $res = gmp_strval(gmp_init($ipv6long, 2), 10);
    }
    if ($res) {
        return $res;
    } else {
        return 0;
    }
}

/**
 * 整型转换ipv6
 * @param null|int|string $ip ip地址
 * @return string
 */
function my_long2ip($ip)
{
    if (empty($ip)) {
        return '';
    }
    $bin = gmp_strval(gmp_init($ip, 10), 2);
    if (strlen($bin) < 128) {
        $pad = 128 - strlen($bin);
        for ($i = 1; $i <= $pad; $i++) {
            $bin = '0' . $bin;
        }
    }
    $bits = 0;
    $ipv6 = '';
    while ($bits <= 7) {
        $bin_part = substr($bin, ($bits * 16), 16);
        $ipv6     .= dechex(bindec($bin_part)) . ':';
        $bits++;
    }
    $res = inet_ntop(inet_pton(substr($ipv6, 0, -1)));
    if ($res) {
        // ipv4一定包含3个.号
        if (substr_count($res, '.') === 3) {
            $res = str_replace(':', '', $res);
        }
        return $res;
    } else {
        return '0';
    }
}

  

标签:bin,gmp,return,ip,地址,res,PHP,bits
From: https://www.cnblogs.com/shemmor/p/18424083

相关文章

  • PHP获取指定日期n天前后的日期列表
    /***获取指定日期n天前后的日期列表*@paramstring$date日期*@paramint$day_num天数*@paramstring$cate类型*@paramstring$format格式*@returnarray*/functionget_date_ab_list(string$date,int$day_num,string$cate='b',string$form......
  • PHP美化打印输出并结束程序执行
    话不多说,直接附上代码if(!function_exists('dd')){/***@notes:打印输出*@parammixed$vars*@returnvoid*@author:lavender*@time:2024/7/159:33*/functiondd(...$vars){/*ob_start();var_......
  • DOM【JavaScript】
    在JavaScript中,DOM(DocumentObjectModel:文档对象模型)是web页面的编程接口,用于表示和操作HTML和XML文档。它将文档结构化为一个树形结构,允许开发者通过JavaScript访问和修改网页的内容、结构和样式。以下是一些关于DOM的关键概念:1.结构DOM树结构是以节点为单位组......
  • IP地址+VLSM
    IP地址全局范围内定位设备----IP地址(32位)192.168.1.111——点分十进制——IPV4地址表示格式4组十进制IPV4地址范围:0.0.0.0——255.255.255.255IANA机构——五大类A类:第一个8位组的第一位一定取值为0   子网掩码/8=255.0.0.0最小:0.0.0.0最大:127.255.255.25......
  • nginx: 按ip地址限流
    一,以固定的速度提供服务语法:例子limit_req_zone$binary_remote_addrzone=test:10mrate=2r/s;server{location/{limit_reqzone=test;}}语法:imit_req_zone用于设置限流和共享内存区域的参数,格式为:limit_req_zonekeyzonerate。key: 定义限流对......
  • ReactJS + Stripe:试用期天数未按预期工作
    :试用期天数未按预期工作一、问题描述(一)预期的试用期天数该应用程序使用ReactJS和Stripe进行支付处理。试用期天数应设置为14天,但实际试用期天数并未按预期工作。用户在注册后,试用期仅持续了7天,而不是预期的14天。这导致用户在试用期结束前就被要求支付订阅费用,给用户带来......
  • PHP抽奖算法
    一、初始化奖品id奖品的idpid奖品的自定义idtype奖品类型,1、虚拟奖品2、实物奖品3、礼包码待扩充name奖品名称total奖品总数chance获奖概率/抽奖基数10000daynum每日数量限制pay充值限制$prize=[['id'=>1,'pid'=>11,'type'=>1,'name'=>'典藏......
  • WPF Canvas show custom control with ellipse filled with image and text,peridoica
    //customcontrol<UserControlx:Class="WpfApp389.ElpImageTbk"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"......
  • Thinkphp8安装topthink/think-captcha验证码的和使用方法
    ThinkPHP8默认没有验证码,安装验证码可以使用composer来安装验证码一、安装验证码执行composer安装验证码composerrequiretopthink/think-captcha二、使用方法1、在目录app\middleware.php中开启session\think\middleware\SessionInit::class2、配置验证码安装好验证......
  • 您在wp-config.php文件中提供的数据库用户名和密码可能不正确 的解决办法
    设置步骤复制配置文件在你的 htdocs 中的WordPress根目录下找到 wp-config-sample.php 文件。将 wp-config-sample.php 文件复制并重命名为 wp-config.php。编辑 wp-config.php 文件使用Notepad++或其他文本编辑器打开 wp-config.php 文件。修改以下......