2

A few months ago I created a code that detect a visitor country and display the legal drinking age. For country in EU is 18 and for other countries is 21.

I'm using the freegeoip.

The code was working great, but now I noticed that doesn't work anymore.

    $.get("http://freegeoip.net/json/", function (response) {
    $("#ip").html("IP: " + response.ip);
    $("#country_code").html(response.country_code);
    if(response.country_code=='AL','AD','AT','BY','BE','BA','BG','HR','CY','CZ','DK','EE','FO','FI','FR','DE','GI','GR','HU','IS','IE','IT','LV','LI','LT','LU','MK','MT','MD','MC','NL','NO','PL','PT','RO','RU','SM','RS','SK','SI','ES','SE','CH','UA','VA','RS','IM','RS','ME') {
        $(".age").html("18");
    } else {
        $(".age").html("21");
    }
}, "jsonp");

Here I dispay the age:

<span>ARE YOU</span> OVER <span class="age"></span>?

I assume that the problem is in freegeoip but I can't fix it.

  • I'm voting to close this question as off-topic because it's about the availability of third-party services. – Ian Kemp Apr 06 '16 at 06:51

2 Answers2

5

You can also use ip-api.com like freegeoip :

function getDataOfIp($ip) {
    try {
        $pageContent = file_get_contents('http://ip-api.com/json/' . $ip);
        $parsedJson  = json_decode($pageContent);
        return [
            "country_name" => $parsedJson->country,
            "country_code" => $parsedJson->countryCode,
            "time_zone" => $parsedJson->timezone
        ];
    } 
    catch (Exception $e) {
        return null;
    }
}
Samuel Dauzon
  • 10,744
  • 13
  • 61
  • 94
  • 1
    Worked perfect I added city and state to my output thank you so much I had to dig like crazy to find a site that was giving the correct city without having to use javascript. – penguinjeff Jun 30 '21 at 17:51
0

I just found an answer for the problem. I noticed that the freegeoip.net website doesn't work so I changed the service.

I replaced the http://freegeoip.net/json/ with http://getcitydetails.geobytes.com/GetCityDetails?callback=?

and added <script language="Javascript" src="http://gd.geobytes.com/gd?after=-1&variables=GeobytesLocationCode,GeobytesCode,GeobytesInternet,GeobytesFqcn"></script>

Now the code works again.

  • I think now you have to use the parameter callback to make it work: freegeoip.net/json/?callback=foobar – Ommadawn Aug 26 '17 at 21:57