dat文件,关于ip对应地区的信息文件
qqwry.dat文件
网上自己下载
class类文件,解析qqwry.data文件的
IpLocation.php文件
- <?php
- class IpLocation {
-
-
-
- private $fp;
-
-
-
-
-
- private $firstip;
-
-
-
-
-
- private $lastip;
-
-
-
-
-
- private $totalip;
-
-
-
-
-
-
- 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;
- }
- }
-
-
-
-
-
-
- public function getlong() {
-
- $result = unpack('Vlong', fread($this->fp, 4));
- return $result['long'];
- }
-
-
-
-
-
-
-
- public function getlong3() {
-
- $result = unpack('Vlong', fread($this->fp, 3).chr(0));
- return $result['long'];
- }
-
-
-
-
-
-
-
-
- public function packip($ip) {
-
-
- return pack('N', intval(ip2long($ip)));
- }
-
-
-
-
-
-
-
-
- public function getstring($data = "") {
- $char = fread($this->fp, 1);
- while (ord($char) > 0) {
- $data .= $char;
- $char = fread($this->fp, 1);
- }
- return mb_convert_encoding($data, 'utf-8', 'gb2312');
- }
-
-
-
-
-
-
-
- public function getarea() {
- $byte = fread($this->fp, 1);
- switch (ord($byte)) {
- case 0:
- $area = "";
- break;
- case 1:
- case 2:
- fseek($this->fp, $this->getlong3());
- $area = $this->getstring();
- break;
- default:
- $area = $this->getstring($byte);
- break;
- }
- return $area;
- }
-
-
-
-
-
-
-
- function getlocation($ip) {
- if (!$this->fp) return null;
- $location['ip'] = gethostbyname($ip);
- $ip = $this->packip($location['ip']);
-
-
- $l = 0;
- $u = $this->totalip;
- $findip = $this->lastip;
- while ($l <= $u) {
- $i = floor(($l + $u) / 2);
- fseek($this->fp, $this->firstip + $i * 7);
- $beginip = strrev(fread($this->fp, 4));
-
-
- if ($ip < $beginip) {
- $u = $i - 1;
- }else{
- fseek($this->fp, $this->getlong3());
- $endip = strrev(fread($this->fp, 4));
- if ($ip > $endip) {
- $l = $i + 1;
- }else{
- $findip = $this->firstip + $i * 7;
- break;
- }
- }
- }
-
-
- fseek($this->fp, $findip);
- $location['beginip'] = long2ip($this->getlong());
- $offset = $this->getlong3();
- fseek($this->fp, $offset);
- $location['endip'] = long2ip($this->getlong());
- $byte = fread($this->fp, 1);
-
- switch (ord($byte)) {
- case 1:
- $countryOffset = $this->getlong3();
- fseek($this->fp, $countryOffset);
- $byte = fread($this->fp, 1);
- switch (ord($byte)) {
- case 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:
- 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") {
- $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")
- $ipfile = "qqwry.dat";
- $iplocation = new IpLocation($ipfile);
- $ipresult = $iplocation->getlocation("ip地址");
- return $ipresult;
- }
- print_r($getIpPlace());
- ?>
-
http://blog.csdn.net/motian06/article/details/7919675
|
请发表评论