PHP - Converting IP to Country

Hello,

Is there any function/pre-made scripts in php which can tell me the name of country based on IP?


For example: If I supply an IP 132.162.138.139 then the script would echo "India"

Thanx in advance :)

Recommended Answers

All 9 Replies

Heres a search for it on sourceforge.

http://sourceforge.net/search/?type_of_search=soft&words=ip+to+country+php

There are a number of IP to country scripts in PHP there..

What to note is that differnet scripts use different IP to Address databases. Some are better than others.

If you're interested in writing your own, you can download a database from sourceforge, or http://www.hostip.info for example..

Then use it to get the country from the IP.

Another way is to use a webservice.

For instance you can use this one:
http://www.hostip.info/use.html

To get the country would be:

http://api.hostip.info/country.php?ip=$IP

Where $IP is the user IP.

PHP would look something like:

<?php

$country = '';
$IP = $_SERVER['REMOTE_ADDR'];

if (!empty($IP)) {
   $country = file_get_contents('http://api.hostip.info/country.php?ip='.$IP);
}
?>

Heres a search for it on sourceforge.

http://sourceforge.net/search/?type_of_search=soft&words=ip+to+country+php

There are a number of IP to country scripts in PHP there..

What to note is that differnet scripts use different IP to Address databases. Some are better than others.

If you're interested in writing your own, you can download a database from sourceforge, or http://www.hostip.info for example..

Then use it to get the country from the IP.

Another way is to use a webservice.

For instance you can use this one:
http://www.hostip.info/use.html

To get the country would be:

http://api.hostip.info/country.php?ip=$IP

Where $IP is the user IP.

PHP would look something like:

<?php

$country = '';
$IP = $_SERVER['REMOTE_ADDR'];

if (!empty($IP)) {
   $country = file_get_contents('http://api.hostip.info/country.php?ip='.$IP);
}
?>

Great post! Thank you very much.

Site api.hostip.info
uses very, very old ip2country database, and so it is very inaccurate.
So, it was unusable for me and I made something with a new database, and I will update database monthly.
It's free for everyone to use, in the same way (good idea)

<?php
  $country = file_get_contents('http://stonito.com/script/geoip/?ip='.$IP);
?>

I appologise for the late reply, but it should help posterity.

Consider ipcountryphp (my site, my code, my honour) as it provides a local internet-lifetime freely updated database. It's fast and fully self-contained, pluggable into anything PHP 5.3, SQLite3 and beyond. Very fast seeks and no performance penalties.

Enough with self-promotion, let's clear something out regarding the other suggestions.

Relying on querying remote services in real-time to get visitor country can become a major bottleneck for your site's functionality depending on the response speed of the queried server. As a rule of thumb you should never query external services for real-time site functionality (like page loading). Using APIs in the background is great but when you need to query the country of each visitor before the page is rendered, you open yourself up to a world of pain.

And do keep in mind you're not the only one abusing free services :)

So queries to 3rd-party services stay in the background while only local functionality that relies on no 3rd-party go into the layers there users interact with. Just my slightly performance paranoid take on this :)

PS: Above mentioned script I wrote has IPv6 support too.

I also want this but only using with PHP. Neither user js plugins nor API to find county

Member Avatar for diafol

Just so that you're aware, the data for this is massive and it usually comes in csv flavour. Converting it to SQL can be a bit awkward - you'll need to take care of commas in some country names. The CSV is around 4Mb.
As new IP addresses are added all the time, you may need to set up some automated script. BTW - the csv I used was only for IPv4.

Also the csv will hold the integer equivalent of the ip address:

the ip address: a.b.c.d

will give [a*16777216 + b*65536 + c*256 + d] as the "ip decimal number"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.