当前位置:网站首页>Multilingual settings of php website (IP address distinguishes domestic and foreign)
Multilingual settings of php website (IP address distinguishes domestic and foreign)
2022-07-31 02:40:00 【Yixi Muze】
Preface: Originally, I thought that I would have peace of mind by making a language switch button, but suddenly the above said that I would like to have one that distinguishes between domestic and foreign, and that the access in China will be displayed in Chinese, and the access in foreign countries will be displayed in English.All right!Look for information.
tp5, the requirement is switch by button, choose reference 3;
Method 1: IP query network
Code implementation (refer to 3 for complete)
// Multilingual Set think_val custom to customLangfunction lang(){$lang = input('lang');if(!$lang){// Default: think_varif(Cookie::has("customLang")){$lang = Cookie::get("customLang");}else{// $lang = "zh-cn";$ip = request() -> ip(0,true);$datatype = 'json';$url = 'https://api.ip138.com/ip/?ip='.$ip.'&datatype='.$datatype;$header = array('token: your token');$obj = $this->getRequest($url,$header);$objson = json_decode($obj,true);$isAddress = $objson['data'][0];if($isAddress == "China"){$lang = "zh-cn";}else{$lang = "en-us";}}}$lang = Lang::range($lang);//Set the current languageLang::load(THINK_PATH.'lang'.DS.$lang.EXT,$lang);//Load the current language packCookie::set('customLang',$lang);return $lang;}
Method 2:
maxmind GeoIP : Register an account with your email address and log in to your account, create a key, the name is like a note, it will generate an ID and key, remember to save it, it will only be displayed once;
1. After logging in, you can download a data package. The data package is updated every Tuesday:
2. Get code support:
1. Installation:
composer require geoip2/geoip2:~2.0
2. To download the database, you need to register an email account to log in:
https://www.maxmind.com/en/accounts/722009/geoip/downloads
3. Code implementation: use GeoIp2\Database\Reader;
function lang(){$lang = input('lang');if(!$lang){if(Cookie::has("lenze_lang")){$lang = Cookie::get("lenze_lang");}else{$ip = request() -> ip(0,true);// maxmind gets the judgment$reader = new Reader('./public/static/GeoLite2-Country.mmdb'); // decompressed packet$record = $reader->country($ip);$isAddress = $record->country->isoCode;if($isAddress == "cn" || $isAddress == "CN"){$lang = "zh-cn";}else{$lang = "en-us";}}}$lang = Lang::range($lang);//Set the current languageLang::load(THINK_PATH.'lang'.DS.$lang.EXT,$lang);//Load the current language packCookie::set('lenze_lang',$lang);return json(['lang'=>$lang]);}
Method three:
You can use the IP location of AutoNavi map, however, the IP address that AutoNavi needs to search (only supports domestic), just, the search result is judged as domestic, and the search data is empty is foreign.
IP Location-API Documentation-Development Guide-Web Service API | AutoNavi Map API
This one has development documentation, so I don't need to nag and do useless work, quack!
Reference:
2, tp5 multi-language switching 版权声明
本文为[Yixi Muze]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/212/202207310233430265.html
边栏推荐
猜你喜欢
Huawei od dice js
f.grid_sample
10 权限介绍
真正的CTO,是一个懂产品的技术人
Observer mode (1)
Fiddler captures packets to simulate weak network environment testing
Mathematics to solve the problem - circular linked list
公司官网建站笔记(六):域名进行公安备案并将备案号显示在网页底部
The application of AI in the whole process of medical imaging equipment
Installation, start and stop of redis7 under Linux
随机推荐
coldfusion8 background scheduled tasks take shell
多线程下类对象的服务承诺探讨
Installation, start and stop of redis7 under Linux
关于 mysql8.0数据库中主键位id,使用replace插入id为0时,实际id插入后自增导致数据重复插入 的解决方法
YOLOV5 study notes (3) - detailed explanation of network module
vlan间路由+静态路由+NAT(PAT+静态NAT)综合实验
General introduction to the Unity interface
汉源高科8路HDMI综合多业务高清视频光端机8路HDMI视频+8路双向音频+8路485数据+8路E1+32路电话+4路千兆物理隔离网络
YOLOV5学习笔记(三)——网络模块详解
静态路由解析(最长掩码匹配原则+主备路由)
mysql index
16. Registration Center-consul
ShardingJDBC usage summary
User interaction + formatted output
基于opencv实现人脸检测
TCP/IP四层模型
LeetCode Daily Question 2022/7/25-2022/7/31
CMOS和TTL的区别?
SQL注入 Less47(报错注入) 和Less49(时间盲注)
英特尔软硬优化,赋能东软加速智慧医疗时代到来