phpip定位
『壹』 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;
『貳』 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);
使用了新浪的開放API
『叄』 php怎樣獲取用戶真實ip
獲取客戶端ip其實不是個簡單的事兒,因為存在IP欺騙,和代理問題,所以獲取客戶端的IP的真實性會打折扣的,不能百分百准確。但是我們還是可以盡量寫一些比較完善的獲取客戶端真正ip方法。使用php獲取IP的方法能有很多,我簡單給個例子:
<?php
functiongetip(){
$unknown='unknown';
if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])&&$_SERVER['HTTP_X_FORWARDED_FOR']&&strcasecmp($_SERVER['HTTP_X_FORWARDED_FOR'],$unknown)){
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}elseif(isset($_SERVER['REMOTE_ADDR'])&&$_SERVER['REMOTE_ADDR']&&strcasecmp($_SERVER['REMOTE_ADDR'],$unknown)){
$ip=$_SERVER['REMOTE_ADDR'];
}
/**
*處理多層代理的情況
*或者使用正則方式:$ip=preg_match("/[d.]{7,15}/",$ip,$matches)?$matches[0]:$unknown;
*/
if(false!==strpos($ip,','))$ip=reset(explode(',',$ip));
return$ip;
}
?>
1、沒有使用代理的情況
REMOTE_ADDR = 客戶端IP
HTTP_X_FORWARDED_FOR = 沒數值或不顯示
2、使用透明代理的情況
REMOTE_ADDR = 最後一個代理伺服器 IP
HTTP_X_FORWARDED_FOR = 客戶端真實 IP (經過多個代理伺服器時,這個值類似:221.5.252.160, 203.98.182.163, 203.129.72.215)
這類代理伺服器還是將客戶端真實的IP發送給了訪問對象,無法達到隱藏真實身份的目的.
3、使用普通的匿名代理
REMOTE_ADDR = 最後一個代理伺服器 IP
HTTP_X_FORWARDED_FOR = 代理伺服器 IP (經過多個代理伺服器時,這個值類似:203.98.182.163, 203.98.182.163, 203.129.72.215)
這種情況下隱藏了客戶端的真實IP,但是向訪問對象透露了客戶端是使用代理伺服器訪問它們的.
4、使用欺騙性代理伺服器
REMOTE_ADDR = 代理伺服器 IP
HTTP_X_FORWARDED_FOR = 隨機的 IP(經過多個代理伺服器時,這個值類似:220.4.251.159, 203.98.182.163, 203.129.72.215)
這種情況下同樣透露了客戶端是使用了代理伺服器,但編造了一個虛假的隨機IP(220.4.251.159)代替客戶端的真實IP來欺騙它.
5、使用高級匿名代理伺服器
REMOTE_ADDR = 代理伺服器 IP
HTTP_X_FORWARDED_FOR = 沒數值或不顯示,也可能是unknown
『肆』 php怎麼實現定位除了ip
QQ空間那些是用手機發才會有這樣的位置信息的,用pc發根本記不會有位置信息。
手機定位,還不好搞嗎?和php沒有半毛錢關系!
『伍』 php到底怎麼獲取IP地址,為什麼三種方法獲取的IP都不一樣
你電腦接了路由器,127.0.0.1是本地回環地址,網上測試那個是公網IP地址,本機做伺服器,伺服器地址就是127.0.0.1
『陸』 php獲取了ip地址,用php怎麼獲取ip的地理位置請大蝦賜教!
用純真ip資料庫啊,這里有個資料是介紹php操作純真ip資料庫的,是原查詢吧query8.com里提取出來的,用時先去網上搜純真ip資料庫下載,查詢吧php版讀取純真ip資料庫源碼http://www.phperz.com/code/php-class/060913H20081372.html
『柒』 php的 ip 定位,經緯度至少精確到鎮,附上源碼和詳解
ip定位一般對於電腦來說的,精確到鎮有點難,淘寶的ip api服務,只能精確到區,或者市
手機的話,你需要獲取位置,位置會因為手機質量的好壞,偏差很大,蘋果和諾基亞手機定位很准,安卓各種機型,各種偏差,使用手機定位的是,html5有獲取定位的方法,然後得到經緯度,發到網路地圖 api,獲取更精確的地址,
ip定位想要那麼精確,有點難,你得找個好點的ip庫服務,達到你要的精讀就行
『捌』 php 怎麼通過ip來獲取所在位置
可以使用第三方介面 比如網路地圖 它可以通過ip來定位用戶的經緯度坐標 有了坐標就可以直接定位用戶所在城市和具體位置
『玖』 如何使用php獲取本機IP地址
function ip() {
if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$ip = getenv('HTTP_CLIENT_IP');
} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$ip = getenv('HTTP_X_FORWARDED_FOR');
} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$ip = getenv('REMOTE_ADDR');
} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$ip = $_SERVER['REMOTE_ADDR'];
}
return preg_match ( '/[\d\.]{7,15}/', $ip, $matches ) ? $matches [0] : '';
}
以上函數取自phpcms的獲取ip方法