ThinkPHP\Common\funcitons.php下有一个get_client_ip()能够获取ip地址
但是有时候不够准确
找到了下面一段来
function get_client_ip($type = 0) {
$type = $type ? 1 : 0;
static $ip = NULL;
if ($ip !== NULL) return $ip[$type];
if($_SERVER[\'HTTP_X_REAL_IP\']){//nginx 代理模式下,获取客户端真实IP
$ip=$_SERVER[\'HTTP_X_REAL_IP\'];
}elseif (isset($_SERVER[\'HTTP_CLIENT_IP\'])) {//客户端的ip
$ip = $_SERVER[\'HTTP_CLIENT_IP\'];
}elseif (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[\'REMOTE_ADDR\'])) {
$ip = $_SERVER[\'REMOTE_ADDR\'];//浏览当前页面的用户计算机的ip地址
}else{
$ip=$_SERVER[\'REMOTE_ADDR\'];
}
// IP地址合法验证
$long = sprintf("%u",ip2long($ip));
$ip = $long ? array($ip, $long) : array(\'0.0.0.0\', 0);
return $ip[$type];
}
现在我们就能用了
$ip=get_client_ip();
现在我们要根据ip地址获取用户的位置
Thinkphp支持IP定位功能,需要使用扩展类库Org\Net\IpLocation,并且要配合IP地址库文件一起使用
$Ip = new \Org\Net\IpLocation(\'UTFWry.dat\'); // 实例化类 参数表示IP地址库文件 $area = $Ip->getlocation(); // 获取某个IP地址所在的位置
这个UTFWry.dat需要我们下载
http://pan.baidu.com/s/1eSz1GTO 密码: 7q4v,解压后放到ThinkPHP\Library\Org\Net目录下
现在我把$area信息输出下
array (size=5) \'ip\' => string \'172.xxx.1.228\' (length=12) \'beginip\' => string \'172.xxx.0.0\' (length=10) \'endip\' => string \'172.xxx.255.255\' (length=14) \'country\' => string \'局域网\' (length=9) \'area\' => string \'对方和您在同一内部网\' (length=30)
因为手机连的电脑wifi。。。。。。。。