PHP displays region code based on IP

Source: Internet
Author: User
Tags chr explode fread pow strlen unpack


<?php
Define (' Ipdata_mini ', Phpcms_root. ' Include/ipdata/mini. Dat ');
Define (' Ipdata_full ', phpcms_root. ' Include/ipdata/qqwry.dat ');

Class Ip_area
{
var $fp = NULL;
var $func;
var $offset;
var $index;

function Ip_area ()
{
if (@file_exists (Ipdata_mini))
{
$this->func = ' Data_mini ';
$this->FP = @fopen (Ipdata_mini, ' RB ');
$this->offset = Unpack (' Nlen ', fread ($this->FP, 4));
$this->index = fread ($this->fp, $this->offset[' Len ']-4);
}
ElseIf (@file_exists (ipdata_full))
{
$this->func = ' data_full ';
$this->FP = @fopen (ipdata_full, ' RB ');
}
}

function Get ($IP)
{
$return = ';
if (Preg_match ("/^d{1,3}.d{1,3}.d{1,3}.d{1,3}$/", $ip))
{
$iparray = Explode ('. ', $IP);
if ($iparray [0] = = | | $iparray [0] = 127 | | ($iparray [0] = && $iparray [1] = 168) | | ($iparray [0] = = 172 && ($iparray [1] >= && $iparray [1] <= 31))
{
$return = ' LAN ';
}
ElseIf ($iparray [0] > 255 | | $iparray [1] > 255 | | $iparray [2] > 255 | | $iparray [3] > 255)
{
$return = ' Invalid IP address ';
}
Else
{
$return = $this->func? $this->{$this->func} ($IP): ';
if (Strpos ($return, ')!== false) $return = substr ($return, 0, Strpos ($return, "));
}
if (Strtolower (CHARSET) = = ' Utf-8 ') $return = Iconv (' GBK ', ' utf-8 ', $return);
}
return $return;
}

function Data_mini ($IP)
{
$ipdot = Explode ('. ', $IP);
$ipdot [0] = (int) $ipdot [0];
$ipdot [1] = (int) $ipdot [1];
$ip = Pack (' N ', Ip2long ($IP));
$length = $this->offset[' len ']-1028;
$start = Unpack (' Vlen ', $this->index[$ipdot [0] * 4]. $this->index[$ipdot [0] * 4 + 1]. $this->index[$ipdot [0] * 4 + 2]. $this->index[$ipdot [0] * 4 + 3]);
for ($start = $start [' Len '] * 8 + 1024; $start < $length; $start + + 8)
{
if ($this->index{$start}. $this->index{$start + 1}. $this->index{$start + 2}. $this->index{$start + 3} >= $IP)
{
$this->index_offset = Unpack (' Vlen ', $this->index{$start + 4}. $this->index{$start + 5}. $this->index{$star T + 6}. "X0");
$this->index_length = Unpack (' Clen ', $this->index{$start + 7});
Break
}
}
Fseek ($this->fp, $this->offset[' len '] + $this->index_offset[' len ']-1024);
if ($this->index_length[' Len '])
{
Return Str_replace ('-', ', fread ($this->fp, $this->index_length[' Len '));
}
Else
{
Return ' Unknown ';
}
}

function Data_full ($IP)
{
Rewind ($this-&GT;FP);
$ip = Explode ('. ', $IP);
$ipNum = $ip [0] * 16777216 + $ip [1] * 65536 + $ip [2] * 256 + $IP [3];
if (!) ( $DataBegin = Fread ($this-&GT;FP, 4)) | | ! ($DataEnd = fread ($this-&GT;FP, 4)) Return
@ $ipbegin = implode (', Unpack (' L ', $DataBegin));
if ($ipbegin < 0) $ipbegin + = POW (2, 32);
@ $ipend = implode (', Unpack (' L ', $DataEnd));
if ($ipend < 0) $ipend + = POW (2, 32);
$ipAllNum = ($ipend-$ipbegin)/7 + 1;
$BeginNum = $ip 2num = $ip 1num = 0;
$ipAddr 1 = $ipAddr 2 = ';
$EndNum = $ipAllNum;
while ($ip 1num > $ipNum | | $ip 2num < $ipNum)
{
$Middle = Intval (($EndNum + $BeginNum)/2);
Fseek ($this->fp, $ipbegin + 7 * $Middle);
$ipData 1 = fread ($this->fp, 4);
if (strlen ($ipData 1) < 4)
{
Fclose ($this-&GT;FP);
Return ' System Error ';
}
$ip 1num = Implode (', Unpack (' L ', $ipData 1));
if ($ip 1num < 0) $ip 1num + + POW (2, 32);
if ($ip 1num > $ipNum)
{
$EndNum = $Middle;
Continue
}
$DataSeek = Fread ($this->fp, 3);
if (strlen ($DataSeek) < 3)
{
Fclose ($this-&GT;FP);
Return ' System Error ';
}
$DataSeek = Implode (', Unpack (' L ', $DataSeek. chr (0)));
Fseek ($this->fp, $DataSeek);
$ipData 2 = fread ($this->fp, 4);
if (strlen ($ipData 2) < 4)
{
Fclose ($this-&GT;FP);
Return ' System Error ';
}
$ip 2num = Implode (', Unpack (' L ', $ipData 2));
if ($ip 2num < 0) $ip 2num + + POW (2, 32);
if ($ip 2num < $ipNum)
{
if ($Middle = = $BeginNum)
{
Fclose ($this-&GT;FP);
Return ' Unknown ';
}
$BeginNum = $Middle;
}
}
$ipFlag = Fread ($this->fp, 1);
if ($ipFlag = = Chr (1))
{
$ipSeek = Fread ($this->fp, 3);
if (strlen ($ipSeek) < 3)
{
Fclose ($this-&GT;FP);
Return ' System Error ';
}
$ipSeek = Implode (', Unpack (' L ', $ipSeek. chr (0)));
Fseek ($this->fp, $ipSeek);
$ipFlag = Fread ($this->fp, 1);
}
if ($ipFlag = = Chr (2))
{
$AddrSeek = Fread ($this->fp, 3);
if (strlen ($AddrSeek) < 3)
{
Fclose ($this-&GT;FP);
Return ' System Error ';
}
$ipFlag = Fread ($this->fp, 1);
if ($ipFlag = = Chr (2)) {
$AddrSeek 2 = fread ($this->fp, 3);
if (strlen ($AddrSeek 2) < 3)
{
Fclose ($this-&GT;FP);
Return ' System Error ';
}
$AddrSeek 2 = implode (', Unpack (' L ', $AddrSeek 2.chr (0)));
Fseek ($this-&GT;FP, $AddrSeek 2);
}
Else
{
Fseek ($this-&GT;FP,-1, seek_cur);
}
while (($char = fread ($this->fp, 1))!= chr (0))
$ipAddr 2. = $char;
$AddrSeek = Implode (', Unpack (' L ', $AddrSeek. chr (0)));
Fseek ($this->fp, $AddrSeek);
while (($char = fread ($this->fp, 1))!= chr (0))
$ipAddr 1. = $char;
}
Else
{
Fseek ($this-&GT;FP,-1, seek_cur);
while (($char = fread ($this->fp, 1))!= chr (0))
$ipAddr 1. = $char;
$ipFlag = Fread ($this->fp, 1);
if ($ipFlag = = Chr (2))
{
$AddrSeek 2 = fread ($this->fp, 3);
if (strlen ($AddrSeek 2) < 3)
{
Fclose ($this-&GT;FP);
Return ' System Error ';
}
$AddrSeek 2 = implode (', Unpack (' L ', $AddrSeek 2.chr (0)));
Fseek ($this-&GT;FP, $AddrSeek 2);
}
Else
{
Fseek ($this-&GT;FP,-1, seek_cur);
}
while (($char = fread ($this->fp, 1))!= chr (0))
$ipAddr 2. = $char;
}
if (Preg_match ('/http/i ', $ipAddr 2)) $ipAddr 2 = ';
$IPADDR = "$ipAddr 1 $ipAddr 2";
$IPADDR = preg_replace ('/cz88.net/is ', ', ', $ipaddr);
$IPADDR = preg_replace ('/^s*/is ', ', ', $ipaddr);
$IPADDR = preg_replace ('/s*$/is ', ', ', $ipaddr);
if (Preg_match ('/http/i ', $ipaddr) | | | $ipaddr = = ') $ipaddr = ' Unknown ';
Return '. $ipaddr;
}

function Close ()
{
@fclose ($this->FP);
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.