Home  >  Article  >  Backend Development  >  About how ThinkPHP uses UTFWry address library for IP positioning

About how ThinkPHP uses UTFWry address library for IP positioning

不言
不言Original
2018-06-19 15:31:443158browse

In WEB applications, locating and recording related access logs based on IP addresses is also a very common requirement. In ThinkPHP, you can easily obtain and locate IP addresses.

You can download the IP positioning extension class on the official website library, or download the extension package which already contains the extension class. 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 the 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 directly used. Used, it has better compatibility than PHP's built-in system variable $_SERVER['HTTP_CLIENT_IP']. Usage:

$ip = get_client_ip();

get_client_ip supports multiple situation 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 needed, IPV4 address numbers can also be returned, for example:

$ip = get_client_ip(1);

The returned results may be similar to:

2130706433

Can be used for address ranges and comparisons.

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)
The decompressed address library file UTFWry.dat can be placed in the directory where the IpLocation extension class library is located.
Usage:

import('ORG.Net.IpLocation');// 导入IpLocation类
$Ip = new IpLocation(); // 实例化类
$location = $Ip->getlocation('218.79.93.194'); // 获取某个IP地址所在的位置

The returned location variable is an array, including:

$location['ip'] // IP地址
$location['beginip'] // 用户IP所在范围的开始地址
$location['endip'] // 用户IP所在范围的结束地址
$location['country'] // 所在国家或者地区
$location['area'] // 所在区域

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

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

$Ip = new IpLocation('MyIpWry.dat'); // 传入IP地址库文件名

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

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

$location = $Ip->getlocation();

It can also support passing in domain names to automatically Get the ip address

import('ORG.Net.IpLocation');// 导入IpLocation类
$Ip = new IpLocation(); // 实例化类
$area = $Ip->getlocation('www.thinkphp.cn'); // 获取域名服务器所在的位置
dump($area);

Running result output:

About how ThinkPHP uses UTFWry address library for IP positioning

If you are using the pure IP address library, or often need to change different address libraries, for convenience IP positioning queries can also be encapsulated in a separate function to obtain location information.

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

About the table method of ThinkPHP CURD method

THinkPHP obtains the client IP and IP address query method

The above is the detailed content of About how ThinkPHP uses UTFWry address library for IP positioning. For more information, please follow other related articles on the PHP Chinese website!

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