Search code examples
javascriptliquidweb-traffic

Best way to block certain countries from access to website


I need to block certain on certain counties from accessing the website, and have made the following script below, but it does not block efficiently enough.

Unfortunately I do not have access to the .htaccess file or Apache config file through the CMS, so I need to solve it with Javascript or similar coding.

Do you have any suggestion how to set it up or improve my script below?

<script>
var langcodes = new Array("zh", "default");
var langredirects = new Array("/lang/{module_pagename}", "DEFAULT");
var languageinfo = navigator.language ? navigator.language : navigator.userLanguage;
var gotodefault = 1;
function redirectpage(dest) {
    if (window.location.replace)
       window.location.replace(dest);
    else
        window.location = dest;
}

for (i = 0; i < langcodes.length - 1; i++) {
    if (languageinfo.substr(0, 2) == langcodes[i]) {
        redirectpage(langredirects[i]);
        gotodefault = 0;
        break;
    }
}
</script>

Solution

  • If you are using PHP,then use an IP geolocation database (some are free) and $_SERVER["REMOTE_ADDR"] to get the visitor's IP address.

    http://www.maxmind.com/app/geolitecity is a free (less accurate) version of a commercial one.

    More details please refer