当前位置:网站首页>utfwry. Dat PHP, about ThinkPHP's method of IP location using utfwry address Library
utfwry. Dat PHP, about ThinkPHP's method of IP location using utfwry address Library
2022-07-03 16:35:00 【Leaf drop traceless 123】
stay WEB Application , according to IP Address location and logging are also very common requirements , stay ThinkPHP You can easily achieve IP Address acquisition and location
It can be downloaded from the official website IP Locate the extended class library , Or the extension class is already included in the download extension package . If it is an uploaded class library downloaded separately , Take the decompressed IpLocation.class.php Put in ThinkPHP/Extend/Library/ORG/Net/( If not, please manually create ) Below directory .
One 、 obtain IP Address
If you only need to get user access IP Address , Then directly use the built-in get_client_ip Function , The function is ThinkPHP Standard mode built-in method , You can use it directly , Than PHP Built in system variables $_SERVER['HTTP_CLIENT_IP'] Better compatibility , usage :$ip = get_client_ip();
get_client_ip Support IP Address detection and legitimacy verification , The return value is obtained IP Address , If I get it IP Illegal address , Will return 0.0.0.0.
if necessary , You can also return IPV4 Address number , for example :$ip = get_client_ip(1);
The result returned may be similar to :2130706433
It can be used for address range and comparison .
Two 、IP Address location
Just get IP Address can not fully meet the needs of applications , It can only be recorded for future log analysis ,IP The address location function allows you to get the user's area . To use IP Positioning function , Except for the need to IpLocation Beyond the extended class library , It also needs to be IP Address library file , because ThinkPHP By default UTF8 code , So it's better to UTF8 Format IP Address library file , If it is pure gbk Coded IP Address library file , You need to encode and convert the obtained results ( As we'll see )
Unzipped address library file UTFWry.dat Put in IpLocation Under the directory where the extension class library is located .
Usage method :import('ORG.Net.IpLocation');// Import IpLocation class
$Ip = new IpLocation(); // Instantiate the class
$location = $Ip->getlocation('218.79.93.194'); // Get a IP The location of the address
Back to location Variable is an array , Include :$location['ip'] // IP Address
$location['beginip'] // user IP The starting address of the range
$location['endip'] // user IP The end address of the range
$location['country'] // Country or region
$location['area'] // Area
Usually , We want to get IP Positioning words , Just take country and area Information :$info = $location['country'].$location['area'];
If you use it IP The address library file is not UTFWry.dat( Pay attention to Linux The case of the following file names also needs to be consistent ) Words , We need to instantiate IpLocation Class, pass in the address library file name , for example :$Ip = new IpLocation('MyIpWry.dat'); // Pass in IP Address library file name
If your IP Address library is GBK Code words , You need to encode and convert the returned results . for example :$info = iconv('gbk','utf-8',$location['country'].$location['area']);
If the getlocation Method without passing any parameters , The system will automatically call the above get_client_ip Function to get the current IP Address :$location = $Ip->getlocation();
You can also support the incoming domain name to automatically obtain ip Address import('ORG.Net.IpLocation');// Import IpLocation class
$Ip = new IpLocation(); // Instantiate the class
$area = $Ip->getlocation('www.thinkphp.cn'); // Get the location of the domain name server
dump($area);
Run result output :

If you use innocence IP Address base , Or you often need to change different address libraries , For convenience IP Locate the query , You can also package a function separately to obtain location information .
The above is the whole content of this paper , I hope it will be helpful for your study , For more information, please pay attention to PHP Chinese net !
Related to recommend :
Related resources :UTFWry.dat(ThinkPHPIP Locate the address library file )_lsDefaultItemFilter- Other ...
边栏推荐
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (I)
- PHP中register_globals参数设置
- Thread pool executes scheduled tasks
- Uploads labs range (with source code analysis) (under update)
- NSQ源码安装运行过程
- Preventing/catching “IllegalArgumentException: parameter must be a descendant of this view” error
- 近视:摘镜or配镜?这些问题必须先了解清楚
- 高等数学(第七版)同济大学 习题2-1 个人解答
- 一台服务器最大并发 tcp 连接数多少?65535?
- Initial test of scikit learn Library
猜你喜欢

NFT新的契机,多媒体NFT聚合平台OKALEIDO即将上线

Two sides of the evening: tell me about the bloom filter and cuckoo filter? Application scenario? I'm confused..

MySQL converts comma separated attribute field data from column to row

一台服务器最大并发 tcp 连接数多少?65535?

Initial test of scikit learn Library

Add color to the interface automation test framework and realize the enterprise wechat test report

Mysql 将逗号隔开的属性字段数据由列转行

Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (I)

0214-27100 a day with little fluctuation

Cocos Creator 2. X automatic packaging (build + compile)
随机推荐
How to set up SVN server on this machine
Preventing/catching “IllegalArgumentException: parameter must be a descendant of this view” error
【剑指 Offer】58 - II. 左旋转字符串
近视:摘镜or配镜?这些问题必须先了解清楚
Custom plug-in construction and use of QT plug-in
The word backspace key cannot delete the selected text, so you can only press Delete
2022 love analysis · panoramic report of digital manufacturers of state-owned enterprises
PHP secondary domain name session sharing scheme
LeetCode1491. Average value of wages after removing the minimum wage and the maximum wage
消息队列消息丢失和消息重复发送的处理策略
在ntpdate同步时间的时候出现“the NTP socket is in use, exiting”
Unreal_ Datatable implements ID self increment and sets rowname
中南大学|通过探索理解: 发现具有深度强化学习的可解释特征
特征多项式与常系数齐次线性递推
Svn usage specification
[combinatorics] summary of combinatorial identities (eleven combinatorial identities | proof methods of combinatorial identities | summation methods)*
深入理解 SQL 中的 Grouping Sets 语句
0214-27100 a day with little fluctuation
Hibernate的缓存机制/会话级缓存机制
(Supplement) double pointer topic