phpip查询具体地理位置
A. php根据ip地址查地区
自己以前做过一个程序 根据discuz里面的ip查询改的 
/**
 * ip地址所属地区计算
 * 修改自 discuz 
 * 使用dicuz tinyipdata数据文件
 * 将一些英文提示修改为汉字
 * $is_simple true的话显示到市 false显示到网通电信等等
 ******/
function convertip($ip,$is_simple=true,$ipfile='include/data/ip.dat') {
 $return = '';
 if( !file_exists($ipfile) ) $ipfile = '../'.$ipfile;
 
 if(preg_match("/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/", $ip)) {
  $iparray = explode('.', $ip);
  if($iparray[0] == 10 || $iparray[0] == 127 || ($iparray[0] == 192 && $iparray[1] == 168) || ($iparray[0] == 172 && ($iparray[1] >= 16 && $iparray[1] <= 31))) {
   return '局域网';
  } elseif($iparray[0] > 255 || $iparray[1] > 255 || $iparray[2] > 255 || $iparray[3] > 255) {
   return 'ERR';
  } elseif($is_simple) {
   return change_simply_area(convertip_tiny($ip, $ipfile));
  }
  else {
   return convertip_tiny($ip, $ipfile);
  }
 }
}
/**
 * 从ip文件得到ip所属地区
 * 
 * 过滤掉了具体的位置(如 网通/电信/**网吧) 基本到市
 ***/
function convertip_tiny($ip, $ipdatafile) {
 static $fp = NULL, $offset = array(), $index = NULL;
 $ipdot = explode('.', $ip);
 $ip    = pack('N', ip2long($ip));
 $ipdot[0] = (int)$ipdot[0];
 $ipdot[1] = (int)$ipdot[1];
 if($fp === NULL && $fp = @fopen($ipdatafile, 'rb')) {
  $offset = unpack('Nlen', fread($fp, 4));
  $index  = fread($fp, $offset['len'] - 4);
 } elseif($fp == FALSE) {
  return  '- Invalid IP data file';
 }
 $length = $offset['len'] - 1028;
 $start  = unpack('Vlen', $index[$ipdot[0] * 4] . $index[$ipdot[0] * 4 + 1] . $index[$ipdot[0] * 4 + 2] . $index[$ipdot[0] * 4 + 3]);
 for ($start = $start['len'] * 8 + 1024; $start < $length; $start += 8) {
  if ($index{$start} . $index{$start + 1} . $index{$start + 2} . $index{$start + 3} >= $ip) {
   $index_offset = unpack('Vlen', $index{$start + 4} . $index{$start + 5} . $index{$start + 6} . "\x0");
   $index_length = unpack('Clen', $index{$start + 7});
   break;
  }
 }
 fseek($fp, $offset['len'] + $index_offset['len'] - 1024);
 if($index_length['len']) {
  return  mb_convert_encoding(fread($fp, $index_length['len']),'utf-8','gb2312'); //将读出的gb编码数据转成utf-8并返回
 } else {
  return '未知';
 }
}
function change_simply_area($area) {
 $tmp = explode(' ',$area); //过滤掉一些具体信息
 return $tmp[0];
}
里面那个ipfile你可以去下载一个discuz 在 ipdata目录里面有wry.dat的文件就是了  其实这个就是网上用的最多的那个纯真版数据库 很多显示ip的qq用的也是那个  
这两个函数的原型参考 discuz 里面 include\misc.func.php 
共同学习进步 :)
B. php用IP查询归属地
class ip_location
{
 function init()
 {
  $this->wrydat = 'ip_area.dat';
  
  $this->fp = fopen($this->wrydat, 'rb');
  $this->getipnumber();
  $this->getwryversion();
  
  $this -> REDIRECT_MODE_0 = 0;
  $this -> REDIRECT_MODE_1 = 1;
  $this -> REDIRECT_MODE_2 = 2;
 }
 
 function get($str)
 {
  return $this->$str;
 }
 
 function set($str,$val)
 {
  $this->$str = $val;
 }
 
 function getbyte($length,$offset=null)
 {
  !is_null($offset) && fseek($this->fp, $offset, SEEK_SET);
  
  return fread($this->fp, $length);
 }
 
