导航:首页 > 编程语言 > phpip判断城市

phpip判断城市

发布时间:2022-10-07 20:28:35

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获取地理位置

通过ip地址定位接口,调用的网络地图接口,每人每天有30000次免费调用机会。。

你可以调用我的接口,但不要多次调用。最好是按照我这篇文章中的方法自己搭建一个接口,毕竟我的接口很有可能无法实现你的要求。

着作权归作者所有。

商业转载请联系作者获得授权,非商业转载请注明出处。
作者:caijun
链接:https://www.bangbangcat.com/index.php/PHP/73.html
来源:https://www.bangbangcat.com/

③ 高分求一段IP判断城市的PHP代码

<?php
/**************************/

/******IP判断城市文件******/

/**************************/

$action=$_GET['action'];
if($action==ip){
if ($_SERVER["HTTP_X_FORWARDED_FOR"]){
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];}
else if ($_SERVER["HTTP_CLIENT_IP"]){
$ip = $_SERVER["HTTP_CLIENT_IP"];}
else if ($_SERVER["REMOTE_ADDR"]){
$ip = $_SERVER["REMOTE_ADDR"];}
else if (getenv("HTTP_X_FORWARDED_FOR")){
$ip = getenv("HTTP_X_FORWARDED_FOR");}
else if (getenv("HTTP_CLIENT_IP")){
$ip = getenv("HTTP_CLIENT_IP");}
else if (getenv("REMOTE_ADDR")){
$ip = getenv("REMOTE_ADDR");}
else{
$ip = "Unknown";}
}

$dat_path = 'qqwry.dat';
//判断IP地址是否有效
if(!ereg("^([0-9]{1,3}.){3}[0-9]{1,3}$", $ip)){
return 'IP Address Invalid';
}
//打开IP数据库
if(!$fd = @fopen($dat_path, 'rb')){
return 'IP data file not exists or access denied';
}
//explode函数分解IP地址,运算得出整数形结果
$ip = explode('.', $ip);
$ipNum = $ip[0] * 16777216 + $ip[1] * 65536 + $ip[2] * 256 + $ip[3];
//获取IP地址索引开始和结束位置
$DataBegin = fread($fd, 4);
$DataEnd = fread($fd, 4);
$ipbegin = implode('', unpack('L', $DataBegin));
if($ipbegin < 0) $ipbegin += pow(2, 32);
$ipend = implode('', unpack('L', $DataEnd));
if($ipend < 0) $ipend += pow(2, 32);
$ipAllNum = ($ipend - $ipbegin) / 7 + 1;
$BeginNum = 0;
$EndNum = $ipAllNum;
//使用二分查找法从索引记录中搜索匹配的IP地址记录
while($ip1num>$ipNum || $ip2num<$ipNum) {
$Middle= intval(($EndNum + $BeginNum) / 2);
//偏移指针到索引位置读取4个字节
fseek($fd, $ipbegin + 7 * $Middle);
$ipData1 = fread($fd, 4);
if(strlen($ipData1) < 4) {
fclose($fd);
return 'File Error';
}
//提取出来的数据转换成长整形,如果数据是负数则加上2的32次幂
$ip1num = implode('', unpack('L', $ipData1));
if($ip1num < 0) $ip1num += pow(2, 32);
//提取的长整型数大于我们IP地址则修改结束位置进行下一次循环
if($ip1num > $ipNum) {
$EndNum = $Middle;
continue;
}
//取完上一个索引后取下一个索引
$DataSeek = fread($fd, 3);
if(strlen($DataSeek) < 3) {
fclose($fd);
return 'File Error';
}
$DataSeek = implode('', unpack('L', $DataSeek.chr(0)));
fseek($fd, $DataSeek);
$ipData2 = fread($fd, 4);
if(strlen($ipData2) < 4) {
fclose($fd);
return 'File Error';
}
$ip2num = implode('', unpack('L', $ipData2));
if($ip2num < 0) $ip2num += pow(2, 32);
//找不到IP地址对应城市
if($ip2num < $ipNum) {
if($Middle == $BeginNum) {
fclose($fd);
return 'No Data';
}
$BeginNum = $Middle;
}
}
$ipFlag = fread($fd, 1);
if($ipFlag == chr(1)) {
$ipSeek = fread($fd, 3);
if(strlen($ipSeek) < 3) {
fclose($fd);
return 'System Error';
}
$ipSeek = implode('', unpack('L', $ipSeek.chr(0)));
fseek($fd, $ipSeek);
$ipFlag = fread($fd, 1);
}
if($ipFlag == chr(2)) {
$AddrSeek = fread($fd, 3);
if(strlen($AddrSeek) < 3) {
fclose($fd);
return 'System Error';
}
$ipFlag = fread($fd, 1);
if($ipFlag == chr(2)) {
$AddrSeek2 = fread($fd, 3);
if(strlen($AddrSeek2) < 3) {
fclose($fd);
return 'System Error';
}
$AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
fseek($fd, $AddrSeek2);
} else {
fseek($fd, -1, SEEK_CUR);
}
while(($char = fread($fd, 1)) != chr(0))
$ipAddr2 .= $char;
$AddrSeek = implode('', unpack('L', $AddrSeek.chr(0)));
fseek($fd, $AddrSeek);
while(($char = fread($fd, 1)) != chr(0))
$ipAddr1 .= $char;
} else {
fseek($fd, -1, SEEK_CUR);
while(($char = fread($fd, 1)) != chr(0))
$ipAddr1 .= $char;
$ipFlag = fread($fd, 1);
if($ipFlag == chr(2)) {
$AddrSeek2 = fread($fd, 3);
if(strlen($AddrSeek2) < 3) {
fclose($fd);
return 'System Error';
}
$AddrSeek2 = implode('', unpack('L', $AddrSeek2.chr(0)));
fseek($fd, $AddrSeek2);
} else {
fseek($fd, -1, SEEK_CUR);
}
while(($char = fread($fd, 1)) != chr(0)){
$ipAddr2 .= $char;
}
}
fclose($fd);
//返回IP地址对应的城市结果
if(preg_match('/http/i', $ipAddr2)) {
$ipAddr2 = '';
}
$ipaddr = "$ipAddr1 $ipAddr2";
$ipaddr = preg_replace('/CZ88.Net/is', '', $ipaddr);
$ipaddr = preg_replace('/^s*/is', '', $ipaddr);
$ipaddr = preg_replace('/s*$/is', '', $ipaddr);
if(preg_match('/http/i', $ipaddr) || $ipaddr == '') {
$ipaddr = 'No Data';
}

