Dave Cox
Dave Cox

Reputation: 11

Display Maxmind Geoip2 Display Country, City, Region and ISP with Javascript

I currently use the legacy maxmind geoip service to display country, city and region the user is visiting from. I would like to use the geoip2 service to display the information but it is beyond my skill level.

Here is the javascript I am currently using to display the data using the legacy version.

<script type="text/javascript" src="http://www.liquidvpn.com/global/ip.php"></script>
<br />
<script type="text/javascript" src="//j.maxmind.com/js/apis/geoip2/v2.0/geoip2.js"></script>
<script type="text/javascript" src="//j.maxmind.com/js/geoip.js">
  </script>
<script type="text/javascript">
         document.write( "Country: "+geoip_country_name()+"<br />" );
         document.write( "Region: "+geoip_region_name()+"<br />" );
        document.write( "City: "+geoip_city()+"<br />" );
</script>

Can anyone tell me how to do the same thing with the geoip2 version and add the ISP, IP and if they are using an open proxy? I am subscribed to their service already (not trying to do this on the free service)

Upvotes: 1

Views: 4508

Answers (1)

Greg Oschwald
Greg Oschwald

Reputation: 1735

I believe this will behave similarly to the code you posted. It uses jQuery but you could rewrite it without.

<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="//j.maxmind.com/js/apis/geoip2/v2.0/geoip2.js"></script>

<script>
geoip2.cityISPOrg(function (response) {
    $("#country").html(response.country.names.en);
    $("#region").html(response.most_specific_subdivision.names.en);
    $("#city").html(response.city.names.en);
    $("#isp").html(response.traits.isp);
    $("#proxy").html(response.traits.is_anonymous_proxy);
    $("#ip").html(response.traits.ip_address);
}, null, { w3cGeolocationDisabled: true });
</script>

<p>
    <span id="country"></span>,
    <span id="region"></span>,
    <span id="city"></span>,
    <span id="isp"></span>,
    <span id="ip"></span>,
    is proxy? <span id="proxy"></span>
</p>

All of the fields in the response object are outlined on the MaxMind dev site.

Upvotes: 1

Related Questions