代码贴 - 查询本机ip

2023-11-17 18:25:22 浏览数 (1)

修一个小bug 凌梦云kangle脚本时不时获取本机ip失效 然后就自己写了这么一个api

凌梦云kangle一键脚本

代码语言:javascript复制
yum -y install wget;wget http://kangle.mxftp.cn/start;sh start

下面贴接口代码

代码语言:javascript复制
<?php
/*
 * @Author: Qicloud
 * @Title: API
 * @Project: 查询本机ip
 * @QQ: 66547997
 * @Date: 2020-12-24 22:55:39
 * @LastEditTime: 2020-12-24 22:56:02
 * @你不懂我的热爱,又怎知我不明白
 */
function getIP()
{
    $ip = $_SERVER['REMOTE_ADDR'];
    if (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && preg_match('/^([0-9]{1,3}.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CF_CONNECTING_IP'])) {
        $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
    } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && preg_match('/^([0-9]{1,3}.){3}[0-9]{1,3}$/', $_SERVER['HTTP_CLIENT_IP'])) {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#d{1,3}.d{1,3}.d{1,3}.d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
        foreach ($matches[0] as $xip) {
            if (!preg_match('#^(10|172.16|192.168).#', $xip)) {
                $ip = $xip;
                break;
            }
        }
    }
    return $ip;
}
echo getIP();

0 人点赞