Very Simple Geo targeting

I personally really like the GEO service provided by ip-api.com

It is very simple to use and a sample PHP code to grab the countryCode would give you what you need very simply. You can do MUCH more and you can read the full doc on it but for your specific needs listed, here is a sample code:

$ip = $_SERVER['REMOTE_ADDR'];
$query = @unserialize(file_get_contents('http://ip-api.com/php/' . $ip));
$geo = $query['countryCode'];

The rest is a simple if $geo == 'US' do whatever else do something else. That’s pretty much it and its very simple to implement and you don’t have to install anything. The MaxMind library is good but it can be heavy and difficult to manage for some.

Good luck.


Update 1 [php] (3/5/18)

In response to your email, switching to JSON instead is not much different than how it is now. The only line that changes is this:

$query = json_decode(file_get_contents('http://ip-api.com/json/' . $ip), true);

and if you want to use a callback function, simply this:

$query = json_decode(file_get_contents('http://ip-api.com/json/' . $ip . '?callback=<function>'), true);

I recommend you use the true parameter on json_decode to get an array so it is easier to manipulate.

Update 2 [jquery] (3/5/18)

I just realized that in your email you mentioned jQuery and while I personally would opt for the above PHP method, here is an example of how to use jQuery to achieve the same thing.

var ip = ...;
var query = $.getJSON(
    "http://ip-api.com/json/"+ ip,
    function (json) {
        var whatever = json.whatever;
        // where whatever is something that is returned
        // such as status aka json.status
        // look at what is returned to see what you need
    });

… means the ip, however you are going to obtain it
no callback should really be necessary as you can just do whatever you want inside the function above and that’s it.