根据用户所在国家/地区重定向网站

时间:2013-02-26 11:31:26

标签: javascript cakephp

我想根据用户所在国家/地区重定向我的网站。例如,如果用户从美国输入www.domain.com,那么他应该重定向到en.domain.com。

2 个答案:

答案 0 :(得分:0)

您可以获得如下所述的位置:

IP to Location using Javascript

然后相应地重定向

答案 1 :(得分:0)

嗯,为了帮助您入门,以下是使用HTML5地理定位检测某人所在国家/地区的方法:

var geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': <YOURLATLNGRESPONSEFROMGEO>}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            if (results[0]) {
                var loc = getCountry(results);
            }
        }
    });

function getCountry(results)
{
    for (var i = 0; i < results[0].address_components.length; i++)
    {
    var shortname = results[0].address_components[i].short_name;
    var longname = results[0].address_components[i].long_name;
    var type = results[0].address_components[i].types;
    if (type.indexOf("country") != -1)
    {
        if (!isNullOrWhitespace(shortname))
        {
            return shortname;
        }
        else
        {
            return longname;
        }
    }
}

}

function isNullOrWhitespace(text) {
    if (text == null) {
        return true;
    }
    return text.replace(/\s/gi, '').length < 1;
}

这将获得该人的国家。不幸的是,我不知道如何使用它来重定向一个人。试试这个链接:http://dev.maxmind.com/geoip/geolite