Use pure qq ip database for fast IP address retrieval

Source: Internet
Author: User
Tags get ip
The pure version of IP data files can be used to optimize the query method, improve the format conversion efficiency, reduce the number of file reads, and quickly query IP addresses online. Greatly reduce the number of file reads, which should be beneficial to protecting the server's hard disk. Add a database browser to view the qq ip data files of the pure version. 1. use ip2long () for IP address verification. use sprintf for forced conversion to use the pure version of IP data files, optimize the query method, improve the format conversion efficiency, reduce the number of file reads, and quickly query IP addresses online. Greatly reduce the number of file reads, which should be beneficial to protecting the server's hard disk. Add a database browser to view the qq ip data files of the pure version.


1. use ip2long () for IP verification
2. use sprintf for forced conversion
3. unpack is used to analyze binary data, reducing the number of file reads
4. substr strpos analysis string, which reduces the number of file reads
5. Unfortunately, the speed has not improved significantly ;)


<-Form name = "ipform" method = "get" action = ""> <-br/> IP Address: <-input type = "text" name = "ip"/> <-br/> <-a href = "javascript: document. ipform. submit (); "> Submit <-/a> <-br/> <-/form>

Define ('ipdata', 'ipdata. db ');
// Get ip
$ Ip = isset ($ _ GET ['IP'])? $ _ GET ['IP']: getenv ("REMOTE_ADDR ");
Echo "IP: ARIN ";
If (-1 = ($ ip = ip2long ($ ip) die ("Invalid IP ");
$ Ip = sprintf ("% u", $ ip );
$ Country = $ city = '';
Echo"
Record No: ". Whois ($ ip, $ country, $ city );
Echo"
Location: [] ";

Function Whois ($ ip, & $ country, & $ city)
{
// Open ip-database
If (NULL = ($ fp = fopen (IPDATA, "rb") die ("cannot open dbfile .");
// Get data section offset
$ Offset = unpack ("Vbegin/Vend", fread ($ fp, 8 ));
$ Begin = 0; $ end = ($ offset ['end']-$ offset ['begin'])/7;
// Seek index
While ($ begin <$ end-1 ){
$ Num = ($ begin + $ end)> 1;
Fseek ($ fp, ($ offset ['begin'] + $ num * 7), SEEK_SET );
$ Record = unpack ("Vip", fread ($ fp, 4 ));
$ Record ['IP'] = sprintf ("% u", $ record ['IP']);
If ($ ip = $ record ['IP']) {$ begin = $ num; break ;}
If ($ ip> $ record ['IP']) $ begin = $ num;
Else $ end = $ num;
}
// Read index
$ Record = unpack ("Vbeginip/Vloc", ReadBinary ($ fp, $ offset ['begin'] + $ begin * 7 ));
$ Locoff = ($ record ['loc '] & 0x00FFFFFF );
$ Info = unpack ("Vendip/Vloc", ReadBinary ($ fp, $ locoff ));
$ Info ['enabled'] = sprintf ("% u", $ info ['enabled']);
If ($ info ['enabled'] <$ ip) die ("[unkown location]");
// Get GEO
$ Countryoff = ($ info ['loc '] & 0x000000FF) = 1 )? ($ Info ['loc ']> 8) :( $ locoff + 4 );
$ Country = ReadGEOStr ($ fp, $ countryoff );
$ City = ReadGEOStr ($ fp, $ countryoff );
Return $ begin;
}

Function ReadGEOStr ($ fp, & $ offset)
{
$ Binarydata = ReadBinary ($ fp, $ offset );
$ Info = unpack ("Vloc", $ binarydata );
If ($ info ['loc '] & 0x000000FF) = 2 ){
$ GeoStr = ReadBinary ($ fp, $ info ['loc ']> 8 );
$ GeoStr = substr ($ GeoStr, 0, strpos ($ GeoStr, 0 ));
$ Offset + = 4;
} Else {
$ Len = strpos ($ binarydata, 0 );
$ GeoStr = substr ($ binarydata, 0, $ len );
$ Offset + = $ len + 1;
}
Return $ GeoStr;
}

Function ReadBinary ($ fp, $ positionz)
{
Fseek ($ fp, $ positionz, SEEK_SET );
Return fread ($ fp, 32 );
}
?>
==========================================

2005-02-22
QQ database in cnss format
Integrated PHP browser queryer
Integrate official whois query http://www.mydot.org/t539.html






Define ('ipdata', 'ipdata. db ');

$ Fp = NULL;
If (isset ($ _ GET ['IP'])
{
$ Ipx = $ _ GET ['IP'];
If (-1 = ($ ip = ip2long ($ ipx) die ("Invalid IP ");
Echo "IP: ARIN ";
$ Ip = sprintf ("% u", $ ip );
$ Ippostion = '';
$ Start_time = get_time ();
Echo"
Record No: ". Whois ($ ippostion );
$ End_time = get_time ();
Echo"
Location :";
Echo"
Process Time: ". round ($ end_time-$ start_time, 3)." seconds ";
}
Else if (isset ($ _ GET ['s '])
{
$ First = $ _ GET ['s '];
$ Rcount = $ _ GET ['c'];
$ Startoff = $ end = 0;
GetSection ($ startoff, $ end );

If ($ I = $ first + $ rcount)> $ end | $ first <0)
Die ("count overflow ");
Else
{
$ J = $ first-$ rcount;
Echo "Prev :::";
$ J = $ first + $ rcount;
Echo "Next
";
}

While ($ first <$ I)
{
Echo ReadRecord ($ startoff, $ first, $ startip, $ endip );
$ Startip = long2ip ($ startip );
$ Endip = long2ip ($ endip );
Echo ":--
";
$ First ++;
}
Fclose ($ fp );
}
Else
{
Echo "Powered by upsdn.net ";
Echo"
Thanks to cz88.net ";
}



Function Whois (& $ country)
{
Global $ ip, $ fp;

$ Startoff = $ begin = $ end = 0;
GetSection ($ startoff, $ end );
// Seek index
While ($ begin <$ end-1 ){
$ Num = ($ begin + $ end)> 1;
Fseek ($ fp, $ startoff + $ num * 7, SEEK_SET );
$ Record = unpack ("Vip", fread ($ fp, 4 ));
$ Record ['IP'] = sprintf ("% u", $ record ['IP']);
If ($ ip = $ record ['IP']) {$ begin = $ num; break ;}
If ($ ip> $ record ['IP']) $ begin = $ num;
Else $ end = $ num;
}
// Read index
$ Startip = $ endip = 0;
$ Country = ReadRecord ($ startoff, $ begin, $ startip, $ endip );
Fclose ($ fp );
Return $ begin;
}

Function GetSection (& $ startoff, & $ total)
{
Global $ fp;
// Open ip-database
If (NULL = ($ fp = fopen (IPDATA, "rb") die ("cannot open dbfile .");
// Get data section offset
$ Offset = unpack ("Vbegin/Vend", fread ($ fp, 8 ));
$ Total = ($ offset ['end']-$ offset ['begin'])/7;
$ Startoff = $ offset ['begin'];
}

Function ReadRecord ($ startoff, $ number, & $ startip, & $ endip)
{
Global $ ip;
$ Record = unpack ("Vbeginip/Vloc", ReadBinary ($ startoff + $ number * 7 ));
$ Startip = $ record ['beginip'];
$ Locoff = ($ record ['loc '] & 0x00FFFFFF );
$ Info = unpack ("Vendip/Vloc", ReadBinary ($ locoff ));
$ Endip = $ info ['enabled'];
$ Info ['enabled'] = sprintf ("% u", $ info ['enabled']);
If ($ info ['enabled'] <$ ip) die ("[unkown location]");
// Get GEO
$ Countryoff = ($ info ['loc '] & 0x000000FF) = 1 )? ($ Info ['loc ']> 8) :( $ locoff + 4 );
Return ReadGEOStr ($ countryoff). "->". ReadGEOStr ($ countryoff );
}

Function ReadGEOStr (& $ offset)
{
$ Binarydata = ReadBinary ($ offset );
$ Info = unpack ("Vloc", $ binarydata );
If ($ info ['loc '] & 0x000000FF) = 2 ){
$ GeoStr = ReadBinary ($ info ['loc ']> 8 );
$ GeoStr = substr ($ GeoStr, 0, strpos ($ GeoStr, 0 ));
$ Offset + = 4;
} Else {
$ Len = strpos ($ binarydata, 0 );
$ GeoStr = substr ($ binarydata, 0, $ len );
$ Offset + = $ len + 1;
}
Return $ GeoStr;
}

Function ReadBinary ($ positionz)
{
Global $ fp;
Fseek ($ fp, $ positionz, SEEK_SET );
Return fread ($ fp, 32 );
}

Function get_time (){
$ Mtime = microtime ();
$ Mtime = explode ("", $ mtime );
$ Mtime = $ mtime [1] + $ mtime [0];
Return ($ mtime );
}
?>

Pure version IP database download: http://www.cz88.net/ip/

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.