thinkphp using Utfwry address library for IP locating instances _php tutorial

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 of the official website, or the extension class is already included in the download extension package (http://www.thinkphp.cn/down/253.html). 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 the PHP built-in system variables $_server[' Http_client_ IP '] has better compatibility, usage:
Copy the Code code as follows: $ip = Get_client_ip ();

GET_CLIENT_IP supports multiple cases of IP address detection and legitimacy verification, the return value is the IP address obtained, and if the IP address obtained is illegal, it will return 0.0.0.0.
If required, you can also return IPV4 address numbers, for example:
Copy the Code code as follows: $ip = GET_CLIENT_IP (1);

The returned results may resemble the following:
Copy the Code code as follows: 2130706433
Available for address ranges and comparisons.

Second, IP address positioning
Simply acquiring an IP address does not fully meet the needs of the application, it can only be recorded for future log analysis needs, IP address positioning allows you to obtain the user's area. To use the IP location feature, in addition to the need to iplocation extension class library, but also need IP address library files, because thinkphp by default UTF8 encoding, it is best to UTF8 format IP address library file, if it is pure GBK encoded IP address library file, You will need to encode the obtained results (as mentioned below), where you can download the UTF8 encoded address library file: http://www.thinkphp.cn/extend/270.html
The extracted address library file UTFWry.dat is placed under the directory where the Iplocation extension class library is located.
How to use:
Copy the code as follows: Import (' ORG.Net.IpLocation ');//Importing Iplocation class
$Ip = new Iplocation (); Instantiating classes
$location = $Ip->getlocation (' 218.79.93.194 '); Get where an IP address is located

The location variable returned is an array, including:
Copy the Code code as follows: $location [' IP ']//IP address
$location [' Beginip ']//start address of the range where the user IP is located
$location [' EndIP ']//End address of the range where the user IP is located
$location [' country ']//country or region
$location [' area ']//region

In general, if we want to obtain IP location, we only need to take country and area information:
Copy the code as follows: $info = $location [' Country ']. $location [' area '];

If the IP address library file you are using is not UTFWry.dat (note that the capitalization of filenames under Linux also needs to be consistent), we need to pass in the address library file name when instantiating the Iplocation class, for example:
Copy the 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 returned results. For example:
Copy the code as follows: $info = Iconv (' GBK ', ' utf-8 ', $location [' Country ']. $location [' area '];

If no arguments are passed when calling the GetLocation method, the system automatically calls the GET_CLIENT_IP function above to get the current IP address:
Copy the Code code as follows: $location = $Ip->getlocation ();

You can also support incoming domain names to get IP addresses automatically
Copy the code as follows: Import (' ORG.Net.IpLocation ');//Importing Iplocation class
$Ip = new Iplocation (); Instantiating classes
$area = $Ip->getlocation (' www.thinkphp.cn '); Get the location of the domain name server
Dump ($area);
Run the result output:

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

http://www.bkjia.com/PHPjc/748160.html www.bkjia.com true http://www.bkjia.com/PHPjc/748160.html techarticle You can download the IP Location Extension class library on the http://www.thinkphp.cn/extend/223.html of the official website, or download the extension pack (http://www.thinkphp.cn/down/253. HTML) also contains the extension class ...

  • Related Article

    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.