美文网首页
php获取用户的ip,内网用户返回1外网返回0

php获取用户的ip,内网用户返回1外网返回0

作者: 此生只愿醉太白 | 来源:发表于2018-03-07 10:12 被阅读0次

几个Ip用于测试(txt文件夹位于/data/webdocs/pchi/tools/iptest.txt";
10.80.1.%

10.100.2.%

10.100.1.%

120.120.12.242

219.136.82.7

实现代码:

<?php 

get_real_ip();

function get_real_ip()

{

    if (getenv('HTTP_CLIENT_IP')) {

        $get_ip = getenv('HTTP_CLIENT_IP');

}

    elseif (getenv('HTTP_X_FORWARDED_FOR')) {

        $get_ip = getenv('HTTP_X_FORWARDED_FOR');

}

    elseif (getenv('REMOTE_ADDR')) {

        $get_ip = getenv('REMOTE_ADDR');

}

    else {

        $get_ip = $_SERVER['REMOTE_ADDR'];

}

//ip txt的路径

    $file_path = "/data/webdocs/pchi/tools/iptest.txt";

    $ips = file_get_contents($file_path);//将整个文件内容读入到一个字符串中

    $str_encoding = mb_convert_encoding($ips, 'UTF-8', 'UTF-8,GBK,GB2312,BIG5');//转换字符集(编码)

    $ip_arr = explode("\r\n", $str_encoding);//转换成数组

    $ip_arr_result = array_filter($ip_arr); //去除空值

    for ($i = 0; $i < count($ip_arr_result); $i++) {

        if (in_array($get_ip, $ip_arr_result)) {

            echo json_encode(1);

            break;

        }else {

            $reult_get_ip = substr($get_ip, 0, strrpos($get_ip, '.'));

            $reult_get_ip_1=$reult_get_ip.".%";

            if (in_array($reult_get_ip_1, $ip_arr_result)) {

                echo json_encode(1);

                break;

            }else{

                echo json_encode(0);

                break;

}

}

}

}

?>

相关文章

网友评论

      本文标题:php获取用户的ip,内网用户返回1外网返回0

      本文链接:https://www.haomeiwen.com/subject/aszdfftx.html