- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章解析php根据ip查询所在地区(非常有用,赶集网就用到)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
dat文件,关于ip对应地区的信息文件 qqwry.dat文件 网上自己下载 class类文件,解析qqwry.data文件的 IpLocation.php文件 。
复制代码 代码如下
<?php class IpLocation { /** * @var resource 指针 */ private $fp; /** * 第一条IP记录的偏移地址 * @var int */ private $firstip; /** * 最后一条IP记录的偏移地址 * @var int */ private $lastip; /** * IP记录的总条数(不包含版本信息记录) * @var int */ private $totalip; /** * 构造函数,打开 QQWry.Dat 文件并初始化类中的信息 * @param string $filename * @return IpLocation */ public function __construct($filename = "qqwry.dat") { $this->fp = 0; if (($this->fp = @fopen($filename, 'rb')) !== false) { $this->firstip = $this->getlong(); $this->lastip = $this->getlong(); $this->totalip = ($this->lastip - $this->firstip) / 7; } } /** * 返回读取的长整型数 * @access private * @return int */ public function getlong() { //将读取的little-endian编码的4个字节转化为长整型数 $result = unpack('Vlong', fread($this->fp, 4)); return $result['long']; } /** * 返回读取的3个字节的长整型数 * * @access private * @return int */ public 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 */ public function packip($ip) { // 将IP地址转化为长整型数,如果在PHP5中,IP地址错误,则返回False, // 这时intval将Flase转化为整数-1,之后压缩成big-endian编码的字符串 return pack('N', intval(ip2long($ip))); } /** * 返回读取的字符串 * * @access private * @param string $data * @return string */ public function getstring($data = "") { $char = fread($this->fp, 1); while (ord($char) > 0) { // 字符串按照C格式保存,以\0结束 $data .= $char; // 将读取的字符连接到给定字符串之后 $char = fread($this->fp, 1); } return mb_convert_encoding($data, 'utf-8', 'gb2312'); } /** * 返回地区信息 * * @access private * @return string */ public 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; } /** * 析构函数,用于在页面执行结束后自动关闭打开的文件。 * */ function __desctruct() { if ($this->fp) { fclose($this->fp); } $this->fp = 0; } } ?> 。
这个也可以网上下载,也可以copy这里的,这里的也是很全的。 执行文件,我这里叫ip_location.php文件 。
复制代码 代码如下
<?php function getIpPlace(){ require_once("IpLocation.php")//加载类文件IpLocation.php $ipfile = "qqwry.dat"; //获取ip对应地区的信息文件 $iplocation = new IpLocation($ipfile); //new IpLocation($ipfile) $ipfile ip对应地区信息文件 $ipresult = $iplocation->getlocation("ip地址"); //根据ip地址获得地区 getlocation("ip地区") return $ipresult; } print_r($getIpPlace()); //调用方法 ?> 。
最后此篇关于解析php根据ip查询所在地区(非常有用,赶集网就用到)的文章就讲到这里了,如果你想了解更多关于解析php根据ip查询所在地区(非常有用,赶集网就用到)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
更新:随意给我反对票,因为问题是我将文件命名为 _stylesheet.html.erb 而不是 _stylesheets.html.erb。我以为我检查了拼写,但显然我没有。我很抱歉浪费了大家的时间
我有一个 Inno Script istaller 在其中运行子 setup.exe 。当向主安装程序提供静默安装参数时,我必须向 setup.exe 提供静默安装参数。 Inno脚本运行命令: [R
我正在尝试在大型数据库中搜索长的、近似的子字符串。例如,一个查询可能是一个 1000 个字符的子字符串,它可能与匹配项相差数百个编辑的 Levenshtein 距离。我听说索引 q-gram 可以做到
我正在尝试在我的应用程序中实现一个非常简单的绘图 View 。这只是我的应用程序的一小部分,但它正在变成一个真正的麻烦。这是我到目前为止所拥有的,但它现在显示的只是莫尔斯电码,如点和线。 - (v
我有一个运行非常慢的 sql 查询,我很困惑为什么。查询是: SELECT DISTINCT(c.ID),c.* FROM `content` c LEFT JOIN `content_meta`
我搜索过这个,但我发现的所有结果对我来说都毫无意义,而且似乎太复杂了。我希望使用 json 或 simplejson 模块来获取对象中字符串的值。 string = '{"name": "Alex"}
我想编写一个流量生成器来复制正在运行的计算机对内存进行的原始读写需求。 但是正在运行的计算机在其内存引用中也显示出(非常强的)局部性,并且在 64 位地址空间中,只会引用非常小范围的地址(事实上,我已
我正在尝试做一个 Project Euler问题,但它涉及添加一个非常大的数字的数字。 (100!) 用Java的int和long太小了。 谢谢你的建议 最佳答案 类 BigInteger看起来它可能
我想在游戏中实现一个物理引擎,以便计算物体在受力时的轨迹。该引擎将根据对象的先前状态计算对象的每个状态。当然,这意味着要在两个时间单位之间进行大量计算才能足够精确。 为了正确地做到这一点,我首先想知道
Edit3:通过将数组的初始化限制为仅奇数进行优化。谢谢@Ronnie! Edit2:谢谢大家,看来我也无能为力了。 编辑:我知道 Python 和 Haskell 是用其他语言实现的,并且或多或少地
背景 我有一个我编写的简单媒体客户端/服务器,我想生成一个非显而易见的时间值,我随每个命令从客户端发送到服务器。时间戳将包含相当多的数据(纳秒分辨率,即使由于现代操作系统中定时器采样的限制,它并不真正
一位招聘软件工程师的 friend 希望我为他开发一个应用。 他希望能够根据技能搜索候选人的简历。 正如您想象的那样,可能有数百、可能数千种技能。 在表格中表示候选人的最佳方式是什么?我在想 skil
我的意思是“慢”,回调类型等待远程服务器超时以有效触发(调用 vimeo 提要,解析它,然后在场景中显示 uiviews) 我大多不明白它是如何工作的。我希望在返回响应后立即从回调中填充我的 View
您好,我正在研究使用快速可靠的生产者消费者队列进行线程切换。我正在使用 VC++ 在 Windows 上工作。 我的设计基于 Anthony Williams队列,基本上就是一个带有 boost::c
我只是想知道您使用 resharper 的经验。我们有一个非常重的 dbml 文件,因为我们的数据库有很多表,每次我需要打开该文件时,我都会收到来自 resharper 的大量异常。以前有人遇到过这个
我目前正在使用 jQuery 中的隐藏/显示功能来帮助从选择框中将表格过滤成组。 实际代码运行良好,但速度非常慢,有时需要一两分钟才能执行。 我切换了代码,所以它使用 css({'display':'
我按顺序调用了以下两个方法(按顺序使用适当的类级别字段) public const string ProcessName = "This is" public const string WindowT
我很难理解描述反射包的文档/示例。我是一名命令式编程老手,但也是一名 Haskell 新手。你能引导我完成一个非常简单的介绍吗? 包裹:https://hackage.haskell.org/pack
已关闭。此问题不符合Stack Overflow guidelines 。目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,因为
我正在尝试编写一段代码来操作一个很长的文档(超过一百万行)。在这个文本文件中,有固定间隔(每 1003 行)和之间的某些时间戳有我需要的数据,它有 1000 行长,还有一个标题和两个空行,但我不需要。
我是一名优秀的程序员,十分优秀!