 function packip($ip)
 {
  return pack('N', intval(ip2long($ip)));
 }
 
 function getlong($length=4, $offset=null)
 {
  $chr=null;
  for($c=0;$length%4!=0&&$c<(4-$length%4);$c++)
  {
   $chr .= chr(0);
  }
  $var = unpack( 'Vlong', $this->getbyte($length, $offset).$chr);
  return $var['long'];
 }
 
 function getwryversion()
 {
  $length = preg_match("/coral/i",$this->wrydat)?26:30;
  $this->wrydat_version = $this->getbyte($length, $this->firstip-$length);
 }
 
 function getipnumber()
 {
  $this->firstip = $this->getlong();
  $this->lastip = $this->getlong();
  $this->ipnumber = ($this->lastip-$this->firstip)/7+1;
 }
 
 function getstring($data='', $offset=NULL)
 {
  $char = $this->getbyte(1,$offset);
  while(ord($char) > 0)
  {
   $data .= $char;
   $char = $this->getbyte(1);
  }
  return $data;
 }
 
 function iplocaltion($ip)
 {
  $ip = $this->packip($ip);
  $low = 0;
  $high = $this->ipnumber-1;
  $ipposition = $this->lastip;
  while($low <= $high)
  {
   $t = floor(($low+$high)/2);
   if($ip < strrev($this->getbyte(4,$this->firstip+$t*7)))
    $high = $t - 1;
   else
   {
    if($ip > strrev($this->getbyte(4,$this->getlong(3))))
     $low = $t + 1;
    else
    {
     $ipposition = $this->firstip+$t*7;
     break;
    }
   }
  }
  return $ipposition;
 }
 
 function getarea()
 {
  $b = $this->getbyte(1);
  switch(ord($b))
  {
   case $this -> REDIRECT_MODE_0 :
    return '';
    break;
   case $this -> REDIRECT_MODE_1:
   case $this -> REDIRECT_MODE_2:
    return $this->getstring('',$this->getlong(3));
    break;
   default:
    return $this->getstring($b);
    break;
  }
 }
 
 function getiplocation($ip)
 {
  $ippos = $this->iplocaltion($ip);
  $this->ip_range_begin = long2ip($this->getlong(4,$ippos));
  $this->ip_range_end = long2ip($this->getlong(4,$this->getlong(3)));
  $b = $this->getbyte(1);
  switch(ord($b))
  {
   case $this -> REDIRECT_MODE_1:
    $b = $this->getbyte(1,$this->getlong(3));
    if(ord($b) == $this -> REDIRECT_MODE_2)
    {
     $countryoffset = $this->getlong(3);
     $this->area = $this->getarea();
     $this->country = $this->getstring('',$countryoffset);
    }
    else
    {
     $this->country = $this->getstring($b);
     $this->area = $this->getarea();
    }
    break;
   case $this -> REDIRECT_MODE_2:
    $countryoffset = $this->getlong(3);
    $this->area = $this->getarea();
    $this->country = $this->getstring('',$countryoffset);
    break;
   default:
    $this->country = $this->getstring($b);
    $this->area   = $this->getarea();
    break;
  }
 }
}
---------------------------------------------------------------
调用方法:
$iploca = new ip_location;
 $iploca -> init();
 $iploca -> getiplocation($ip);
 
 $area['country'] = str_replace(array('CZ88.NET'), '', $iploca -> get('country'));
 $area['area'] = str_replace(array('CZ88.NET'), '', $iploca -> get('area'));
 
 $area['country']=='' && $area['country']='未知';
 $area['area']=='' && $area['area']='未知';
 return $area;
