当前位置:网站首页>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.02. 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
边栏推荐
- The modification is not properly placed in the sandbox, causing Apple compatibility issues
- Detailed explanation of STP election (step + case)
- SQL注入 Less46(order by后的注入+rand()布尔盲注)
- multiplayer-hlap 包有问题,无法升级的解决方案
- BAT can't sell "Medical Cloud": Hospitals flee, mountains stand, and there are rules
- [1154] How to convert string to datetime
- 6、显示评论和回复
- To write good test cases, you must first learn test design
- AI在医疗影像设备全流程应用
- 字体压缩神器font-spider的使用
猜你喜欢

Pythagorean tuple od js

汉源高科8路HDMI综合多业务高清视频光端机8路HDMI视频+8路双向音频+8路485数据+8路E1+32路电话+4路千兆物理隔离网络

Live Preview | KDD2022 Doctoral Dissertation Award Champion and Runner-up Dialogue

Basic introduction to ShardingJDBC

SQL注入 Less54(限制次数的SQL注入+union注入)

ShardingJDBC使用总结

Inter-vlan routing + static routing + NAT (PAT + static NAT) comprehensive experiment

图解lower_bound&upper_bound

STP选举(步骤+案列)详解

STM32CUBEMX develops GD32F303 (11) ---- ADC scans multiple channels in DMA mode
随机推荐
软件积累 -- 截图软件ScreenToGif
Mathematical Ideas in AI
Unity3D Button 鼠标悬浮进入与鼠标悬浮退出按钮事件
try-catch中含return
局域网电脑硬件信息收集工具
【银行系列第一期】中国人民银行
ShardingJDBC使用总结
Installation, start and stop of redis7 under Linux
6、显示评论和回复
Introduction and use of Drools WorkBench
Maximum area of solar panel od js
YOLOV5学习笔记(二)——环境安装+运行+训练
跨专业考研难度大?“上岸”成功率低?这份实用攻略请收下!
关于 mysql8.0数据库中主键位id,使用replace插入id为0时,实际id插入后自增导致数据重复插入 的解决方法
4、敏感词过滤(前缀树)
How to do a startup CTO?
mysql index
Manchester City confuses fans with smart scarf that detects emotions
The application of AI in the whole process of medical imaging equipment
tcp框架需要解决的问题