Coral IP ライブラリの簡単な分析_PHP チュートリアル

WBOY
リリース: 2016-07-21 15:57:11
オリジナル
913 人が閲覧しました

这不是什么新鲜事情了,很早之前就已经有人做出来了。
就是使用PHP操作纯真IP库或珊瑚虫IP库,根据来访者的IP得到所在的物理位置。

我先帖出代码。然后再慢慢一步步浅析出来。希望对想了解这一块的朋友们有帮助。

Only For PHP5的代码。会继续优化代码的。

class IpLocation{
private $fp;
private $wrydat;
private $wrydat_version;
private $ipnumber;
private $firstip;
private $lastip;
private $ip_range_begin;
private $ip_range_end;
プライベート $country;
private $area;
const REDIRECT_MODE_0 = 0;
const REDIRECT_MODE_1 = 1;
const REDIRECT_MODE_2 = 2;
function __construct(){
$args = func_get_args();
$this-&g t;wrydat = func_num_args()>0 ?$args[0]:'CoralWry.dat';
$this->initialize();
}
function __destruct(){
fclose($this->fp);
}
プライベート関数initialize() {
if(file_exists($this->wrydat))
$this->fp = fopen($this->wrydat,'rb');
$this->getipnumber();
$this- >getwryversion();
}
public function get($str){
return $this->$str;
}
public function set($str,$val){
$this->$str = $val;
}
private function getbyte($length,$offset=null){
if(!is_null($offset)){
fseek($this->fp,$offset,SEEK_SET);
}
} $ b = fread($this->fp,$length);
return $b;
}
/**
* IP アドレスをビッグ エンディアン (上位ビットが先) 形式のバイナリ データにパックします。
* データ ストレージ形式は、次のようなリトル エンディアン (下位ビットが先) です:
* 00 28 C6 DA 218.198.40.0 リトル エンディアン
* 3F 28 C6 DA 218.198.40.0 リトルエンディアン
* このようなデータは二分探索では比較できないので、取得したIPデータをstrrevでビッグエンディアンに変換する必要があります
* @param $ip
* @return バイナリデータをビッグエンディアン形式で返す
*/
private function packageip($ip){
return Pack( "N", intval( ip2long( $ip)));
}

プライベート関数 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'];
}

private function getwryversion(){
$length = preg_match("/coral/i",$this->wrydat)?26:30;
$this->wrydat_version = $this->getbyte($length, $this->firstip-$length);
}

private function getipnumber(){
$this->firstip = $this-> ;getlong();
$this->lastip = $this->getlong();
$this->ipnumber = ($this->lastip-$this->firstip)/7+1;
}

プライベート関数 getstring($data="",$offset=null){
$char = $this->getbyte(1,$offset);
while(ord($char) > 0){
$data .= $char;
$char = $this->getbyte(1);
}
return $data;
}

プライベート関数 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 ;
}
プライベート関数 getarea(){
$b = $this->getbyte(1);
switch(ord($b)){
case self::REDIRECT_MODE_0 :
return "未知";
休憩;
case self::REDIRECT_MODE_1:
case self::REDIRECT_MODE_2:
return $this->getstring("",$this->getlong(3));
Break;
デフォルト:
return $this-> getstring($b);
Break;
}
}
public function getiplocation($ip){
$ippos = $this->iplocaltion($ip);
$this->ip_range_begin = long 2ip($this- >getlong(4,$ippos));
$this->ip_range_end = long2ip($this->getlong(4,$this->getlong(3)));
$b = $this-> ;getbyte(1);
switch (ord($b)){
case self::REDIRECT_MODE_1:
$b = $this->getbyte(1,$this->getlong(3));
if( ord($b) == 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();
}
ブレーク;

case self::REDIRECT_MODE_2 :
$countryoffset = $this->getlong(3);
$this->area = $this->getarea();
$this->country = $this ->getstring("", $countryoffset);
Break;

デフォルト:
$this->country = $this->getstring($b);
$this->area = $this->getarea();
休憩;
}
}
}
/* */
echo microtime();
echo "n";
$iploca = new IpLocation;
//$iploca = new IpLocation('QQWry.dat');
echo $iploca ->get('wrydat_version');
echo "n";
echo $iploca->get('ipnumber');
echo "n";
$iploca->getiplocation('211.44.32.34') ;
/**/
echo $iploca->get('ip_range_begin');
echo "n";
echo $iploca->get('ip_range_end');
echo "n";
echo $iploca ->get('country');
echo "n";
echo $iploca->get('area');

echo "n";
echo $iploca->get('lastip');
echo "n";
echo microtime();
echo "n";
unset($iploca);

参考资料:LumaQQの纯真IPデータベース库格式详解

CoralWry.dat文件结构上分享3个領域:

  • 文件头[固定8个字节]
  • データステーション[不固定長、记录IPの地址情報]
  • 索引区[大小由文件头决定]

このファイルブロックの保存形式は、リトルエンディアンです。
ここでは、リトルエンディアンとビッグエンディアンに関するUnicodeコードの区切りを参照します

引用

ビッグ エンディアンとリトル エンディアンは、CPU が多文字数を処理する方法です。たとえば、「チェック」文字の Unicode コードは 6C49 です。ファイルに書き込むとき、厳密には 6C を前に書き込むのですか? 49 を前に書き込むのですか? 6C を先頭に書けば、ビッグエンディアンになります。また、49 を先頭に書けば、リトルエンディアンになります。

「エンディアン」これは《格列佛游定》から出ています。小人民国の内部は吃鸡蛋白質に由来するものであり、大規模(ビッグエンディアン)からの拡張でもあり、これからもずっと小头(リトルエンディアン)からの拡張である6回の乱乱が発生し、そのうちの1人の皇帝が命を授け、もう1人の皇帝が王位を継承した。

私たちは通常、エンディアン翻訳を「文字列」とし、ビッグ エンディアンとリトル エンディアンを「大尾」と「小尾」と呼びます。

テキストボックス:

色の四角形のエリアはテキストボックスで、前の 4 文字はインデックス領域の開始位置、後の 4 文字はインデックス領域の終了位置です。

下の図に示されているように:


点击放大

ファイルの 0 ~ 3 の文字を取得し、再度 unpack 関数を使用してデータをビッグ エンディアン形式に変換します。

処理後のインデックス エリアの開始位置は 00077450 です。インデックス エリアの終了位置は 000CE17C です。の位置、つまり IP アドレス インデックス領域の開始点です。 Coral IP ライブラリの簡単な分析_PHP チュートリアル

点击放大


色の四角形がインデックス区の開始位置です。

www.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/317901.html技術記事これは何も新しいことではなく、ずっと前に誰かがやっていたのです。 PHP を使用して Pure IP ライブラリまたは Coral IP ライブラリを操作し、訪問者の IP に基づいて物理的な位置を取得します。 私は...
ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!