Javascript 根据位置(IP 地址)将用户重定向到不同的网站
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13857734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Redirect User to different Website Based on Location (IP Address)
提问by Vishnudev K
How to redirect user visiting my sites to location specific sites using the IP Address.
I have 3 location specific sites.
1. Europe.
2. US.
3. Asia.
In short
I wanted to find the location of the client using his IP Address. The solution needs to use Google APIs.
如何使用 IP 地址将访问我的站点的用户重定向到特定位置的站点。
我有 3 个位置特定的站点。
1. 欧洲。
2. 美国。
3. 亚洲。
简而言之,我想使用他的 IP 地址找到客户端的位置。该解决方案需要使用 Google API。
回答by Vishnudev K
I got the solution using "google.loader.ClientLocation"
here is the code for that if somebody needs it.
我使用“google.loader.ClientLocation”得到了解决方案,
这里是如果有人需要它的代码。
Check this JSFiddle
检查这个 JSFiddle
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Get web visitor's location</title>
<meta name="robots" value="none" />
</head>
<body>
<div id="yourinfo"></div>
<script type="text/javascript" src="http://www.google.com/jsapi?key=ABQIAAAAp04yNttlQq-7b4aZI_jL5hQYPm-xtd00hTQOC0OXpAMO40FHAxQMnH50uBbWoKVHwgpklyirDEregg"></script>
<script type="text/javascript">
var Continent = {"AD":"Europe","AE":"Asia","AF":"Asia","AG":"North America","AI":"North America","AL":"Europe","AM":"Asia","AN":"North America","AO":"Africa","AQ":"Antarctica","AR":"South America","AS":"Australia","AT":"Europe","AU":"Australia","AW":"North America","AZ":"Asia","BA":"Europe","BB":"North America","BD":"Asia","BE":"Europe","BF":"Africa","BG":"Europe","BH":"Asia","BI":"Africa","BJ":"Africa","BM":"North America","BN":"Asia","BO":"South America","BR":"South America","BS":"North America","BT":"Asia","BW":"Africa","BY":"Europe","BZ":"North America","CA":"North America","CC":"Asia","CD":"Africa","CF":"Africa","CG":"Africa","CH":"Europe","CI":"Africa","CK":"Australia","CL":"South America","CM":"Africa","CN":"Asia","CO":"South America","CR":"North America","CU":"North America","CV":"Africa","CX":"Asia","CY":"Asia","CZ":"Europe","DE":"Europe","DJ":"Africa","DK":"Europe","DM":"North America","DO":"North America","DZ":"Africa","EC":"South America","EE":"Europe","EG":"Africa","EH":"Africa","ER":"Africa","ES":"Europe","ET":"Africa","FI":"Europe","FJ":"Australia","FK":"South America","FM":"Australia","FO":"Europe","FR":"Europe","GA":"Africa","GB":"Europe","GD":"North America","GE":"Asia","GF":"South America","GG":"Europe","GH":"Africa","GI":"Europe","GL":"North America","GM":"Africa","GN":"Africa","GP":"North America","GQ":"Africa","GR":"Europe","GS":"Antarctica","GT":"North America","GU":"Australia","GW":"Africa","GY":"South America","HK":"Asia","HN":"North America","HR":"Europe","HT":"North America","HU":"Europe","ID":"Asia","IE":"Europe","IL":"Asia","IM":"Europe","IN":"Asia","IO":"Asia","IQ":"Asia","IR":"Asia","IS":"Europe","IT":"Europe","JE":"Europe","JM":"North America","JO":"Asia","JP":"Asia","KE":"Africa","KG":"Asia","KH":"Asia","KI":"Australia","KM":"Africa","KN":"North America","KP":"Asia","KR":"Asia","KW":"Asia","KY":"North America","KZ":"Asia","LA":"Asia","LB":"Asia","LC":"North America","LI":"Europe","LK":"Asia","LR":"Africa","LS":"Africa","LT":"Europe","LU":"Europe","LV":"Europe","LY":"Africa","MA":"Africa","MC":"Europe","MD":"Europe","ME":"Europe","MG":"Africa","MH":"Australia","MK":"Europe","ML":"Africa","MM":"Asia","MN":"Asia","MO":"Asia","MP":"Australia","MQ":"North America","MR":"Africa","MS":"North America","MT":"Europe","MU":"Africa","MV":"Asia","MW":"Africa","MX":"North America","MY":"Asia","MZ":"Africa","NA":"Africa","NC":"Australia","NE":"Africa","NF":"Australia","NG":"Africa","NI":"North America","NL":"Europe","NO":"Europe","NP":"Asia","NR":"Australia","NU":"Australia","NZ":"Australia","OM":"Asia","PA":"North America","PE":"South America","PF":"Australia","PG":"Australia","PH":"Asia","PK":"Asia","PL":"Europe","PM":"North America","PN":"Australia","PR":"North America","PS":"Asia","PT":"Europe","PW":"Australia","PY":"South America","QA":"Asia","RE":"Africa","RO":"Europe","RS":"Europe","RU":"Europe","RW":"Africa","SA":"Asia","SB":"Australia","SC":"Africa","SD":"Africa","SE":"Europe","SG":"Asia","SH":"Africa","SI":"Europe","SJ":"Europe","SK":"Europe","SL":"Africa","SM":"Europe","SN":"Africa","SO":"Africa","SR":"South America","ST":"Africa","SV":"North America","SY":"Asia","SZ":"Africa","TC":"North America","TD":"Africa","TF":"Antarctica","TG":"Africa","TH":"Asia","TJ":"Asia","TK":"Australia","TM":"Asia","TN":"Africa","TO":"Australia","TR":"Asia","TT":"North America","TV":"Australia","TW":"Asia","TZ":"Africa","UA":"Europe","UG":"Africa","US":"North America","UY":"South America","UZ":"Asia","VC":"North America","VE":"South America","VG":"North America","VI":"North America","VN":"Asia","VU":"Australia","WF":"Australia","WS":"Australia","YE":"Asia","YT":"Africa","ZA":"Africa","ZM":"Africa","ZW":"Africa"};
if(google.loader.ClientLocation)
{
visitor_lat = google.loader.ClientLocation.latitude;
visitor_lon = google.loader.ClientLocation.longitude;
visitor_city = google.loader.ClientLocation.address.city;
visitor_region = google.loader.ClientLocation.address.region;
visitor_country = google.loader.ClientLocation.address.country;
visitor_countrycode = google.loader.ClientLocation.address.country_code;
if(visitor_countrycode!= null)
{
var Cont = Continent[visitor_countrycode];
//alert(Cont);
}
//
document.getElementById('yourinfo').innerHTML = '<p>Lat/Lon: ' + visitor_lat + ' / ' + visitor_lon + '</p><p>Location: ' + visitor_city + ', ' + visitor_region +', Continent : ' + Cont + ', ' + visitor_country + ' (' + visitor_countrycode + ')</p>';
}
else
{
document.getElementById('yourinfo').innerHTML = '<p>Whoops!</p>';
}
</script>
</body>
回答by Nir
There are two options:
有两种选择:
A backend solution, using Maxmind GeoIP DLL, an IP to country converter: http://dev.maxmind.com/geoip/downloadable.
A frontend solution (The one I know is not free but pretty cheap: http://www.maxmind.com/en/javascript)
后端解决方案,使用 Maxmind GeoIP DLL,IP 到国家/地区转换器:http://dev.maxmind.com/geoip/downloadable 。
一个前端解决方案(我知道的那个不是免费的,但是很便宜:http: //www.maxmind.com/en/javascript)
Both allow you to transform an IP to country, then you can aggregate country to location.
两者都允许您将 IP 转换为国家/地区,然后您可以将国家/地区聚合到位置。
Once you have the location (Europe, US, Asia) you can then redirect to the appropriate site.
获得位置(欧洲、美国、亚洲)后,您可以重定向到相应的站点。
回答by SriSree
Dont know If this answers your problem, I wanted to try this using Python, though its been really long time this Question was posted. just to see If I'm thinking in the right way :)
不知道这是否能解决您的问题,我想使用 Python 尝试此操作,尽管发布此问题已经很长时间了。只是想看看我是否以正确的方式思考:)
I've a weblog, with most frequent visitors from an IP, I've checked the IP and it belongs to a Consulting company. Finally after through analysis I've 3 different IPs of a Educational Institute, Product Dev company & another consulting company constituting about ~30% each. I wanted to deploy a User specific Page and test it. I was not able to do it for some reasons. Anway sharing is caring.
我有一个博客,最常访问者来自 IP,我检查了 IP,它属于一家咨询公司。最后,经过分析,我拥有教育机构、产品开发公司和另一家咨询公司的 3 个不同 IP,各占约 30%。我想部署一个特定于用户的页面并对其进行测试。由于某些原因,我无法做到这一点。安威分享是关怀。
first get IP,
先拿到IP,
import socket
user_ip= gethostbyname(gethostname())
IP_cons=xxx # you can also set IP range in the form of a list
IP_edu =yyy
IP_cons2=zzz
if user_ip ==IP_cons: # user_ip in IP_cons if lsit of IPs
<redirect to IP_cons.html>
elif user_ip == IP_edu:
redirect to IP_edu.html>
elif user_ip == IP_cons2:
redirect to IP_cons2.html>
else:
home.html
Thanks.
谢谢。
回答by M Hanif
A possible solution could be as explain here https://medium.com/@ipapi/automatically-redirect-to-country-specific-page-f687e514595e
一个可能的解决方案可以在这里解释 https://medium.com/@ipapi/automatically-redirect-to-country-specific-page-f687e514595e
It will return you country code based on visitor's IP, you can then write your redirection logic based on it.
它会根据访问者的 IP 返回您的国家/地区代码,然后您可以根据它编写重定向逻辑。

