954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

PHP - Converting IP to Country

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 :)

cancer10
Posting Whiz in Training
234 posts since Dec 2004
Reputation Points: 58
Solved Threads: 1
 

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]
<?php

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

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

digital-ether
Nearly a Posting Virtuoso
Moderator
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
 
smithveg
Newbie Poster
7 posts since Oct 2007
Reputation Points: 10
Solved Threads: 0
 

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] <?php

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

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

Great post! Thank you very much.

naroito
Newbie Poster
1 post since Jul 2009
Reputation Points: 10
Solved Threads: 0
 

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);
?>
stonito
Newbie Poster
1 post since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You