C. php 怎么通过ip来获取所在位置
我的代码,需要的朋友可以直接复制使用:
<?php  
header('Content-Type:text/html;Charset=utf-8');  
function GetIp(){  
    $realip = '';  
    $unknown = 'unknown';  
    if (isset($_SERVER)){  
        if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR']) && strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'], $unknown)){  
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);  
            foreach($arr as $ip){  
                $ip = trim($ip);  
                if ($ip != 'unknown'){  
                    $realip = $ip;  
                    break;  
                }  
            }  
        }else if(isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP']) && strcasecmp($_SERVER['HTTP_CLIENT_IP'], $unknown)){  
            $realip = $_SERVER['HTTP_CLIENT_IP'];  
        }else if(isset($_SERVER['REMOTE_ADDR']) && !empty($_SERVER['REMOTE_ADDR']) && strcasecmp($_SERVER['REMOTE_ADDR'], $unknown)){  
            $realip = $_SERVER['REMOTE_ADDR'];  
        }else{  
            $realip = $unknown;  
        }  
    }else{  
        if(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), $unknown)){  
            $realip = getenv("HTTP_X_FORWARDED_FOR");  
        }else if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), $unknown)){  
            $realip = getenv("HTTP_CLIENT_IP");  
        }else if(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), $unknown)){  
            $realip = getenv("REMOTE_ADDR");  
        }else{  
            $realip = $unknown;  
        }  
    }  
    $realip = preg_match("/[\d\.]{7,15}/", $realip, $matches) ? $matches[0] : $unknown;  
    return $realip;  
}  
  
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('123.125.114.144'); //.com IP地址  
var_mp($ipInfos);
D. 关于PHP获取IP地理位置
参考我这个回答,确实能用的
http://..com/question/97006048.html
E. php获取了ip地址,用php怎么获取ip的地理位置请大虾赐教!
通过纯真QQip地址库文件查询IP地理位置
2009-02-26 14:11
一、下载纯真IP地址库文件QQWry.Dat (网上有很多,并且可以定期升级)
二、创建类文件 IPLocation.php ,将下面代码直接拷贝到php文件中
<?php
/**
* IP 地理位置查询类
*
* @author 马秉尧
* @version 1.5
* @right 2005 CoolCode.CN
*/
class IpLocation 
{
    /**
     * QQWry.Dat文件指针(使用以前珊瑚虫QQ的IP)
     *
     * @var resource
     */
    var $fp;
    
    /**
     * 第一条IP记录的偏移地址
     *
     * @var int
     */
    var $firstip;
    
    /**
     * 最后一条IP记录的偏移地址
     *
     * @var int
     */
    var $lastip;
    
    /**
     * IP记录的总条数(不包含版本信息记录)
     *
     * @var int
     */
    var $totalip;
    
    /**
     * 返回读取的长整型数
     *
     * @access private
     * @return int
     */
    function getlong() 
    {
        //将读取的little-endian编码的4个字节转化为长整型数
        $result = unpack('Vlong', fread($this->fp, 4));
        return $result['long'];
    }
    
    /**
     * 返回读取的3个字节的长整型数
     *
     * @access private
     * @return int
     */
    function getlong3() 
    {
        //将读取的little-endian编码的3个字节转化为长整型数
        $result = unpack('Vlong', fread($this->fp, 3).chr(0));
        return $result['long'];
    }
    
    /**
     * 返回压缩后可进行比较的IP地址
     *
     * @access private
     * @param string $ip
     * @return string
     */
    function packip($ip) 
    {
        // 将IP地址转化为长整型数,如果在PHP5中,IP地址错误,则返回False,
        // 这时intval将Flase转化为整数-1,之后压缩成big-endian编码的字符串
        return pack('N', intval(ip2long($ip)));//intaval 获取变量的整数值
    }
    
    /**
     * 返回读取的字符串
     *
     * @access private
     * @param string $data
     * @return string
     */
    function getstring($data = "") 
    {
        $char = fread($this->fp, 1);
        while (ord($char) > 0) 
        {        // 字符串按照C格式保存,以\0结束 ord()得到字符的ASCII码
            $data .= $char;                // 将读取的字符连接到给定字符串之后
            $char = fread($this->fp, 1);
        }
        return $data;
    }
    
    /**
     * 返回地区信息
     *
     * @access private
     * @return string
     */
    function getarea() 
    {
        $byte = fread($this->fp, 1);    // 标志字节
        switch (ord($byte)) {
            case 0:                        // 没有区域信息
                $area = "";
                break;
            case 1:
            case 2:                        // 标志字节为1或2,表示区域信息被重定向
                fseek($this->fp, $this->getlong3());
                $area = $this->getstring();
                break;
            default:                    // 否则,表示区域信息没有被重定向
                $area = $this->getstring($byte);
                break;
        }
        return $area;
    }
    
