-
-
/**
- * 获取客户端ip
- * @return [string] [description]
- */
- function getclientip() {
- $ip = null;
- if (isset($_server['http_x_forwarded_for'])) {
- $arr = explode(',', $_server['http_x_forwarded_for']);
- $pos = array_search('unknown',$arr);
- if(false !== $pos) unset($arr[$pos]);
- $ip = trim($arr[0]);
- }elseif (isset($_server['http_client_ip'])) {
- $ip = $_server['http_client_ip'];
- }elseif (isset($_server['remote_addr'])) {
- $ip = $_server['remote_addr'];
- }
- // ip地址合法验证
- $ip = (false !== ip2long($ip)) ? $ip : '0.0.0.0';
- return $ip;
- }
/**
- * 获取在线ip
- * @return string
- */
- function getonlineip($format=0) {
- global $s_global;
- if(empty($s_global['onlineip'])) {
- 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);
- $s_global['onlineip'] = $onlineipmatches[0] ? $onlineipmatches[0] : 'unknown';
- }
if($format) {
- $ips = explode('.', $s_global['onlineip']);
- for($i=0;$i$ips[$i] = intval($ips[$i]);
- }
- return sprintf('%03d%03d%03d', $ips[0], $ips[1], $ips[2]);
- } else {
- return $s_global['onlineip'];
- }
- }
-
复制代码
php获取远程客户端真实ip地址
php在内网机器获取公网IP的方法
php读取纯真ip数据库的简单例子
PHP获取本机的局域网IP地址方法
PHP获取局域网中计算机名、IP地址与MAC地址
PHP获取IP地址的多种方法
PHP通过IP获取地理位置的代码
PHP获取指定的IP网段信息
php IP获取城市API(纯真IP数据库)
php获取真实ip地址的实例分享
探讨:PHP获取域名及域名IP地址的方法
php通过IP获取地理位置的实例参考
|