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地址的。更具體的位置
只有網路供應商才能查的到。