    /**
     * 根据所给 IP 地址或域名返回所在地区信息
     *
     * @access public
     * @param string $ip
     * @return array
     */
    function getlocation($ip) 
    {
        if (!$this->fp) return null;            // 如果数据文件没有被正确打开,则直接返回空
        $location['ip'] = gethostbyname($ip);     // 将输入的域名转化为IP地址
        $ip = $this->packip($location['ip']);    // 将输入的IP地址转化为可比较的IP地址
                                                // 不合法的IP地址会被转化为255.255.255.255
        // 对分搜索
        $l = 0;                            // 搜索的下边界
        $u = $this->totalip;            // 搜索的上边界
        $findip = $this->lastip;        // 如果没有找到就返回最后一条IP记录(QQWry.Dat的版本信息)
        while ($l <= $u) 
        {                // 当上边界小于下边界时,查找失败
            $i = floor(($l + $u) / 2);    // 计算近似中间记录
            fseek($this->fp, $this->firstip + $i * 7);
            $beginip = strrev(fread($this->fp, 4));        // 获取中间记录的开始IP地址
            // strrev函数在这里的作用是将little-endian的压缩IP地址转化为big-endian的格式
            // 以便用于比较,后面相同。
            if ($ip < $beginip) 
            {        // 用户的IP小于中间记录的开始IP地址时
                $u = $i - 1;            // 将搜索的上边界修改为中间记录减一
            }
            else 
            {
                fseek($this->fp, $this->getlong3());
                $endip = strrev(fread($this->fp, 4));    // 获取中间记录的结束IP地址
                if ($ip > $endip) 
                {        // 用户的IP大于中间记录的结束IP地址时
                    $l = $i + 1;        // 将搜索的下边界修改为中间记录加一
                }
                else 
                {                    // 用户的IP在中间记录的IP范围内时
                    $findip = $this->firstip + $i * 7;
                    break;                // 则表示找到结果,退出循环
                }
            }
        }
    
        //获取查找到的IP地理位置信息
        fseek($this->fp, $findip);
        $location['beginip'] = long2ip($this->getlong());    // 用户IP所在范围的开始地址
        $offset = $this->getlong3();
        fseek($this->fp, $offset);
        $location['endip'] = long2ip($this->getlong());        // 用户IP所在范围的结束地址
        $byte = fread($this->fp, 1);    // 标志字节
        switch (ord($byte)) 
        {
            case 1:                        // 标志字节为1,表示国家和区域信息都被同时重定向
                $countryOffset = $this->getlong3();            // 重定向地址
                fseek($this->fp, $countryOffset);
                $byte = fread($this->fp, 1);    // 标志字节
                switch (ord($byte)) 
                {
                    case 2:                // 标志字节为2,表示国家信息又被重定向
                        fseek($this->fp, $this->getlong3());
                        $location['country'] = $this->getstring();
                        fseek($this->fp, $countryOffset + 4);
                        $location['area'] = $this->getarea();
                        break;
                    default:            // 否则,表示国家信息没有被重定向
                        $location['country'] = $this->getstring($byte);
                        $location['area'] = $this->getarea();
                        break;
                }
                break;
            case 2:                        // 标志字节为2,表示国家信息被重定向
                fseek($this->fp, $this->getlong3());
                $location['country'] = $this->getstring();
                fseek($this->fp, $offset + 8);
                $location['area'] = $this->getarea();
                break;
            default:                    // 否则,表示国家信息没有被重定向
                $location['country'] = $this->getstring($byte);
                $location['area'] = $this->getarea();
                break;
        }
        if ($location['country'] == " CZ88.NET") 
        {    // CZ88.NET表示没有有效信息
            $location['country'] = "未知";
        }
        if ($location['area'] == " CZ88.NET") 
        {
            $location['area'] = "";
        }
        return $location;
    }
    
