Home > Backend Development > PHP Tutorial > ThinkPHP uses UTFWry address library for IP positioning example_PHP tutorial

ThinkPHP uses UTFWry address library for IP positioning example_PHP tutorial

WBOY
Release: 2016-07-13 10:34:53
Original
893 people have browsed it

You can download the IP positioning extension class library at http://www.thinkphp.cn/extend/223.html on the official website, or download the expansion package (http://www.thinkphp.cn/down/253.html) This extension class is already included. If the uploaded class library is downloaded separately, put the decompressed IpLocation.class.php under the ThinkPHP/Extend/Library/ORG/Net/ (please create it manually if it does not exist) directory.

1. Obtain IP address

If you only need to obtain the IP address visited by the user, then directly use the system's built-in get_client_ip function. This function is a built-in method in ThinkPHP standard mode and can be used directly. It is better than PHP's built-in system variable $_SERVER['HTTP_CLIENT_IP' ] has better compatibility, usage:

Copy code The code is as follows:
$ip = get_client_ip();

get_client_ip supports various detection and legality verification of IP addresses. The return value is the obtained IP address. If the obtained IP address is illegal, 0.0.0.0 will be returned.
If necessary, you can also return the IPV4 address number, for example:

Copy code The code is as follows:
$ip = get_client_ip(1 );

The returned result may be similar to:

Copy the code The code is as follows:
2130706433

can be used for address ranges and compare.

2. IP address positioning
Merely obtaining the IP address cannot fully meet the needs of the application. It can only be recorded for future log analysis needs. The IP address positioning function allows you to obtain the user's area. To use the IP location function, in addition to the IpLocation extension class library, you also need an IP address library file. Since ThinkPHP uses UTF8 encoding by default, it is best to use an IP address library file in UTF8 format. If it is a pure gbk encoded IP address library file, you need to perform encoding conversion on the obtained results (mentioned below). You can download the UTF8 encoded address library file here: http://www.thinkphp.cn/extend/270.html
Decompressed address The library file UTFWry.dat can be placed under the directory where the IpLocation extension class library is located.
Usage:

Copy code The code is as follows:
import('ORG.Net.IpLocation');// Import IpLocation Class
$Ip = new IpLocation(); // Instantiate class
$location = $Ip->getlocation('218.79.93.194'); // Get the location of an IP address

The location variable returned is an array, including:

Copy code The code is as follows:
$location['ip'] / / IP address
$location['beginip'] // The starting address of the user IP range
$location['endip'] // The end address of the user IP range
$location['country' ] // Country or region
$location['area'] // Region

Usually, if we want to get IP positioning, we only need to get the country and area information:

Copy the code The code is as follows:
$ info = $location['country'].$location['area'];

If the IP address library file you use is not UTFWry.dat (note that the case of the file name 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 code The code is as follows:
$Ip = new IpLocation('MyIpWry.dat'); // Pass in the IP address library file name

If your IP address library is encoded in GBK, you need to perform encoding conversion on the returned result. For example:

Copy code The code is as follows:
$info = iconv('gbk','utf-8',$location[' country'].$location['area']);

If no parameters are passed in when calling the getlocation method, the system will automatically call the get_client_ip function above to obtain the current IP address:

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

You can also pass in a domain name to automatically obtain an IP address

Copy the code The code is as follows:
import('ORG.Net.IpLocation'); // Import IpLocation class
$Ip = new IpLocation(); // Instantiate class
$area = $Ip->getlocation('www .thinkphp.cn'); // Get the location of the domain name server
dump($area);

Operating result output:

ThinkPHP uses UTFWry address library for IP positioning example_PHP tutorial

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/748160.htmlTechArticleYou can download the IP positioning extension class at http://www.thinkphp.cn/extend/223.html on the official website library, or download the extension package (http://www.thinkphp.cn/down/253.html) which already contains this extension class...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template