【发布时间】:2015-04-27 17:40:46
【问题描述】:
我的公司有几个不同的域,最简单的是 .com 到 .ca 站点。我们如何创建一个根据用户位置更改域的系统。例如:如果来自美国的访问者在 google 上找到了我们的 .ca 网站,我该如何将他们重定向到 .com?
谢谢!
【问题讨论】:
-
您是否已经拥有某种类型的地理定位功能来识别访问者的来源?
标签: php wordpress geolocation
我的公司有几个不同的域,最简单的是 .com 到 .ca 站点。我们如何创建一个根据用户位置更改域的系统。例如:如果来自美国的访问者在 google 上找到了我们的 .ca 网站,我该如何将他们重定向到 .com?
谢谢!
【问题讨论】:
标签: php wordpress geolocation
也许使用 html5 地理定位和谷歌地图 api 和 window.location.replace 来重定向可能是一个解决方案。
window.onload = function () {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(fnPosition);
}
}
function fnPosition(position) {
var lat = position.coords.latitude;
var long = position.coords.longitude;
checkCountry(lat,long);
}
function checkCountry(lat,long) {
var country = {};
var latlng = new google.maps.LatLng(lat, long);
geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': latlng}, function(results) {
if (results[1]) {
for (var i=0; i<results[0].address_components.length; i++) {
for (var b=0;b<results[0].address_components[i].types.length;b++) {
if (results[0].address_components[i].types[b] == "country") {
country =results[0].address_components[i];
break;
}
}
}
}
fnRedirect(country);
});
}
function fnRedirect(country) {
switch(country.short_name) {
case 'CA':
// some window window.location.replace(...)
break;
default:
// whatever
}
}
我用这个例子从位置here获取信息
在php中,可以根据ip获取位置。
$ip = $_SERVER['REMOTE_ADDR'];
$details = json_decode(file_get_contents("http://ipinfo.io/{$ip}/json"));
switch ($details->country) {
case 'CA':
header('Location: http://www.example.com/');
exit;
break;
default:
#whatever
break;
}
PHP 示例here
【讨论】: