当前位置:网站首页>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 ...
边栏推荐
- Mb10m-asemi rectifier bridge mb10m
- word 退格键删除不了选中文本,只能按delete
- Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
- TCP擁塞控制詳解 | 3. 設計空間
- Interviewer: how does the JVM allocate and recycle off heap memory
- NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
- Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
- Register in PHP_ Globals parameter settings
- Everyone in remote office works together to realize cooperative editing of materials and development of documents | community essay solicitation
- Nifi from introduction to practice (nanny level tutorial) - flow
猜你喜欢
近视:摘镜or配镜?这些问题必须先了解清楚
[statement] about searching sogk1997 and finding many web crawler results
TCP拥塞控制详解 | 3. 设计空间
What is the maximum number of concurrent TCP connections for a server? 65535?
The mixlab editing team is recruiting teammates~~
Mysql 单表字段重复数据取最新一条sql语句
Basis of target detection (IOU)
Mb10m-asemi rectifier bridge mb10m
Slam learning notes - build a complete gazebo multi machine simulation slam from scratch (II)
QT串口ui设计和解决显示中文乱码
随机推荐
在ntpdate同步时间的时候出现“the NTP socket is in use, exiting”
NFT new opportunity, multimedia NFT aggregation platform okaleido will be launched soon
相同切入点的抽取
Zebras are recognized as dogs, and Stanford found the reason why AI made mistakes
Thread pool executes scheduled tasks
Processing strategy of message queue message loss and repeated message sending
程序猿如何快速成长
First knowledge of database
远程办公之大家一同实现合作编辑资料和开发文档 | 社区征文
Golang 匿名函数使用
面试官:JVM如何分配和回收堆外内存
Expression of request header in different countries and languages
Explore Cassandra's decentralized distributed architecture
Pointcut expression
特征多项式与常系数齐次线性递推
《天天数学》连载56:二月二十五日
Mb10m-asemi rectifier bridge mb10m
QT串口ui设计和解决显示中文乱码
疫情常态化大背景下,关于远程办公的思考|社区征文
Golang anonymous function use