2

Possible Duplicate:
IP to Country?

What is the best way to get country from given IP in PHP?

Community
  • 1
  • 1
Vijay Bhatia
  • 55
  • 1
  • 6
  • http://stackoverflow.com/questions/3931485/how-to-track-users-location-region-in-php/3931552#3931552 – Hannes Nov 28 '10 at 08:32
  • -1 Please [Do Your Homework](http://stackoverflow.com/questions/ask-advice) before asking questions that have been answered numerous times before. You received the linked page before you could ask your question and apparently you ignored everything on it. Then, when entering your question title, Stack Overflow suggested a number of possible duplicates with the first four questions all answering your question. – Gordon Nov 28 '10 at 11:22
  • IPLocate.io provides a free API: [`https://www.iplocate.io/api/lookup/8.8.8.8`](https://www.iplocate.io/api/lookup/8.8.8.8) - Disclaimer: I run this service. – ttarik Nov 15 '17 at 00:47

2 Answers2

6

Take a look at:

The IP-to-Country Handbook

You can download this free database

Once you have imported the database, you need to run this query to get country from the IP:

 $country_query  = "SELECT country_code2,country_name FROM iptoc
                    WHERE IP_FROM<='$REMOTE_ADDR' AND IP_TO>='$REMOTE_ADDR'";
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
1

You can use one of the free IP address geolocation tools existing, like the one at http://ipinfodb.com/ip_location_api.php.

It is very easy to use, having example for many programming languages, as well as some modules for different content management systems (wordpress, drupal, joomla).

misterjinx
  • 2,606
  • 3
  • 27
  • 29