php获取用户IP

function getIp() {
    static $realip = NULL;
 
    if ($realip !== NULL) {
        return $realip;
    }
 
    if (getenv( 'HTTP_X_FORWARDED_FOR')) {
        $realip = getenv( 'HTTP_X_FORWARDED_FOR');
    } elseif (getenv( 'HTTP_CLIENT_IP')) {
        $realip = getenv( 'HTTP_CLIENT_IP');
    } else {
        $realip = getenv( 'REMOTE_ADDR');
    }
 
    preg_match("/[\d\.]{7,15}/", $realip, $onlineip);
    $realip = !empty($onlineip[0]) ? $onlineip[0] : '0.0.0.0';
 
    return $realip;
}
 

 


$data['ip'] = sprintf( '%u',ip2long(getIp()) );
 
ip2long ——把(IPv4)的字符串地址转换为int类型
sprintf—————把有符号的数字强制转换为无符号
 
反向 long2ip
posted @ 2016-10-10 17:27  samuel#  阅读(1170)  评论(0)    收藏  举报