Php query the region (pure ip database) by ip address _ PHP Tutorial

Source: Internet
Author: User
Tags ip location information unpack
Php queries the region (pure ip database) based on the ip address ). Pure ip library download? PhpclassIpLocation {*** @ varresource pointer * private $ fp; *** offset address of the first IP record * @ varint * private $ firstip; * ** download the last IP record from the pure ip library

 Fp = 0; if ($ this-> fp = @ fopen ($ filename, 'RB '))! = False) {$ this-> firstip = $ this-> getlong (); $ this-> lastip = $ this-> getlong (); $ this-> totalip = ($ this-> lastip-$ this-> firstip)/7 ;}} /*** return the number of long integers read * @ access private * @ return int */public function getlong () {// Convert the four bytes of the read little-endian encoding into the number of long integers $ result = unpack ('vlong', fread ($ this-> fp, 4 )); return $ result ['long'];}/*** return the number of long integers read in three bytes ** @ access private * @ return int */public function Getlong3 () {// Convert the 3 bytes of the read little-endian encoding to the long integer $ result = unpack ('vlong', fread ($ this-> fp, 3 ). chr (0); return $ result ['long'];} /*** return the compressed ip address ** @ access private * @ param string $ ip * @ return string */public function packip ($ ip) {// Convert the IP address to a long integer. If an IP address error occurs in PHP5, False is returned. // intval converts Flase to an integer-1, then compress the data into the big-endian encoded string return pack ('N', intval (ip2long ($ ip);}/*** returns the read string ** @ access pr Ivate * @ param string $ data * @ return string */public function getstring ($ data = "") {$ char = fread ($ this-> fp, 1 ); while (ord ($ char)> 0) {// string is saved in C format and ended with \ 0 $ data. = $ char; // connect the read characters to the given string $ char = fread ($ this-> fp, 1);} return mb_convert_encoding ($ data, 'utf-8', 'gb2312');}/*** return region information ** @ access private * @ return string */public function getarea () {$ byte = fread ($ this-> fp, 1); // flag Byte switch (ord ($ byte) {case 0: // no region information $ area = ""; break; case 1: case 2: // The flag byte is 1 or 2, indicating that the region information is redirected to fseek ($ this-> fp, $ this-> getlong3 ()); $ area = $ this-> getstring (); break; default: // otherwise, the region information is not redirected $ area = $ this-> getstring ($ byte ); break;} return $ area ;} /*** return region information based on the given IP address or domain name * @ access public * @ param string $ ip * @ return array */function getlocation ($ ip) {if (! $ This-> fp) return null; // if the data file is not properly opened, a blank $ location ['IP'] = gethostbyname ($ ip) is returned ); // Convert the entered domain name to an ip address $ ip = $ this-> packip ($ location ['IP']); // Convert the entered IP address to a comparable IP address // an invalid IP address is converted to 255.255.255.255.255 // You can search for $ l = 0; // search for the bottom boundary $ u = $ this-> totalip; // search for the top boundary $ findip = $ this-> lastip; // if not found, return the last IP record (QQWry. dat version information) while ($ l <= $ u) {// when the boundary is smaller than the bottom boundary, the query fails. $ I = floor ($ l + $ u) /2); // calculate the approximate intermediate record fseek ($ th Is-> fp, $ this-> firstip + $ I * 7); $ beginip = strrev (fread ($ this-> fp, 4 )); // Obtain the starting IP address of the intermediate record. // The strrev function is used to convert the compressed IP address of little-endian to the format of big-endian. // This function is used for comparison. If ($ ip <$ beginip) {// when the user's IP address is smaller than the starting ip address of the intermediate record, $ u = $ I-1; // change the upper boundary of the search to an intermediate record minus one} else {fseek ($ this-> fp, $ this-> getlong3 ()); $ endip = strrev (fread ($ this-> fp, 4); // Obtain the end ip address of the intermediate record if ($ ip> $ endip) {// when the user's IP address is greater than the end IP address of the intermediate record, $ l = $ I + 1; // change the bottom boundary of the search to an intermediate record plus} else {// when the user's IP address is within the IP range of the intermediate record $ findip = $ this-> firstip + $ I * 7; break; // indicates finding the result and exiting the loop} // obtain the IP location information fseek ($ this-> fp, $ findip); $ location [' Beginip'] = long2ip ($ this-> getlong (); // start address of the user's IP address range $ offset = $ this-> getlong3 (); fseek ($ this-> fp, $ offset); $ location ['enabled'] = long2ip ($ this-> getlong ()); // end address of the user's IP address range $ byte = fread ($ this-> fp, 1); // Mark byte switch (ord ($ byte) {case 1: // The flag byte is 1, indicating that the country and region information is redirected at the same time $ countryOffset = $ this-> getlong3 (); // redirect address fseek ($ this-> fp, $ countryOffset); $ byte = fread ($ this-> fp, 1); // Mark byte switch (ord ($ Byte) {case 2: // The flag byte is 2, indicating that the country information is redirected to fseek ($ this-> fp, $ this-> getlong3 ()); $ location ['country'] = $ this-> getstring (); fseek ($ this-> fp, $ countryOffset + 4 ); $ location ['region'] = $ this-> getarea (); break; default: // otherwise, the country information is not Redirected. $ location ['country'] = $ this-> getstring ($ byte); $ location ['region'] = $ this-> getarea (); break;} break; case 2: // indicates that the country information is redirected to fseek ($ this-> fp, $ this-> getlong3 (); $ l Ocation ['country'] = $ this-> getstring (); fseek ($ this-> fp, $ offset + 8 ); $ location ['region'] = $ this-> getarea (); break; default: // otherwise, the country information is not Redirected. $ location ['country'] = $ this-> getstring ($ byte); $ location ['region'] = $ this-> getarea (); break;} if ($ location ['country'] = "CZ88.NET") {// CZ88.NET indicates no valid information. $ location ['country'] = "unknown ";} if ($ location ['region'] = "CZ88.NET") {$ location ['region'] = "";} Return $ location;}/*** destructor, used to automatically close opened files after Page execution. **/Function _ desctruct () {if ($ this-> fp) {fclose ($ this-> fp );} $ this-> fp = 0 ;}}?>

This can also be downloaded from the Internet or copied here, which is also comprehensive.

Execution file

 Getlocation ("ip address"); // Obtain the region getlocation ("ip region") return $ ipresult;} print_r ($ getIpPlace () based on the ip address; // call method?>

Download ghost? Phpclass IpLocation {/*** @ var resource pointer */private $ fp;/*** offset address of the first IP record * @ var int */private $ firstip; /*** The last IP record...

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.