php获取客户端IP,国家,省份,地区,ISP等信息
php获取客户端IP,国家,省份,地区,ISP等信息,首先获取获取客户端的公网ip,开始本地搭建的环境,本地访问获取到的是127.0.0.1,然后用138的接口可以实现curl模拟客户端访问 http://city.ip138.com/ip2city.asp 可看到真实客户端的ip。
<?php header(‘Content-Type:text/html;Charset=utf-8‘);</code> //获取ip方法 -- 如果获取不精准可以用第三方获取 http://city.ip138.com/ip2city.asp function GetIP(){ if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) $ip = getenv("HTTP_CLIENT_IP"); else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) $ip = getenv("HTTP_X_FORWARDED_FOR"); else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) $ip = getenv("REMOTE_ADDR"); else if (isset($_SERVER[‘REMOTE_ADDR‘]) && $_SERVER[‘REMOTE_ADDR‘] && strcasecmp($_SERVER[‘REMOTE_ADDR‘], "unknown")) $ip = $_SERVER[‘REMOTE_ADDR‘]; else $ip = "unknow"; return($ip); } //新浪接口,获取ip所在国家,地域。 function GetIpLookup($ip = ‘‘){ if(empty($ip)){ $ip = GetIp(); } $res = @file_get_contents(‘http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&&ip=‘ . $ip); if(empty($res)){ return false; } $jsonMatches = array(); preg_match(‘#\{.+?\}#‘, $res, $jsonMatches); if(!isset($jsonMatches[0])){ return false; } $json = json_decode($jsonMatches[0], true); if(isset($json[‘ret‘]) && $json[‘ret‘] == 1){ $json[‘ip‘] = $ip; unset($json[‘ret‘]); }else{ return false; } return $json; } $ipInfos = GetIpLookup(); //IP地址 print_r($ipInfos); //打印数组 //测试url: http://127.0.0.1/ip.php
相关推荐
zyyjay 2020-11-09
xuebingnan 2020-11-05
samtrue 2020-11-22
stefan0 2020-11-22
yifangs 2020-10-13
songshijiazuaa 2020-09-24
hebiwtc 2020-09-18
天步 2020-09-17
83911535 2020-11-13
whatsyourname 2020-11-13
zhouyuqi 2020-11-10
Noneyes 2020-11-10
mathchao 2020-10-28
王志龙 2020-10-28
wwwsurfphpseocom 2020-10-28
diskingchuan 2020-10-23
savorTheFlavor 2020-10-23