IP geolocation Sorunu

Merhaba arkadaşlar,

Javascript IP geolocation kodunu kullanıyorum. Burada şöyle bir problem var. Aynı koda sahip 4 tane subdomain websitesi var. Burada amaç örnek olarak Almanyadan geldiyse kullanıcı almanyanın sitesine, Hollandadan geldiyse Hollandanın sitesine yönlendirmek. Problem her gittiği sitede kod çalıştığı için sürekli sayfa refresh oluyor. Bunu önlemenin yolu nedir ?

<script>
        //regular expressions to extract IP and country values
        const countryCodeExpression = /loc=([\w]{2})/;
        const userIPExpression = /ip=([\w\.]+)/;
        var xhr = new XMLHttpRequest();
    
        //automatic country determination.
        function initCountry() {
            var promise = new Promise((resolve, reject) => {
              
                xhr.timeout = 3000;
                xhr.onload = function ()
                {
                    if (xhr.readyState == 4) {
                        if (this.status == 200) {
                            countryCode = countryCodeExpression.exec(this.responseText)
                            ip = userIPExpression.exec(this.responseText)
                            if (countryCode === null || countryCode[1] === '' ||
                                ip === null || ip[1] === '') {
                                reject('IP/Country code detection failed');
                            }
                            let result = {
                                "countryCode": countryCode[1]
                            };
                            resolve(result)
                        } else {
                            reject(xhr.status)
                        }

                    }

                }

                xhr.ontimeout = function ()
                {
                    reject('timeout')
                }
                xhr.open('GET', 'https://www.cloudflare.com/cdn-cgi/trace', true);
                xhr.send();
                
               
            });
         
            return promise;
          
        };


            initCountry().then(function (result)
            {


                if (result.countryCode == "DE") {

                    window.location.href = "https://test.de.site.com/"


                }

                else if (result.countryCode == "NL") {
                    window.location.href = "https://test.nl.site.com/"

                }

                else if (result.countryCode == "FR") {
                    window.location.href = "https://test.fr.site.com/"

                }

                else {
                    window.location.href = "https://test.gl.site.com/"

                }


            });

        
        

       
      
    </script>
1 Beğeni

window.location.href zaten dogru degerse degistirmemek olabilir mesela.

1 Beğeni

Mesela nasıl karşılaştırma yapmam lazım. Kod tarafında yardımcı olur musun. Örnek olarak.

Bu şekilde yapabilirsin, hepsine if koymana gerek yok

  initCountry().then(function (result)
            {
                let cnCode = result.countryCode.toLowerCase()
                let url = `https://test.${cnCode}.site.com/`
                if(window.location.href !==url){
                    window.location.href = url
                }
            });
1 Beğeni