thinkphp use Utfwry address library for IP location instance _php instance

Source: Internet
Author: User
Tags get ip domain name server

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 the IP address

If you just need access to the IP address of the user, then directly using the system built-in GET_CLIENT_IP function, the function is thinkphp standard mode built-in method, can be used directly, than PHP built-in system variables $_server[' Http_client_ IP '] has better compatibility, usage:

Copy Code 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 you want, you can also return IPV4 address numbers, for example:

Copy Code code as follows:
$ip = GET_CLIENT_IP (1);

The result returned might resemble the following:

Copy Code code as follows:
2130706433

Can be used for address ranges and comparisons.

Second, IP address positioning
Just getting the IP address does not fully meet the needs of the application, only recorded for future log analysis needs, the IP address positioning function allows you to get 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.
How to use:

Copy Code code as follows:
Import (' ORG.Net.IpLocation ');//Iplocation class
$Ip = new Iplocation (); Instantiating classes
$location = $Ip->getlocation (' 218.79.93.194 '); Get the location of an IP address

The returned location variable is an array, including:

Copy Code code as follows:
$location [' IP ']//IP address
$location [' Beginip ']//The start address of the User IP range
$location [' EndIP ']//End address of User IP range
$location [' country ']//country or region
$location [' area ']//region

In general, we need to take country and area information only if we want to get IP location:

Copy Code 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 file name when instantiating the Iplocation class, for example:

Copy Code 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:

Copy Code code as follows:
$info = Iconv (' GBK ', ' utf-8 ', $location [' Country ']. $location [' area ']);

If no arguments are passed in when the GetLocation method is invoked, the above Get_client_ip function is automatically invoked to obtain the current IP address:

Copy Code code as follows:
$location = $Ip->getlocation ();

You can also support incoming domain names to automatically obtain IP addresses

Copy Code code as follows:
Import (' ORG.Net.IpLocation ');//Iplocation class
$Ip = new Iplocation (); Instantiating classes
$area = $Ip->getlocation (' www.thinkphp.cn '); Get the location of the domain name server
Dump ($area);

Run result output:

If you are using a pure IP address library, or often need to transform different address libraries, in order to facilitate IP positioning of the query, you can also encapsulate a function to obtain location information, reference here: http://www.thinkphp.cn/code/88.html

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.