About thinkphp using Utfwry address library for IP location

Source: Internet
Author: User
Tags get ip domain name server
In Web applications, locating and logging related access logs based on IP addresses is also a common requirement, and in thinkphp you can easily implement IP address acquisition and positioning

You can download the IP Location Extension class library on your website, or the extension is already included in the download extension package. 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:

$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:

$ip = GET_CLIENT_IP (1);

The returned results may resemble the following:

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 need to encode the results of the fetch (as mentioned below)
The extracted address library file UTFWry.dat is placed under the directory where the Iplocation extension class library is located.
How to use:

Import (' ORG.Net.IpLocation ');//Importing iplocation class $ip = new Iplocation (); Instantiate class $location = $Ip->getlocation (' 218.79.93.194 '); Get where an IP address is located

The location variable returned is an array, including:

$location [' IP ']//IP address $location[' Beginip '//The 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 ']/ $location[' area '//region in your country or region

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

$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:

$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:

$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:

$location = $Ip->getlocation ();

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

Import (' ORG.Net.IpLocation ');//Importing iplocation class $ip = new Iplocation (); Instantiate class $area = $Ip->getlocation (' www.thinkphp.cn '); Get the domain Name server location 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.

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.