您的位置:首页 > 技术中心 > PHP教程 >

分享PHP函数使用小工具(附代码示例)

时间:2023-01-21 08:30

本篇文章给大家带来了关于PHP的相关知识,其中主要介绍了PHP怎么检测IP相关信息的,以及PHP如何获取HTTP、vue包等,感兴趣的朋友,下面一起来看一下吧,希望对大家有帮助。

  • PHP检测IP是否内网地址、保留地址

/**
 * @param string $ip 被检测的IP
 * @return bool 是否内网或者保留IP
 */
public function isInternalIp($ip)
{
    $ip = ip2long($ip);
    if (!$ip) {
        //非法IP,直接算true吧
        return true;
    }
    $net_a = ip2long('10.255.255.255') >> 24; //A类网预留ip的网络地
    $net_b = ip2long('172.31.255.255') >> 20; //B类网预留ip的网络地址
    $net_c = ip2long('192.168.255.255') >> 16; //C类网预留ip的网络地址
    $net_local127 = ip2long('127.255.255.255') >> 24; //127保留地址
    $net_local169 = ip2long('169.254.255.255') >> 16; //169保留地址
    return $ip >> 24 === $net_a || $ip >> 20 === $net_b || $ip >> 16 === $net_c || $net_local127 === $ip >> 24 || $net_local169 === $ip >> 16;
}

这个是我网上找来的,具体地址我忘了,然后自己加了保留地址的检测

  • PHP获取HTTP包流量整个HTTP请求包流量

    public function http()
    {
        $row = $_SERVER['REQUEST_URI'] . "\r\r";
        $header = getallheaders();
        foreach ($header as $k => $v) {
            $row .= $k . ': ' . $v . "\r";
        }
        $row .= "\r\r" . file_get_contents("php://input");
        return $row;
    }
  • vue差量更新包-PHP处理

public function test()
{
    $config = json_decode(file_get_contents('vueconfig.json'), true); //配置目录,初次使用要先建立配置
    $path = 'D:\\web\\project\\vue\\dist\\static\\'; // 打包的静态地址
    foreach ($config as  $dir => $type) {
        foreach (scandir($path . $dir) as $fkey => $fva) {
            if ($fva == '.' || $fva == '..') {
                continue;
            } else {
                if (in_array($fva, $type)) {
                    //没有更新就删除该文件
                    unlink($path . $dir . '\\' . $fva);
                } else {
                    echo '新增文件:' . $path . $dir . '\\' . $fva . "<br>";
                    //有更新就把新文件加入到配置表里记录
                    $config[$dir][$fkey] = $fva;
                }
            }
        }
    }
    //更新配置表
    file_put_contents('vueconfig.json', json_encode($config));
}

直接运行即可删除没有改变的文件,保留更新的文件,实现差量更新

推荐学习:《PHP视频教程》

以上就是分享PHP函数使用小工具(附代码示例)的详细内容,更多请关注gxlsystem.com其它相关文章!

本类排行

今日推荐

热门手游