thinkphp use Utfwry address library for IP location instances

Source: Internet
Author: User
Tags extend get ip

  in Web applications, it is also a common requirement to locate and record related access logs based on IP addresses, and you can easily implement IP address acquisition and location in thinkphp

You can download the IP Location Extension class library at the http://www.thinkphp.cn/extend/223.html website, or download the extension package (http://www.thinkphp.cn/down/253.html) inside the extended class. If the Upload class library is downloaded separately, put the extracted IpLocation.class.php into the thinkphp/extend/library/org/net/(if not manually created) directory below.     First, get IP address   If you only need to obtain the IP address that the user accesses, then directly uses the system built-in GET_CLIENT_IP function, this function is the thinkphp standard mode built-in method, may use directly, $_server[' http_client_ip '] has better compatibility than PHP's built-in system variables, usage:   code as follows: $ip = Get_client_ip ();   GET_CLIENT_IP supports a variety of IP address detection and legality verification, the return value is the IP address obtained, if the IP address is obtained illegal, will return 0.0.0.0.   If necessary, you can return IPV4 address numbers, for example:     code as follows: $ip = GET_CLIENT_IP (1);   returned results may resemble:     copy code code as follows: 2130706433 can be used for address ranges and comparisons.     II, IP address positioning only to get the IP address can not fully meet the needs of the application, can only be recorded for future log analysis needs, IP address positioning function allows you to obtain the user's area. To use the IP positioning function, in addition to the need to iplocation extended class libraries, but also need IP address library files, because the thinkphp default UTF8 encoding, so it is best to UTF8 format IP address library files, if it is pure GBK encoded IP address library files, You need to encode the results (mentioned below) to download the UTF8 encoded address library file: http://www.thinkphp.cn/extend/270.html  The Unpacked address library file UTFWry.dat is placed under the directory where the Iplocation extension class library resides.   Usage:   Code as follows: Import (' ORG.Net.IpLocation ');//Iplocation Class $Ip = new Iplocation (); Instantiate class $location = $Ip->getlocation (' 218.79.93.194 '); Get the location of an IP address   The returned location variable is an array, including:     code as follows: $location [' IP ']//IP address $location [' beginip ']// The start address of the range where the user's IP is located $location [' EndIP ']//The end address of the user's IP range $location [' country ']//country or region $location [' area ']//region   Usually, we To obtain IP positioning, you only need to take country and area information:     Code as follows: $info =   $location [' Country ']. $location [' area '];   If you are using an IP address library file that is not UTFWry.dat (note that the file name needs to be consistent under Linux), we need to pass in the address library filename when instantiating the Iplocation class, for example:     Code as follows: $ Ip = new Iplocation (' MyIpWry.dat '); Incoming IP Address library file name   If your IP address library is GBK encoded, you need to encode the return result. For example: The   code is as follows: $info = Iconv (' GBK ', ' utf-8 ', $location [' Country ']. $location [' area ']);   If you call the GetLocation method without passing in any arguments, the system automatically invokes the GET_CLIENT_IP function above to obtain the current IP address:     Code as follows: $location = $Ip-> GetLocation ();   can also support incoming domain names to automatically obtain Ip address   code as follows: Import (' ORG.Net.IpLocation ');//Iplocation class $Ip = new Iplocation (); Instantiate class $area = $Ip->getlocation (' www.thinkphp.cn '); Get domainThe location where the name server is dump ($area);  

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.