PHP based on IP query region (pure IP Library) _php tutorial

Source: Internet
Author: User
Tags fread unpack
Pure IP Library Download

< span="">

 fp = 0;            if ($this-&GT;FP = @fopen ($filename, ' RB ')!== false) {$this->firstip = $this->getlong ();            $this->lastip = $this->getlong ();        $this->totalip = ($this->lastip-$this->firstip)/7; }}/** * Returns the read number of long integers * @access private * @return int */Public Function Getlong () {//Littl to be read        E-endian encoded 4 bytes into a long integer $result = unpack (' Vlong ', fread ($this-&GT;FP, 4));    return $result [' Long ']; }/** * Returns the length of 3 bytes Read * * @access private * @return int */Public Function Getlong3 () {//will read        The Little-endian encoded 3 bytes into a long integer $result = unpack (' Vlong ', fread ($this-&GT;FP, 3). chr (0));    return $result [' Long ']; /** * Returns the compressed IP address that can be compared * * @access private * @param string $ip * @return String */Public Functio        n Packip ($IP) {//IP address is converted to a long integer, if the IP address is incorrect in PHP5, return false,///Then Intval convert flase to integer-1, then compress into Big-endian encoded string ReturnPack (' N ', Intval (Ip2long ($IP))); }/** * Returns the Read String * * @access private * @param string $data * @return String */Public function get        String ($data = "") {$char = Fread ($this->fp, 1); while (Ord ($char) > 0) {//string is saved in C format and ends with $data. = $char;//Connect prompt the read word after the given string $char = Fread ($t        HIS-&GT;FP, 1);    } return Mb_convert_encoding ($data, ' utf-8 ', ' gb2312 '); }/** * Returns area information * * @access private * @return String */Public Function Getarea () {$byte = Fre AD ($this-&GT;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 fseek ($this->fp, $this->getlong3 ());            $area = $this->getstring ();            Break            Default://Otherwise, indicates that the zone information is not redirected $area = $this->getstring ($byte);        Break } RETUrn $area; /** * Returns the region information according to the given IP address or domain name * @access public * @param string $IP * @return Array */function Getloc ation ($IP) {if (! $this-&GT;FP) return null;//If the data file is not opened correctly, return empty $location [' ip '] = gethostbyname ($IP); /convert the entered domain name to an IP address $ip = $this->packip ($location [' IP ']); Converting the IP address entered into a comparable IP address//Illegal IP address will be converted to 255.255.255.255//Sub-search $l = 0; Bottom boundary $u of search = $this->totalip; Top boundary of search $findip = $this->lastip;  If not found, returns the last IP record (version information of QQWry.Dat) while ($l <= $u) {//when the upper boundary is less than the lower boundary, the lookup fails $i = Floor (($l + $u)/2);            Calculate approximate intermediate records fseek ($this->fp, $this->firstip + $i * 7); $beginip = Strrev (fread ($this-&GT;FP, 4));            Get the start IP address of the intermediate record//The Strrev function here is to convert the Little-endian's compressed IP address into a Big-endian format//For comparison, and later the same.                if ($ip < $beginip) {//user's IP is less than the start IP address of the intermediate record $u = $i-1;//change the upper boundary of the search to intermediate record minus one}else{Fseek ($this->fp, $this->getlong3 ()); $endip = Strrev (fread ($this-&GT;FP, 4)); Gets the end IP address of the intermediate record if ($ip > $endip) {//The user's IP is greater than the end IP address of the intermediate record $l = $i + 1;//The bottom boundary of the search is modified                    Add a}else{//For intermediate record//The IP of the user is within the IP range of the intermediate record $findip = $this->firstip + $i * 7; Break        To find the result, exit the Loop}}}//Get the IP geolocation information found fseek ($this->fp, $findip); $location [' beginip '] = Long2ip ($this->getlong ());        The start address of the range where the user IP is located $offset = $this->getlong3 ();        Fseek ($this->fp, $offset); $location [' endip '] = Long2ip ($this->getlong ()); End address of the range where the user IP is located $byte = fread ($this->fp, 1); Flag byte switch (ORD ($byte)) {Case 1://Flag byte 1, indicating that both country and region information are redirected at the same time $countryOffset = $this-& Gt;getlong3 ();                REDIRECT Address fseek ($this->fp, $countryOffset); $byte = Fread ($this->fp, 1);          Flag byte      Switch (ord ($byte)) {Case 2://Flag Byte is 2, indicating that the country information is redirected fseek ($this->fp, $t                        His->getlong3 ());                        $location [' country '] = $this->getstring ();                        Fseek ($this->fp, $countryOffset + 4);                        $location [' area '] = $this->getarea ();                    Break                        Default://Otherwise, indicates that the country information is not redirected $location [' country '] = $this->getstring ($byte);                        $location [' area '] = $this->getarea ();                Break            } break;                Case 2://The flag byte is 2, indicating that the country information is redirected fseek ($this->fp, $this->getlong3 ());                $location [' country '] = $this->getstring ();                Fseek ($this->fp, $offset + 8);                $location [' area '] = $this->getarea ();            Break Default://Otherwise, indicates that the country information is not redirected $location [' country '] = $this->getString ($byte);                $location [' area '] = $this->getarea ();        Break } if ($location [' country '] = = "Cz88.net") {//CZ88.        NET means no valid information $location [' country '] = "Unknown";        if ($location [' area '] = = "Cz88.net") {$location [' area '] = "";    } return $location;    The/** * destructor is used to automatically close open files after the page execution ends.        * */function __desctruct () {if ($this->fp) {fclose ($this-&GT;FP);    } $this->fp = 0;  }}?>

This can also be downloaded online, you can copy here, here is also very full.

Execute file

 
  GetLocation ("IP Address"); Obtain the Region getlocation ("IP region") return $ipresult according to the IP address; Print_r ($getIpPlace ());//Call Method?>

http://www.bkjia.com/PHPjc/440118.html www.bkjia.com true http://www.bkjia.com/PHPjc/440118.html techarticle Pure IP library download? phpclass iplocation {/** * @var resource pointer */private $FP;/** * Offset address for first IP record * @var int */Private $first ip /** * 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.