当前位置:网站首页>php 网站的多语言设置(IP地址区分国内国外)
php 网站的多语言设置(IP地址区分国内国外)
2022-07-31 02:33:00 【逸曦穆泽】
序:本来,以为做一个语言切换的按钮就可以一直安枕无忧了,突然上头说要来一个区分国内与国外的,在国内访问的显示中文,在国外访问的显示英文。好吧!找一下资料看看。
tp5,需求是按钮切换的,选择参考3;
方法一:IP查询网

代码实现( 参考3有完整的)
// 多语言 将 think_val 自定义设置为 customLang
function lang(){
$lang = input('lang');
if(!$lang){
// 默认:think_var
if(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:你的token');
$obj = $this-> getRequest($url,$header);
$objson = json_decode($obj,true);
$isAddress = $objson['data'][0];
if($isAddress == "中国"){
$lang = "zh-cn";
}else{
$lang = "en-us";
}
}
}
$lang = Lang::range($lang);//设定当前语言
Lang::load(THINK_PATH.'lang'.DS.$lang.EXT,$lang);//加载当前语言包
Cookie::set('customLang',$lang);
return $lang;
}方法二:
maxmind GeoIP :先使用邮箱注册一个账号,并登录账号,创建一个密钥,名称就像一个备注,它会生成一个ID和密钥,记得保存好,它只显示一次;
1、登录后就可以下载一个数据包了,数据包是每周二更新的:


2、获取代码支持:


1、安装:
composer require geoip2/geoip2:~2.02、下载数据库,需要注册邮箱账号登录:
https://www.maxmind.com/en/accounts/722009/geoip/downloads
3、代码实现: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 获取判断
$reader = new Reader('./public/static/GeoLite2-Country.mmdb'); // 解压的数据包
$record = $reader->country($ip);
$isAddress = $record->country->isoCode;
if($isAddress == "cn" || $isAddress == "CN"){
$lang = "zh-cn";
}else{
$lang = "en-us";
}
}
}
$lang = Lang::range($lang);//设定当前语言
Lang::load(THINK_PATH.'lang'.DS.$lang.EXT,$lang);//加载当前语言包
Cookie::set('lenze_lang',$lang);
return json(['lang'=>$lang]);
}方法三:
可以使用高德地图的IP定位,不过,高德需要搜索的IP地址(仅支持国内),刚好,搜索得到的就判断为国内,搜索数据为空的为国外。
IP定位-API文档-开发指南-Web服务 API | 高德地图API
这个有开发文档了,就不用我多唠叨和做无用功了,嘎嘎!
参考:
1、最新多种方式, 判断客户端IP是国内还是国外?_解忧杂货铺Q的博客-CSDN博客_判断ip是国内还是国外
边栏推荐
- Intranet Infiltration - Privilege Escalation
- try-catch中含return
- 怎样做好一个创业公司CTO?
- Problems that need to be solved by the tcp framework
- Huawei od dice js
- Validate XML documents
- SQL注入 Less46(order by后的注入+rand()布尔盲注)
- Project development software directory structure specification
- Drools基本介绍,入门案例,基本语法
- 图解lower_bound&upper_bound
猜你喜欢

tcp框架需要解决的问题

What have I experienced to become a tester who is harder than development?

基于FPGA的售货机

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

uniapp uses 3rd party fonts

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

934. The Shortest Bridge

力扣刷题之爬楼梯(7/30)

f.grid_sample

The effective square of the test (one question of the day 7/29)
随机推荐
leetcode-128: longest continuous sequence
19. Support Vector Machines - Intuitive Understanding of Optimization Objectives and Large Spacing
uniapp uses 3rd party fonts
mysql 索引
Software testing basic interface testing - getting started with Jmeter, you should pay attention to these things
Introduction to flask series 】 【 flask - using SQLAlchemy
全流程调度——MySQL与Sqoop
【AcWing 62nd Weekly Game】
Software Testing Defect Reporting - Definition, Composition, Defect Lifecycle, Defect Tracking Post-Production Process, Defect Tracking Process, Purpose of Defect Tracking, Defect Management Tools
LeetCode 每日一题 2022/7/25-2022/7/31
The effective square of the test (one question of the day 7/29)
Inner monologue from a female test engineer...
Classic linked list OJ strong training problem - fast and slow double pointer efficient solution
cudaMemcpy学习笔记
tcp框架需要解决的问题
golang GUI for nuxui — HelloWorld
FPGA-based vending machine
Basic introduction to ShardingJDBC
What are the project management tools like MS Project
mmdetection trains a model related command