It depends on the configuration of your site, meaning whether you have nginx/apache or nginx/php-fpm or just apache serving the requests or something else. In either case, the idea is using geoip module, there exist versions of it for either configuration, including implementing it in plain PHP script.