Php display area code by IP address-PHP source code

Source: Internet
Author: User
Php display area code by IP address php display area code by IP

Script ec (2); script


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] = 10 | $ iparray [0] = 127 | ($ iparray [0] = 192 & $ iparray [1] = 168) | ($ iparray [0] = 172 & ($ iparray [1]> = 16 & $ 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 {$ start + 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-> fp );
$ Ip = explode ('.', $ ip );
$ IpNum = $ ip [0] x 16777216 + $ ip [1] * 65536 + $ ip [2] * 256 + $ ip [3];
If (! ($ DataBegin = fread ($ this-> fp, 4) |! ($ DataEnd = fread ($ this-> 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 = $ ip2num = $ ip1num = 0;
$ IpAddr1 = $ ipAddr2 = '';
$ EndNum = $ ipAllNum;
While ($ ip1num> $ ipNum | $ ip2num <$ ipNum)
{
$ Middle = intval ($ EndNum + $ BeginNum)/2 );
Fseek ($ this-> fp, $ ipbegin + 7 * $ Middle );
$ IpData1 = fread ($ this-> fp, 4 );
If (strlen ($ ipData1) <4)
{
Fclose ($ this-> fp );
Return 'System error ';
}
$ Ip1num = implode ('', unpack ('l', $ ipData1 ));
If ($ ip1num <0) $ ip1num + = pow (2, 32 );
If ($ ip1num> $ ipNum)
{
$ EndNum = $ Middle;
Continue;
}
$ DataSeek = fread ($ this-> fp, 3 );
If (strlen ($ DataSeek) <3)
{
Fclose ($ this-> fp );
Return 'System error ';
}
$ DataSeek = implode ('', unpack ('l', $ DataSeek. chr (0 )));
Fseek ($ this-> fp, $ DataSeek );
$ IpData2 = fread ($ this-> fp, 4 );
If (strlen ($ ipData2) <4)
{
Fclose ($ this-> fp );
Return 'System error ';
}
$ Ip2num = implode ('', unpack ('l', $ ipData2 ));
If ($ ip2num <0) $ ip2num + = pow (2, 32 );
If ($ ip2num <$ ipNum)
{
If ($ Middle = $ BeginNum)
{
Fclose ($ this-> 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-> 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-> fp );
Return 'System error ';
}
$ IpFlag = fread ($ this-> fp, 1 );
If ($ ipFlag = chr (2 )){
$ AddrSeek2 = fread ($ this-> fp, 3 );
If (strlen ($ AddrSeek2) <3)
{
Fclose ($ this-> fp );
Return 'System error ';
}
$ AddrSeek2 = implode ('', unpack ('l', $ AddrSeek2.chr (0 )));
Fseek ($ this-> fp, $ AddrSeek2 );
}
Else
{
Fseek ($ this-> fp,-1, SEEK_CUR );
}
While ($ char = fread ($ this-> fp, 1 ))! = Chr (0 ))
$ IpAddr2. = $ char;
$ AddrSeek = implode ('', unpack ('l', $ AddrSeek. chr (0 )));
Fseek ($ this-> fp, $ AddrSeek );
While ($ char = fread ($ this-> fp, 1 ))! = Chr (0 ))
$ IpAddr1. = $ char;
}
Else
{
Fseek ($ this-> fp,-1, SEEK_CUR );
While ($ char = fread ($ this-> fp, 1 ))! = Chr (0 ))
$ IpAddr1. = $ char;
$ IpFlag = fread ($ this-> fp, 1 );
If ($ ipFlag = chr (2 ))
{
$ AddrSeek2 = fread ($ this-> fp, 3 );
If (strlen ($ AddrSeek2) <3)
{
Fclose ($ this-> fp );
Return 'System error ';
}
$ AddrSeek2 = implode ('', unpack ('l', $ AddrSeek2.chr (0 )));
Fseek ($ this-> fp, $ AddrSeek2 );
}
Else
{
Fseek ($ this-> fp,-1, SEEK_CUR );
}
While ($ char = fread ($ this-> fp, 1 ))! = Chr (0 ))
$ IpAddr2. = $ char;
}
If (preg_match ('/http/I', $ ipAddr2) $ ipAddr2 = '';
$ Ipaddr = "$ ipAddr1 $ ipAddr2 ";
$ 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.