如何使用Maxmind Geoip2 Javascript根据国家/地区重定向流量
我买了Maxmind GeoIP2一段时间,我想做一个简单的重定向基于国家,但似乎没有明确的答案如何做到这一点,即使在他们的网站上。我不擅长编码,但我正在努力理解它是如何工作的 例如,我的网站是google.com,我想将一些国家重定向到yahoo.com,我想出了这个代码,但它将所有人重定向到yahoo.com。有人能帮我或重建这个脚本,使它的目的工作吗?非常感谢如何使用Maxmind Geoip2 Javascript根据国家/地区重定向流量,javascript,redirect,maxmind,Javascript,Redirect,Maxmind,我买了Maxmind GeoIP2一段时间,我想做一个简单的重定向基于国家,但似乎没有明确的答案如何做到这一点,即使在他们的网站上。我不擅长编码,但我正在努力理解它是如何工作的 例如,我的网站是google.com,我想将一些国家重定向到yahoo.com,我想出了这个代码,但它将所有人重定向到yahoo.com。有人能帮我或重建这个脚本,使它的目的工作吗?非常感谢 var user_country=geoip2.country( 功能(响应){ 如果(response.country.is
var user_country=geoip2.country(
功能(响应){
如果(response.country.iso_code==“FR”){window.location=“http://yahoo.com"}
else{window.location=”http://google.com"}
如果(response.country.iso_code==“BE”){window.location=”http://yahoo.com"}
else{window.location=”http://google.com"}
如果(response.country.iso_code==“PH”){window.location=“http://yahoo.com"}
else{window.location=”http://google.com"}
},
函数(错误){
//处理错误
}
);
试试这个,它应该可以工作
<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
<script language="JavaScript">
geoip2.country(
function (response) {
if (response.country.iso_code == "FR") {
window.location = "http://yahoo.com"
}
if (response.country.iso_code == "BE") {
window.location = "http://yahoo.com"
}
if (response.country.iso_code == "PH") {
window.location = "http://yahoo.com"
}
else {
window.location = "http://google.com"
}
}
);
</script>
geoip2.1国家(
功能(响应){
如果(response.country.iso_代码==“FR”){
window.location=”http://yahoo.com"
}
如果(response.country.iso_代码==“BE”){
window.location=”http://yahoo.com"
}
如果(response.country.iso_代码==“PH”){
window.location=”http://yahoo.com"
}
否则{
window.location=”http://google.com"
}
}
);
试试这个,它应该可以工作
<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
<script language="JavaScript">
geoip2.country(
function (response) {
if (response.country.iso_code == "FR") {
window.location = "http://yahoo.com"
}
if (response.country.iso_code == "BE") {
window.location = "http://yahoo.com"
}
if (response.country.iso_code == "PH") {
window.location = "http://yahoo.com"
}
else {
window.location = "http://google.com"
}
}
);
</script>
geoip2.1国家(
功能(响应){
如果(response.country.iso_代码==“FR”){
window.location=”http://yahoo.com"
}
如果(response.country.iso_代码==“BE”){
window.location=”http://yahoo.com"
}
如果(response.country.iso_代码==“PH”){
window.location=”http://yahoo.com"
}
否则{
window.location=”http://google.com"
}
}
);
试试这个,它应该可以工作
<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
<script language="JavaScript">
geoip2.country(
function (response) {
if (response.country.iso_code == "FR") {
window.location = "http://yahoo.com"
}
if (response.country.iso_code == "BE") {
window.location = "http://yahoo.com"
}
if (response.country.iso_code == "PH") {
window.location = "http://yahoo.com"
}
else {
window.location = "http://google.com"
}
}
);
</script>
geoip2.1国家(
功能(响应){
如果(response.country.iso_代码==“FR”){
window.location=”http://yahoo.com"
}
如果(response.country.iso_代码==“BE”){
window.location=”http://yahoo.com"
}
如果(response.country.iso_代码==“PH”){
window.location=”http://yahoo.com"
}
否则{
window.location=”http://google.com"
}
}
);
试试这个,它应该可以工作
<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
<script language="JavaScript">
geoip2.country(
function (response) {
if (response.country.iso_code == "FR") {
window.location = "http://yahoo.com"
}
if (response.country.iso_code == "BE") {
window.location = "http://yahoo.com"
}
if (response.country.iso_code == "PH") {
window.location = "http://yahoo.com"
}
else {
window.location = "http://google.com"
}
}
);
</script>
geoip2.1国家(
功能(响应){
如果(response.country.iso_代码==“FR”){
window.location=”http://yahoo.com"
}
如果(response.country.iso_代码==“BE”){
window.location=”http://yahoo.com"
}
如果(response.country.iso_代码==“PH”){
window.location=”http://yahoo.com"
}
否则{
window.location=”http://google.com"
}
}
);
我基于此创建了一个更复杂的版本。您需要使用geoip2javascript客户端API(上述解决方案也是如此!)。玩得开心
<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
<script language="JavaScript">
var redirect = (function () {
/* Get actual URL */
var url = window.location.href;
/* This implements the actual redirection. */
var redirectBrowser = function (site) {
var uri = "https://www.example.com/" + site;
window.location = uri;
};
/* These are the country codes for the countries we have sites for.
* We will check to see if a visitor is coming from one of these countries.
* If they are, we redirect them to the country-specific site. If not, we
* redirect them to https://www.example.com/ */
var sites = {
"us": {"active": true, "target": "en-us"},
"gb": {"active": true, "target": "en-gb"},
"de": {"active": true, "target": "de"},
"fr": {"active": true, "target": "fr"},
"fi": {"active": true, "target": "fi"},
"hu": {"active": true, "target": "hu"},
"nl": {"active": true, "target": "nl"},
"se": {"active": true, "target": "sv"}
};
var defaultSite = "";
var onSuccess = function (geoipResponse) {
/* There's no guarantee that a successful response object
* has any particular property, so we need to code defensively. */
if (!geoipResponse.country.iso_code) {
redirectBrowser(defaultSite);
return;
}
/* ISO country codes are in upper case. */
var code = geoipResponse.country.iso_code.toLowerCase();
if ( sites[code].active ) {
redirectBrowser(sites[code].target);
}
else if ( url == defaultSite ) {
return;
}
else {
redirectBrowser(defaultSite);
}
};
/* We don't really care what the error is, we'll send them
* to the default site. */
var onError = function (error) {
redirectBrowser(defaultSite);
};
return function () {
geoip2.country( onSuccess, onError );
};
}());
redirect();
</script>
var redirect=(函数(){
/*获取实际URL*/
var url=window.location.href;
/*这实现了实际的重定向*/
var redirectBrowser=功能(站点){
var uri=”https://www.example.com/“+现场;
window.location=uri;
};
/*这些是我们网站所在国家的国家代码。
*我们将检查是否有游客来自这些国家之一。
*如果是,我们将其重定向到特定国家/地区的网站。如果不是,我们将
*将它们重定向到https://www.example.com/ */
变量站点={
“us”:{“active”:true,“target”:“en-us”},
“gb”:{“active”:true,“target”:“en-gb”},
“de”:{“active”:true,“target”:“de”},
“fr”:{“active”:true,“target”:“fr”},
“fi”:{“active”:true,“target”:“fi”},
“胡”:{“主动”:正确,“目标”:“胡”},
“nl”:{“活动”:真,“目标”:“nl”},
“se”:{“active”:true,“target”:“sv”}
};
var defaultSite=“”;
var onSuccess=函数(geoipResponse){
/*不能保证一个成功的响应对象
*具有任何特定属性,因此我们需要编写防御代码*/
if(!geoipResponse.country.iso_代码){
重定向浏览器(默认站点);
返回;
}
/*ISO国家代码为大写*/
var code=geoipsresponse.country.iso_code.toLowerCase();
如果(站点[代码]。活动){
重定向浏览器(站点[代码].目标);
}
else if(url==defaultSite){
返回;
}
否则{
重定向浏览器(默认站点);
}
};
/*我们真的不在乎错误是什么,我们会发送给他们
*到默认站点*/
var onError=函数(错误){
重定向浏览器(默认站点);
};
返回函数(){
geoip2.国家(成功、失败);
};
}());
重定向();
我基于此创建了一个更复杂的版本。您需要使用geoip2javascript客户端API(上述解决方案也是如此!)。玩得开心
<script src="//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
<script language="JavaScript">
var redirect = (function () {
/* Get actual URL */
var url = window.location.href;
/* This implements the actual redirection. */
var redirectBrowser = function (site) {
var uri = "https://www.example.com/" + site;
window.location = uri;
};
/* These are the country codes for the countries we have sites for.
* We will check to see if a visitor is coming from one of these countries.
* If they are, we redirect them to the country-specific site. If not, we
* redirect them to https://www.example.com/ */
var sites = {
"us": {"active": true, "target": "en-us"},
"gb": {"active": true, "target": "en-gb"},
"de": {"active": true, "target": "de"},
"fr": {"active": true, "target": "fr"},
"fi": {"active": true, "target": "fi"},
"hu": {"active": true, "target": "hu"},
"nl": {"active": true, "target": "nl"},
"se": {"active": true, "target": "sv"}
};
var defaultSite = "";
var onSuccess = function (geoipResponse) {
/* There's no guarantee that a successful response object
* has any particular property, so we need to code defensively. */
if (!geoipResponse.country.iso_code) {
redirectBrowser(defaultSite);
return;
}
/* ISO country codes are in upper case. */
var code = geoipResponse.country.iso_code.toLowerCase();
if ( sites[code].active ) {
redirectBrowser(sites[code].target);
}
else if ( url == defaultSite ) {
return;
}
else {
redirectBrowser(defaultSite);
}
};
/* We don't really care what the error is, we'll send them
* to the default site. */
var onError = function (error) {
redirectBrowser(defaultSite);
};
return function () {
geoip2.country( onSuccess, onError );
};
}());
redirect();
</script>
var redirect=(函数(){
/*获取实际URL*/
var url=window.location.href;
/*这实现了实际的重定向*/
var redirectBrowser=功能(站点){
var uri=”https://www.example.com/“+现场;
window.location=uri;
};
/*这些是我们网站所在国家的国家代码。
*我们将检查是否有游客来自这些国家之一。
*如果是,我们将其重定向到特定国家/地区的网站。如果不是,我们将
*将它们重定向到https://www.example.com/ */
变量站点={
“美国”:{“主动”:正确,“目标”:“en-u”