require_once("cityadd.php");

if(strpos($ipaddr,$hefei))
{

}

echo "var jstext='$hefei'"; //输出一句JS语句,生成一个JS变量,并赋颠值为PHP变量 $action的值

?>
一下是cityadd.php的内容

<?php
$hefei = "合肥市";
$wuhu = "芜湖市";
$huainan = "淮南市";
$maanshan = "马鞍山市";
$chuzhou = "滁州市";
$huaibei = "淮北市";
$tongling = "铜陵市";
$anqing = "安庆市";
$huangshan = "黄山市";
$fuyang = "阜阳市";
$bengbu = "蚌端口市";
$suzhou = "宿州市";
$luan = "六安市";
$bozhou = "亳州市";
$chizhou = "池州市";
$xuancheng = "宣城市";
$qita = "其他";
?>
上网下一个qqwry.dat
这几个文件放在一个文件夹,做一个页面里面放一个JS,并且调用第一个文件的函数,就可以了
<script >document.write(jstext)</script>

④ 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) "" }

⑤ php 如何判断客户端ip地址属于哪个省市

1。取得客户端IP地址
2。需要IP地址库,对比查询获得
这个IP地址库,最开始的时候珊瑚虫有,现在不知道哪家还在做

⑥ php 怎么通过ip来获取所在位置

可以使用第三方接口 比如网络地图 它可以通过ip来定位用户的经纬度坐标 有了坐标就可以直接定位用户所在城市和具体位置

阅读全文

与phpip判断城市相关的资料

热点内容
为什么软件需要服务器 浏览:61
redis操作命令大全 浏览:597
python字符串重复索引 浏览:959
为什么香信新版本连接不上服务器 浏览:46
元旦程序员打羽毛球 浏览:612
otc焊接机器人离线编程教学 浏览:410
51单片机的ea引脚有何用途 浏览:205
centos查看用户命令 浏览:838
程序员脸胖 浏览:744
hdfs在主目录下创建文件夹 浏览:798
股票选股器源码公式如何弄 浏览:31
服务器如何使用在微信上 浏览:326
app登不了是怎么回事 浏览:252
dd命令u盘 浏览:568
单片机生日快乐程序 浏览:891
安卓手机连车载的叫什么 浏览:223
怎么让自己的手机键盘变得好看app 浏览:53
能看qq的文件夹 浏览:515
android二维码生成代码 浏览:567
焦炉气压缩机 浏览:402