ip地址查询

ip地址查询,ip查询,ip所在地查询

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>ip归属地</title>

</head>
<body>
<h1>站长爱好者网IP地址查询</h1>
<form id="form1" name="form1" method="post" action="">
  <input type="text" name="ipinfo" id="ipinfo" style="border:solid 1px #fdbdd5" />
  <input type="submit" name="button" id="button" value="查询" style="border:solid 1px #fdbdd5;width:50px" />
</form>
<?php
include("ipcheck.class.php");
$ipinfo=$_POST['ipinfo'];
$iptest=new IpLocation();
echo $iptest->getlocation($ipinfo);
?>
</body>
</html>
好现在来看看ipcheck.class.php文件.

<?php
/**
* @author 网事如风
* E-mail:rainrenamy@gmail.com
* 个人网址:http://www.adminfan.com
* 演示地址:http;//www.adminfan.com/seo/ip.php
*/
class IpLocation {
/**
* QQWry.Dat文件指针
* @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)));

}

/**
* 返回读取的字符串
*
* @access private
* @param string $data
* @return string
*/

function getstring($data = "") {

$char = fread($this->fp, 1);

while (ord($char) > 0) { // 字符串按照C格式保存,以\0结束

$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;
echo "你的IP:".$location['ip']."<br />来自".$location['country'].$location['area'];

}

/**
* 构造函数,打开 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);

}

}

?>

时间: 2024-07-31 23:26:22

ip地址查询的相关文章

IP地址查询代码

ip地址 IP地址查询的实现方法可能和大家自己的想法不是一样的.大家可以看看一般的实现方法.  ipQueary.jsp <%@page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" %><jsp:useBean id="user" scope="page" class="u

全球IP地址查询完整版

ip地址 全球IP地址查询完整版 突然在某某网站看到IP地址查询,于是心血来潮做了一个.这是用asp查询全球IP地址的程式,由于类似的的用Asp查询IP的速度不是很理想,本人使用的也是宏志宏的IP地址数据库,只是对它进行了改进. 本人在 win98+pws+access2000上测试通过,效果比较理想. 数据库的设计在一个软件中的比例,毫不夸张的说占60%,虽然这是一个小的程式,但也得到一定的体现. 有任何错误或建议请一定要给我发E-mail: ljz811@163.com  ,我也不了解"追捕

C#使用有道ip地址查询接口方法实例详解

  本文实例讲述了C#使用有道ip地址查询接口方法.分享给大家供大家参考.具体实现方法如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 #region 读取http://www.yodao.com接口IP地址 /// <summary> /// 读取http://www.yodao.com接口IP地址 /// </summary> public static

淘宝ip地址查询类分享

 需要显示评论者的地域属性,这个特点可以通过记录会员IP的地理信息来实现,下面提供一个淘宝IP地址查询类,简化相关的信息查询,大家参考使用吧  淘宝公司提供了一个很好用的IP地理信息查询接口.在这里:http://ip.taobao.com/   以下这个taobaoIPQuery类将极大的简化相关的信息查询.   代码如下: <?php   class taobaoIPQuery {       private $m_ip;     private $m_content;       publ

THinkPHP获取客户端IP与IP地址查询的方法_php实例

本文实例讲述了THinkPHP获取客户端IP与IP地址查询的方法.分享给大家供大家参考,具体如下: TP 中获取客户端IP地址的系统公共函数是:function get_client_ip().返回值就是IP地址. 查询IP地址所在国家与地区的类文件是IpLocation.class.php,位于ThinkPHP\Lib\ORG\Net目录下.类名是IpLocation,方法是 public function getlocation($ip=''); 省略时查询客户端IP所在地址.返回的是一个数

THinkPHP获取客户端IP与IP地址查询的方法

本文实例讲述了THinkPHP获取客户端IP与IP地址查询的方法.分享给大家供大家参考,具体如下: TP 中获取客户端IP地址的系统公共函数是:function get_client_ip().返回值就是IP地址. 查询IP地址所在国家与地区的类文件是IpLocation.class.php,位于ThinkPHP\Lib\ORG\Net目录下.类名是IpLocation,方法是 public function getlocation($ip=''); 省略时查询客户端IP所在地址.返回的是一个数

IP地址查询接口及调用方法

1.查询地址 搜狐IP地址查询接口(IP):http://pv.sohu.com/cityjson  1616 IP地址查询接口(IP+地址):http://w.1616.net/chaxun/iptolocal.php 126(地址): http://ip.ws.126.net/ipquery   2.使用 <script src="http://pv.sohu.com/cityjson?ie=utf-8"></script>//ie指定编码,默认是gbk &

php二分法在IP地址查询中的应用_php技巧

数据库大概存储几十万条IP记录,记录集如下: +----------+----------+------------+---------+---------+--------+--------+  | ip_begin | ip_end   | country_id | prov_id | city_id | isp_id | netbar |  +----------+----------+------------+---------+---------+--------+--------+ 

淘宝ip地址查询类分享(利用淘宝ip库)_php实例

淘宝公司提供了一个很好用的IP地理信息查询接口.在这里:http://ip.taobao.com/ 以下这个taobaoIPQuery类将极大的简化相关的信息查询. 复制代码 代码如下: <?php class taobaoIPQuery {     private $m_ip;    private $m_content;     public function __construct($ip) {        if (isset($ip)) {            $this->m_i