検索
ホームページphp教程php手册サンゴポリプIPライブラリの簡単な分析

サンゴポリプIPライブラリの簡単な分析

Jun 13, 2016 pm 12:33 PM
php使用図書館操作するとは持っている

这不是什么新鲜事情了,很早之前就已经有人做出来了。
就是使用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;
private $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->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;
}
プライベート関数 getbyte($length,$offset=null){
if(!is_null($offset) ){
fseek($this->fp,$offset,SEEK_SET);
}
$b = fread($this->fp,$length);
$b を返す;
}
/**
* IP アドレスをビッグ エンディアン (ハイ エンディアン) 形式のバイナリ データにパックします。
* データ ストレージ形式はリトル エンディアン (ロー エンディアン) です。
* 00 28 C6 DA 218.198.40.0 little endian
* 3F 28 C6 DA 218.198.40.0 little endian
* このようなデータは二分探索では比較できないため、取得した IP データをビッグエンディアンに変換する必要があります
* @param using strrev $ip
* @ビッグエンディアン形式でバイナリデータを返します
*/
private function Packip($ip){
return Pack( "N", intval( ip2long( $ip)));
}

private function getlong($length=4, $offset=null){
$chr=null;
for($c=0;$length%4!=0&&$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;
}

private function getstring($data="",$offset=) null){
$char = $this->getbyte(1,$offset);
while(ord($char) > 0){
$data .= $char;
$char = $this->getbyte(1);
}
return $data;
}

private function iplocaltion($ip){
$ip = $this->>packip($ip);
$low = 0;
$high = $this-> ipnumber-1;
$ipposition = $this->lastip;
while($low $t = Floor(($low $high)/2);
if($ip 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 "未知";
Break;
case self::REDIRECT_MODE_1:
case self::REDIRECT_MODE_2:
return $this->getstring("",$this->getlong(3) );
休憩;
デフォルト:
return $this->getstring($b);
Break;
}
}
public 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 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->count ry = $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->get area();
$this->country = $this->getstring("",$countryoffset);
Break;

デフォルト:
$this-& gt;国 = $this-> getstring($b);
$this->area = $this->getarea();
Break;
}
}
}
/* */
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 文件结构上分域:

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

この文書ブロックの保存形式は、リトル エンディアンです。
ここでは、Unicode コードのリトル エンディアンとビッグ エンディアンに関する領域を参照しています。

引用

ビッグ エンディアンとリトル エンディアンは、CPU が処理する多文字数の異なる方式です。たとえば、「チェック」文字の Unicode コードは 6C49 です。

6C を前に書けばビッグエンディアンになります。49 を前に書けばリトルエンディアンになります。

「エンディアン」これは《格配列》から出ています。これまでに6回の乱乱が発生し、そのうちの1人の皇帝が命を授け、もう1人の皇帝が王位を継承した。

一般にエンディアン翻訳を「文字シーケンス」とし、ビッグ エンディアンとリトル エンディアンを「大尾」と「小尾」と呼びます。

文ファイル: 色の四角形の中にあるのが文ファイルで、前の 4 文字がインデックス領域の開始位置、後の 4 文字がインデックス領域の終了位置です。

次の図に示すように:

点击放大

データベースはリトルエンディアンの文字列を使用しているため、これをオーバーライドする必要があります。
は、ファイルの 0 ~ 3 の文字列を取得し、unpack 関数を使用してデータを二重に変換します。

処理後のインデックス エリアの開始位置は 00077450 です。インデックス エリアの終了位置は 000CE17C です。

ハンドルに UltraEdit のソフトウェアがある場合は、CoralWry を起動できます。
次の図に示すように、
は、アドレス 00077450 の位置、つまり IP アドレス インデックス領域の始まりです。

点击放大

色の棒が存在するのがインデックス エリアの開始位置です。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

WebStorm Mac版

WebStorm Mac版

便利なJavaScript開発ツール

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

このプロジェクトは osdn.net/projects/mingw に移行中です。引き続きそこでフォローしていただけます。 MinGW: GNU Compiler Collection (GCC) のネイティブ Windows ポートであり、ネイティブ Windows アプリケーションを構築するための自由に配布可能なインポート ライブラリとヘッダー ファイルであり、C99 機能をサポートする MSVC ランタイムの拡張機能が含まれています。すべての MinGW ソフトウェアは 64 ビット Windows プラットフォームで実行できます。

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

mPDF

mPDF

mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。