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方法