IP照会の方法(PHPとC#)
まず最新の:(純真)qwry.datファイルをダウンロードします
C#での使用方法:
PHPで使用:
C#での使用方法:
class IPHelper
{
FileStream ipFile;
long ip;
string ipfilePath;
///<summary>
///
///</summary>
///<param name="ipfilePath"> IP </param>
public IPHelper(string ipfilePath)
{
this.ipfilePath = ipfilePath;
}
///<summary>
/// ,
///</summary>
public struct IPLocation
{
public string country, area;
}
///<summary>
/// IP
///</summary>
///<param name="strIP"> IP </param>
///<returns></returns>
public IPLocation GetIPLocation(string strIP)
{
ip = IPToLong(strIP);
ipFile = new FileStream(ipfilePath, FileMode.Open, FileAccess.Read);
long[] ipArray = BlockToArray(ReadIPBlock());
long offset = SearchIP(ipArray, 0, ipArray.Length - 1) * 7 + 4;
ipFile.Position += offset;// IP
ipFile.Position = ReadLongX(3) + 4;// IP
IPLocation loc = new IPLocation();
int flag = ipFile.ReadByte();//
if (flag == 1)//
{
ipFile.Position = ReadLongX(3);
flag = ipFile.ReadByte();//
}
long countryOffset = ipFile.Position;
loc.country = ReadString(flag);
if (flag == 2)
{
ipFile.Position = countryOffset + 3;
}
flag = ipFile.ReadByte();
loc.area = ReadString(flag);
ipFile.Close();
ipFile = null;
return loc;
}
///<summary>
/// IP long
///</summary>
///<param name="strIP"></param>
///<returns></returns>
public long IPToLong(string strIP)
{
byte[] ip_bytes = new byte[8];
string[] strArr = strIP.Split(new char[] { '.' });
for (int i = 0; i < 4; i++)
{
ip_bytes[i] = byte.Parse(strArr[3 - i]);
}
return BitConverter.ToInt64(ip_bytes, 0);
}
///<summary>
/// IP Long
///</summary>
///<param name="ipBlock"></param>
long[] BlockToArray(byte[] ipBlock)
{
long[] ipArray = new long[ipBlock.Length / 7];
int ipIndex = 0;
byte[] temp = new byte[8];
for (int i = 0; i < ipBlock.Length; i += 7)
{
Array.Copy(ipBlock, i, temp, 0, 4);
ipArray[ipIndex] = BitConverter.ToInt64(temp, 0);
ipIndex++;
}
return ipArray;
}
///<summary>
/// IP IP
///</summary>
///<param name="ipArray">IP </param>
///<param name="start"> </param>
///<param name="end"> </param>
///<returns></returns>
int SearchIP(long[] ipArray, int start, int end)
{
int middle = (start + end) / 2;
if (middle == start)
return middle;
else if (ip < ipArray[middle])
return SearchIP(ipArray, start, middle);
else
return SearchIP(ipArray, middle, end);
}
///<summary>
/// IP
///</summary>
///<returns></returns>
byte[] ReadIPBlock()
{
long startPosition = ReadLongX(4);
long endPosition = ReadLongX(4);
long count = (endPosition - startPosition) / 7 + 1;//
ipFile.Position = startPosition;
byte[] ipBlock = new byte[count * 7];
ipFile.Read(ipBlock, 0, ipBlock.Length);
ipFile.Position = startPosition;
return ipBlock;
}
///<summary>
/// IP long
///</summary>
///<param name="bytesCount"> , 8 </param>
///<returns></returns>
long ReadLongX(int bytesCount)
{
byte[] _bytes = new byte[8];
ipFile.Read(_bytes, 0, bytesCount);
return BitConverter.ToInt64(_bytes, 0);
}
///<summary>
/// IP
///</summary>
///<param name="flag"> </param>
///<returns></returns>
string ReadString(int flag)
{
if (flag == 1 || flag == 2)//
ipFile.Position = ReadLongX(3);
else
ipFile.Position -= 1;
List<byte> list = new List<byte>();
byte b = (byte)ipFile.ReadByte();
while (b > 0)
{
list.Add(b);
b = (byte)ipFile.ReadByte();
}
return Encoding.Default.GetString(list.ToArray());
}
}
呼び出しは次のとおりです.static void Main(string[] args)
{
string ipfilePath = @"D:\qqwry.dat";
IPHelper IPHelper = new IPHelper(ipfilePath);
string ip = "72.51.27.51";
IPHelper.IPLocation loc = IPHelper.GetIPLocation(ip);
Console.WriteLine(" ip :{0} :{1} {2}", ip, loc.country, loc.area);
Console.ReadKey();
}
PHPで使用:
<?php
header("content-type:text/html;charset=gb2312");
//*
// [ (4byte)] + [ (4byte)] 8
// [ ip (4byte)] + [ 1] + [ 2] 4 +
// [ ip (4byte)] + [ (3byte)] 7
// : IP , "CoralWry.dat" .
//by www.query8.com
//*
class ipLocation {
var $fp;
var $firstip; // ip
var $lastip; // ip
var $totalip; // ip
//*
// ,
//$datfile IP , .
//*
function ipLocation($datfile = "qqwry.dat"){
$this->fp=fopen($datfile,'rb'); //
$this->firstip = $this->get4b(); // ip
$this->lastip = $this->get4b(); // ip
$this->totalip =($this->lastip - $this->firstip)/7 ; //ip 7 , 7,
register_shutdown_function(array($this,"closefp")); // php5 , , ip .
}
//*
// ip
//*
function closefp(){
fclose($this->fp);
}
//*
// 4 long
//*
function get4b(){
$str=unpack("V",fread($this->fp,4));
return $str[1];
}
//*
//
//*
function getoffset(){
$str=unpack("V",fread($this->fp,3).chr(0));
return $str[1];
}
//*
// ip
//*
function getstr(){
$split=fread($this->fp,1);
$str="";
while (ord($split)!=0) {
$str.=$split;
$split=fread($this->fp,1);
}
return $str;
}
//*
// ip ip2long ipv4 , big-endian
// ip
//*
function iptoint($ip){
return pack("N",intval(ip2long($ip)));
}
//*
// ip
// : ip , ip .
//*
function getIP() {
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
}
elseif (getenv('HTTP_X_FORWARDED_FOR')) { // ip
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
elseif (getenv('HTTP_X_FORWARDED')) {
$ip = getenv('HTTP_X_FORWARDED');
}
elseif (getenv('HTTP_FORWARDED_FOR')) {
$ip = getenv('HTTP_FORWARDED_FOR');
}
elseif (getenv('HTTP_FORWARDED')) {
$ip = getenv('HTTP_FORWARDED');
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
//*
//
//*
function readaddress(){
$now_offset=ftell($this->fp); //
$flag=$this->getflag();
switch (ord($flag)){
case 0:
$address="";
break;
case 1:
case 2:
fseek($this->fp,$this->getoffset());
$address=$this->getstr();
break;
default:
fseek($this->fp,$now_offset);
$address=$this->getstr();
break;
}
return $address;
}
//*
// 1 2
// .
//*
function getflag(){
return fread($this->fp,1);
}
//*
// ip
//*
function searchip($ip){
$ip=gethostbyname($ip); // ip
$ip_offset["ip"]=$ip;
$ip=$this->iptoint($ip); // ip
$firstip=0; //
$lastip=$this->totalip; //
$ipoffset=$this->lastip; // ip
while ($firstip <= $lastip){
$i=floor(($firstip + $lastip) / 2); // floor ,
fseek($this->fp,$this->firstip + $i * 7); //
$startip=strrev(fread($this->fp,4)); // ip , little-endian big-endian
if ($ip < $startip) {
$lastip=$i - 1;
}
else {
fseek($this->fp,$this->getoffset());
$endip=strrev(fread($this->fp,4));
if ($ip > $endip){
$firstip=$i + 1;
}
else {
$ip_offset["offset"]=$this->firstip + $i * 7;
break;
}
}
}
return $ip_offset;
}
//*
// ip
//*
function getaddress($ip){
$ip_offset=$this->searchip($ip); // ip
$ipoffset=$ip_offset["offset"];
$address["ip"]=$ip_offset["ip"];
fseek($this->fp,$ipoffset); //
$address["startip"]=long2ip($this->get4b()); // ip
$address_offset=$this->getoffset(); // ip ip
fseek($this->fp,$address_offset); //
$address["endip"]=long2ip($this->get4b()); // ip
$flag=$this->getflag(); //
switch (ord($flag)) {
case 1: // 1 2
$address_offset=$this->getoffset(); //
fseek($this->fp,$address_offset); //
$flag=$this->getflag(); //
switch (ord($flag)) {
case 2: // 1 ,
fseek($this->fp,$this->getoffset());
$address["area1"]=$this->getstr();
fseek($this->fp,$address_offset+4); // 4
$address["area2"]=$this->readaddress(); // 2 ,
break;
default: // 1, 2
fseek($this->fp,$address_offset); //
$address["area1"]=$this->getstr();
$address["area2"]=$this->readaddress();
break;
}
break;
case 2: // 1 2
$address1_offset=$this->getoffset(); //
fseek($this->fp,$address1_offset);
$address["area1"]=$this->getstr();
fseek($this->fp,$address_offset+8);
$address["area2"]=$this->readaddress();
break;
default: // 1 2
fseek($this->fp,$address_offset+4);
$address["area1"]=$this->getstr();
$address["area2"]=$this->readaddress();
break;
}
//*
if (strpos($address["area1"],"CZ88.NET")!=false){
$address["area1"]=" ";
}
if (strpos($address["area2"],"CZ88.NET")!=false){
$address["area2"]=" ";
}
return $address;
}
}
//
$t = new ipLocation;
$result = $t->getaddress('72.51.27.51');
print_r($result);
echo $result[' area1'];
//*ipLocation class end
?>