获取客户端IP以int(11)类型存入数据库发现很多127.255.255.255

因为mysql中int(11)最大值是2147483647,用long2IP转换后就是127.255.255.255

把int(11)改为bigint(20),好了这样就解决问题了

客户端IP获取:

<?php
 function get_ip($outType = 'int') {
        if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
            $onlineip = getenv('HTTP_CLIENT_IP');
        } elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
            $onlineip = getenv('HTTP_X_FORWARDED_FOR');
        } elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
            $onlineip = getenv('REMOTE_ADDR');
        } elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
            $onlineip = $_SERVER['REMOTE_ADDR'];
        }

        preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches);
        $onlineip = $onlineipmatches[0] ? $onlineipmatches[0] : false;
        unset($onlineipmatches);

        if ($outType == 'int') {
            $onlineip = ip2long($onlineip);
            if ($onlineip) {
                $onlineip = (int)$onlineip;
            }
        }

        return $onlineip;
    }

关注友儿不迷路

Last modification:June 7th, 2021 at 04:21 pm
如果觉得我的文章对你有用,请随意赞赏