1

I am using a php file with a script in it to redirect 1 url to various urls based on the user location, but the it gets stuck. simply wont redirect. the thing is - it worked up until yesterday...

    
<!DOCTYPE html>
<html>
<head>


<script language="Javascript" src="http://gd.geobytes.com/gd?after=-1&variables=GeobytesLocationCode,GeobytesCode,GeobytesInternet"> 
</script>
<script>
function redirect(){
var sub= getSub();
var country= sGeobytesInternet;
if(country  == "BE"){window.location = "http://sitelink1.com/?subid="+sub}
else if(country  == "FR"){window.location = "http://sitelink2.com/?subid="+sub}
else if(country  == "DE"){window.location = "http://sitelink3.com/?subid="+sub}
else if(country  == "AT"){window.location = "http://sitelink4.com/?subid="+sub}
else if(country  == "CH"){window.location = "http://sitelink5.com/?subid="+sub}
else if(country  == "US"){window.location = "http://sitelink6.com/?subid="+sub}
else if(country  == "PT"){window.location = "http://sitelink7.com/?subid="+sub}
else if(country  == "BG"){window.location = "http://sitelink8.com/?subid="+sub}
else if(country  == "HU"){window.location = "http://sitelink9.com/?subid="+sub}
else if(country  == "PL"){window.location = "http://sitelink10.com/?subid="+sub}
else if(country  == "SK"){window.location = "http://sitelink11.com/?subid="+sub}
else if(country  == "IT"){window.location = "http://sitelink12.com/?subid="+sub}
else {window.location = ""+sub}
}
function getSub(){
regex = /subid=([\w-]*)/;
url =window.location.href;
if(-1 != url.indexOf("subid")){
var id = url.match(regex)[1];
return id;
}else{
return '';
}
}
redirect()
</script>
</head>
<body>
</body>
</html>
Croises
  • 18,570
  • 4
  • 30
  • 47
  • 1
    There are some problems with your url: http://gd.geobytes.com/gd?after=-1&variables=GeobytesLocationCode,GeobytesCode,GeobytesInternet . I have tried to open It, but It currently unavailable. – Ali Mamedov Apr 23 '16 at 14:17
  • I have updated my answer.You can try a small sample. I have used another geoIP provider. It will work good for you. – Ali Mamedov Apr 23 '16 at 14:40

2 Answers2

0

I think It is possible to improve your code. And I advice you to change geoIp provider. Lets see my example:

<script  src="https://code.jquery.com/jquery-2.2.3.min.js"></script>
<script type="text/javascript">

    $(function() {

        $.getJSON('http://ipinfo.io/json', function(data) {

            /*
                {
                    "ip": "46.23.xxx.xxx",
                    "hostname": "host.example.com",
                    "city": "Baku",
                    "region": "Baku City",
                    "country": "AZ",
                    "loc": "40.000,40.000",
                }
            */

            var
                map,
                countryCode = data.country,
                getParameter;

            /* 
                http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript?answertab=active#tab-top 
            */
            getParameter = function(name) {

                var
                    regex,
                    results;

                url = window.location.href;
                name = name.replace(/[\[\]]/g, "\\$&");
                regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)");
                results = regex.exec(url);

                if (!results) return null;
                if (!results[2]) return '';

                return decodeURIComponent(results[2].replace(/\+/g, " "));
            }

            map = {

                "AZ": 'http://google.az',
                "DE": 'http://google.de',
                "FR": 'http://google.fr',
                "PL": 'http://google.pl',
                "IT": 'http://google.it',
                "CH": 'http://google.ch',
                "SK": 'http://google.sk',
                "AT": 'http://google.at',
                "US": 'http://google.us',
                "BE": 'http://google.be',
                "PT": 'http://google.pt',
                "BG": 'http://google.bg',
                "HU": 'http://google.hu',
            };

            if (map[countryCode] !== 'undefined') {

                location.href = map[countryCode] + '?subid=' + getParameter('subid');
            }
            else {

                location.href = 'http://google.com' + '?subid=' + getParameter('subid');
            }
        });
    });

</script>
Ali Mamedov
  • 5,116
  • 3
  • 33
  • 47
0

Try adding semicolons to your script.

    
<!DOCTYPE html>
<html>
<head>


<script language="Javascript" src="http://gd.geobytes.com/gd?after=-1&variables=GeobytesLocationCode,GeobytesCode,GeobytesInternet"> 
</script>
<script>
function redirect(){
var sub= getSub();
var country= sGeobytesInternet;
if(country  == "BE"){window.location = "http://sitelink1.com/?subid="+sub;}
else if(country  == "FR"){window.location = "http://sitelink2.com/?subid="+sub;}
else if(country  == "DE"){window.location = "http://sitelink3.com/?subid="+sub;}
else if(country  == "AT"){window.location = "http://sitelink4.com/?subid="+sub;}
else if(country  == "CH"){window.location = "http://sitelink5.com/?subid="+sub;}
else if(country  == "US"){window.location = "http://sitelink6.com/?subid="+sub;}
else if(country  == "PT"){window.location = "http://sitelink7.com/?subid="+sub;}
else if(country  == "BG"){window.location = "http://sitelink8.com/?subid="+sub;}
else if(country  == "HU"){window.location = "http://sitelink9.com/?subid="+sub;}
else if(country  == "PL"){window.location = "http://sitelink10.com/?subid="+sub;}
else if(country  == "SK"){window.location = "http://sitelink11.com/?subid="+sub;}
else if(country  == "IT"){window.location = "http://sitelink12.com/?subid="+sub;}
else {window.location = ""+sub;}
}
function getSub(){
regex = /subid=([\w-]*)/;
url =window.location.href;
if(-1 != url.indexOf("subid")){
var id = url.match(regex)[1];
return id;
}else{
return '';
}
}
redirect()
</script>
</head>
<body>
</body>
</html>
Sub 6 Resources
  • 1,674
  • 15
  • 31