    /**
     * 构造函数,打开 QQWry.Dat 文件并初始化类中的信息
     *
     * @param string $filename
     * @return IpLocation
     */
    function IpLocation($filename = "QQWry.Dat") 
    {
        if (($this->fp = @fopen($filename, 'rb')) !== false) 
        {
            $this->firstip = $this->getlong();
            $this->lastip = $this->getlong();
            $this->totalip = ($this->lastip - $this->firstip) / 7;
            //注册析构函数,使其在程序执行结束时执行
            register_shutdown_function(array(&$this, '_IpLocation'));
        }
    }
    
    /**
     * 析构函数,用于在页面执行结束后自动关闭打开的文件。
     *
     */
    function _IpLocation() 
    {
        fclose($this->fp);
    }
}
?>
三、程序部分
require_once("includes/IPLocation.php") ;                               //这里引用刚创建的类文件
$ipLocation = new IpLocation("includes/QQWry.Dat") ;           //这里引用ip地址库
$ips = $ipLocation->getlocation($userIP) ;                               //$ips 就是所得到的物理地址,$userIP是传入的IP ,例如(202.106.12.23)
来源:http://hi..com/yang_bd/blog/item/7b44f830b8764e1eebc4afd1.html 
下面是一个实例,获取IP并且检索出该IP地理位置
<?
//获取IP
error_reporting (E_ERROR | E_WARNING | E_PARSE);
if($HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"]) 
{                                              
       $ip = $HTTP_SERVER_VARS["HTTP_X_FORWARDED_FOR"];
      
}                                              
elseif($HTTP_SERVER_VARS["HTTP_CLIENT_IP"])    
{                                              
       $ip = $HTTP_SERVER_VARS["HTTP_CLIENT_IP"];
         
}                                              
elseif ($HTTP_SERVER_VARS["REMOTE_ADDR"])       
{                                              
       $ip = $HTTP_SERVER_VARS["REMOTE_ADDR"];  
            
}                                              
elseif (getenv("HTTP_X_FORWARDED_FOR"))          
{                                              
       $ip = getenv("HTTP_X_FORWARDED_FOR");  
               
}                                              
elseif (getenv("HTTP_CLIENT_IP"))                
{                                              
       $ip = getenv("HTTP_CLIENT_IP");  
                     
}          
                                 
elseif (getenv("REMOTE_ADDR"))                   
{                                              
       $ip = getenv("REMOTE_ADDR"); 
                        
}       
                                    
else                                           
{                                              
       $ip = "Unknown";    
                                 
}                                              
require_once("includes/IPLocation.php") ;                               //这里引用刚创建的类文件
$ipLocation = new IpLocation("includes/QQWry.Dat") ;           //这里引用ip地址库
$ips = $ipLocation->getlocation($ip) ;                               //$ips 就是所得到的物理地址
var_mp($ips);
?>
效果如下(我使用本地测试):
array(5) { ["ip"]=> string(9) "127.0.0.1" ["beginip"]=> string(9) "127.0.0.1" ["endip"]=> string(9) "127.0.0.1" ["country"]=> string(8) "本机地址" ["area"]=> string(0) "" }
F. 如何进行PHP查询ip所在地
<?php
/**
* 根据IP地址取得地理位置
*/
function get_ip_arr()
{
$ip=file_get_contents("http://fw.qq.com/ipaddress");
preg_match_all("/\"(.*)\"/",$ip,$arr);
return $arr;
}
//返回一个数组,包括地区、IP等信息,自行获取即可。
?>
G. 怎么查找ip地址对应的实际地理位置
www.ip138.com
上面去查
H. IP查询具体地理位置。快!!!
1、打开浏览器,打开网络。点击更多,找到网络应用,点击进入。

I. PHP获取IP同时获取所在地区
需要用IP数据库或者在线IP查询服务。
国内的用纯真数据库(免费)
http://www.65536.cn/work/2008/MobileIPquery/qqwry.htm
国外的用Maxmind数据库(这个要收费)
J. IP查询具体地理位置
这个没法实现的
,即使能查到是安徽的ip地址,也并不代表
使用这个ip地址人在安徽安庆,网络供应商都是给自动获取
ip地址的。更具体的位置
只有网络供应商才能查的到。
