PHP函数使用实例代码分析

本文讲解"PHP函数使用实例代码分析",希望能够解决相关问题。

  • 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 escapeshellarg函数使用的中文问题如何解决:本文讲解"PHP escapeshellarg函数使用的中文问题怎么解决",希望能够解决相关问题。PHP 中命令行调用 escapeshellarg 函数中文问题escapes ...