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;
}
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.