根据IP获取国家地区
1. Nginx配置
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
2.
public function getIpCountry(Request $request){//$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];$ip = getenv('HTTP_X_FORWARDED_FOR');//$ip = '211.112.242.163';$ipCountryCode = $this->toGetIpCountry($ip);return $this->respond($ipCountryCode);}protected function toGetIpCountry($ip){$countrys = [// [ 'cnName'=> '俄罗斯联邦', 'enName'=> 'Russian Federation', 'code'=> 'RU', 'number'=> '007' ],// [ 'cnName'=> '希腊', 'enName'=> 'Greece', 'code'=> 'GR', 'number'=> '0030' ],// [ 'cnName'=> '荷兰', 'enName'=> 'Netherlands', 'code'=> 'NL', 'number'=> '0031' ],// [ 'cnName'=> '比利时', 'enName'=> 'Belgium', 'code'=> 'BE', 'number'=> '0032' ],// [ 'cnName'=> '法国', 'enName'=> 'France', 'code'=> 'FR', 'number'=> '0033' ],// [ 'cnName'=> '西班牙', 'enName'=> 'Spain', 'code'=> 'ES', 'number'=> '0034' ],// [ 'cnName'=> '直布罗陀', 'enName'=> 'Gibraltar', 'code'=> 'GI', 'number'=> '00350' ],// [ 'cnName'=> '葡萄牙', 'enName'=> 'Portugal', 'code'=> 'PT', 'number'=> '00351' ],// [ 'cnName'=> '卢森堡', 'enName'=> 'Luxembourg', 'code'=> 'LU', 'number'=> '00352' ],// [ 'cnName'=> '爱尔兰', 'enName'=> 'Ireland', 'code'=> 'IE', 'number'=> '00353' ],// [ 'cnName'=> '冰岛', 'enName'=> 'Iceland', 'code'=> 'IS', 'number'=> '00354' ],// [ 'cnName'=> '阿尔巴尼亚', 'enName'=> 'Albania', 'code'=> 'AL', 'number'=> '00355' ],// [ 'cnName'=> '马耳他', 'enName'=> 'Malta', 'code'=> 'MT', 'number'=> '00356' ],// [ 'cnName'=> '安道尔', 'enName'=> 'Andorra', 'code'=> 'AD', 'number'=> '00376' ],// [ 'cnName'=> '芬兰', 'enName'=> 'Finland', 'code'=> 'FI', 'number'=> '00358' ],// [ 'cnName'=> '保加利亚', 'enName'=> 'Bulgaria', 'code'=> 'BG', 'number'=> '00359' ],// [ 'cnName'=> '匈牙利', 'enName'=> 'Hungary', 'code'=> 'HU', 'number'=> '0036' ],// [ 'cnName'=> '德国', 'enName'=> 'Germany', 'code'=> 'DE', 'number'=> '0049' ],// //[ 'cnName'=> '南斯拉夫', 'enName'=> '66666', 'code'=> '8888888', 'number'=> '00381' ],// [ 'cnName'=> '意大利', 'enName'=> 'Italy', 'code'=> 'IT', 'number'=> '0039' ],// [ 'cnName'=> '圣马力诺', 'enName'=> 'San Marino', 'code'=> 'SM', 'number'=> '00378' ],// [ 'cnName'=> '梵蒂冈', 'enName'=> 'Vatican City (The Holy See)', 'code'=> 'VA', 'number'=> '00390' ],// [ 'cnName'=> '罗马尼亚', 'enName'=> 'Romania', 'code'=> 'RO', 'number'=> '0040' ],// [ 'cnName'=> '瑞士', 'enName'=> 'Switzerland', 'code'=> 'CH', 'number'=> '0041' ],// [ 'cnName'=> '列支敦士登', 'enName'=> 'Liechtenstein', 'code'=> 'LI', 'number'=> '00423' ],// [ 'cnName'=> '奥地利', 'enName'=> 'Austria', 'code'=> 'AT', 'number'=> '0043' ],// [ 'cnName'=> '英国', 'enName'=> 'Great Britain (United Kingdom; England)', 'code'=> 'GB', 'number'=> '0044' ],// [ 'cnName'=> '丹麦', 'enName'=> 'Denmark', 'code'=> 'DK', 'number'=> '0045' ],// [ 'cnName'=> '瑞典', 'enName'=> 'Sweden', 'code'=> 'SE', 'number'=> '0046' ],// [ 'cnName'=> '挪威', 'enName'=> 'Norway', 'code'=> 'NO', 'number'=> '0047' ],// [ 'cnName'=> '波兰', 'enName'=> 'Poland', 'code'=> 'PL', 'number'=> '0048' ],// [ 'cnName'=> '捷克', 'enName'=> 'Czech Republic', 'code'=> 'CZ', 'number'=> '00420' ],// [ 'cnName'=> '斯洛伐克', 'enName'=> 'Slovakia', 'code'=> 'SK', 'number'=> '00421' ],// [ 'cnName'=> '摩纳哥', 'enName'=> 'Monaco', 'code'=> 'MC', 'number'=> '00377' ],// [ 'cnName'=> '马其顿', 'enName'=> 'Republic of Macedonia (FYROM)', 'code'=> 'MK', 'number'=> '00389' ],// /* [ 'cnName'=> '科罗地亚', 'enName'=> '66666', 'code'=> '8888888', 'number'=> '00385' ],// [ 'cnName'=> '斯洛文尼亚', 'enName'=> '66666', 'code'=> '8888888', 'number'=> '00386' ],// [ 'cnName'=> '波斯尼亚和塞哥维那', 'enName'=> '66666', 'code'=> '8888888', 'number'=> '00387' ], */// [ 'cnName'=> '亚美尼亚共和国', 'enName'=> 'Armenia', 'code'=> 'AM', 'number'=> '00374' ],// [ 'cnName'=> '白俄罗斯共和国', 'enName'=> 'Belarus', 'code'=> 'BY', 'number'=> '00375' ],// [ 'cnName'=> '格鲁吉亚共和国', 'enName'=> 'Georgia', 'code'=> 'GE', 'number'=> '00995' ],// [ 'cnName'=> '哈萨克斯坦共和国', 'enName'=> 'Kazakhstan', 'code'=> 'KZ', 'number'=> '007' ],// [ 'cnName'=> '吉尔吉斯斯坦共和国', 'enName'=> 'Kyrgyzstan', 'code'=> 'KG', 'number'=> '00996' ],// [ 'cnName'=> '乌兹别克斯坦共和国', 'enName'=> 'Uzbekistan', 'code'=> 'UZ', 'number'=> '00998' ],// [ 'cnName'=> '塔吉克斯坦共和国', 'enName'=> 'Tajikistan', 'code'=> 'TJ', 'number'=> '00992' ],// [ 'cnName'=> '土库曼斯坦共和国', 'enName'=> 'Turkmenistan', 'code'=> 'TM', 'number'=> '00993' ],// [ 'cnName'=> '乌克兰', 'enName'=> 'Ukraine', 'code'=> 'UA', 'number'=> '00380' ],// [ 'cnName'=> '立陶宛', 'enName'=> 'Lithuania', 'code'=> 'LT', 'number'=> '00370' ],// [ 'cnName'=> '拉脱维亚', 'enName'=> 'Latvia', 'code'=> 'LV', 'number'=> '00371' ],// [ 'cnName'=> '爱沙尼亚', 'enName'=> 'Estonia', 'code'=> 'EE', 'number'=> '00372' ],// [ 'cnName'=> '摩尔多瓦', 'enName'=> 'Moldova', 'code'=> 'MD', 'number'=> '00373' ],// [ 'cnName'=> '阿塞拜疆', 'enName'=> 'Azerbaijan', 'code'=> 'AZ', 'number'=> '00994' ],['cnName' => '南非', 'enName' => 'South Africa', 'code' => 'ZA', 'number' => '00710'],// [ 'cnName'=> '印度', 'enName'=> 'India', 'code'=> 'IN', 'number'=> '0090' ],];// $ip='155.93.224.89';$result = file_get_contents("http://ip-api.com/json/" . $ip);$arr = json_decode($result, true);if ($arr && isset($arr['countryCode'])) {$country_code = $arr['countryCode'];return $country_code;foreach ($countrys as $country) {if(in_array($arr['countryCode'], $country)) {return 'za';}else {return 'gb';}}} else {$result = file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip);$arr = json_decode($result, true);if ($arr && $arr['geoplugin_countryCode']) {$country_code = $arr['geoplugin_countryCode'];return $country_code;foreach ($countrys as $country) {if (in_array($arr['geoplugin_countryCode'], $country)) {return 'za';} else {return 'gb';}}} else {$result = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=" . $ip);$arr = json_decode($result, true);foreach ($countrys as $country) {$country_code = $arr['data']['country_id'];return $country_code;if (in_array($arr['data']['country_id'], $country) || in_array($arr['data']['country'], $country)) {return 'za';} else {return 'gb';}}}}}
3. https://dev.maxmind.com/geoip/geoip2/geolite2/
根据IP获取国家地区相关推荐
- PHP 通过ip获取国家及地址方法集合
一.通过http请求淘宝和新浪的接口进行在线查询 优点:调用简单方便代码量少 缺点:在无网络的情况下无法查询. print_r(get_area('223.67.235.28')); functi ...
- 【java】使用ip获取 国家 省份 城市
1.下载GeoLite2 City库 下载地址: http://dev.maxmind.com/geoip/geoip2/geolite2/ https://download.csdn.net/dow ...
- 百度API根据ip获取对应地区
1.ak 2.代码 import com.alibaba.fastjson.JSON; import com.common.util.http.HttpClientUtils; import org. ...
- GeoIP2数据库——根据ip确定国家/地区
由于GEOIP官方的原免费GeoLite Legacy数据库(.dat)已于2019年1月开始不再提供更新和支持. 而网上的90%的教程还是老教程, 就想着和大家分享一下. 项目中原本使用的是纯真数据 ...
- GeoIP2数据库——根据ip确定国家/地区,过滤某区域ip
GeoIP2下载:https://dev.maxmind.com/geoip/geoip2/downloadable/ GeoIP2数据库使用例子: from django.conf import s ...
- GeoIP获取IP的所在地区
1.下载文件 官网地址:https://dev.maxmind.com/?lang=en 最下方从Products→GeoLite2 Free Geolocation Data 进入 进入之后,按照登 ...
- 使用php根据ip数据库获得国家地区并统计国家使用量排名
使用file_get_contents调取第三方接口,动不动报错 需要在本地安装ip数据库 https://www.ipip.net/product/client.html csv文件内容一行有2列, ...
- 引用Geoip实现由IP地址获取国家城市等信息
需求 当我们知道用户ip的情况下,需要统计用户所属的国家/省份/城市等信息. 这时可以用开源的Geoip2 避免重复造轮子. github地址:https://github.com/maxmind/G ...
- PHP 跟据用户IP获取所在国家高效解决方案(GEOIP)
最近项目中有一个需求统计访客数据,为了保证效率.前端尽量轻量化,仅将访客原始请求信息不作任何处理直接写入消息队列.后端计划任务服务器监听消息队列,解析 user agent, ip 地址,处理结果写入 ...
- 实践:nginx代理,通过使用GeoIp模块获取访问者IP及访问地区信息
目录 前言 一.在Linux中创建存放nginx模块文件夹 存在就不需要再次创建. 将所有nginx额外模块包都放到这个目录下 二.配置host文件代理,避免访问github太慢(已配置了跳过) 三. ...
最新文章
- HDU 5693 D Game 区间dp
- kaggle较好的竞赛经验
- HTML5 编辑 API 之 Range 对象(一)
- android addView的使用
- BZOJ1433 ZJOI2009 假期的宿舍 二分图匹配
- python异步回调实现原理_快速理解Python异步编程的基本原理
- java统计大串中小串的出现次数_统计大串中小串出现的次数
- 提高sql性能的方法_三种提高T-SQL性能的方法
- mysql show slave_MySQL show slave status 参考
- Flutter实战之顶部导航栏以及ListView简单应用
- dw虚线横线的代码_虚线代码大全
- WSO2 ESB / EI / AM 汉化方法
- Hexo 设置博客背景图片 (NexT 主题 )
- 使用CamlBuilder生成CAML查询语句
- 关于图文识别功能相关技术的大致实现
- 数商云B2B分销商城系统:助力企业加快分销速度,构建高效智能B2B网上分销平台
- 五、Hive数据仓库(完整版)
- 北大青鸟---不怎么样
- eNSP配置静态路由实例
- 在Linux下搭建 miniGUI mStudio环境