942,956 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 29330
  • PHP RSS
May 11th, 2007
0

PHP - Converting IP to Country

Expand Post »
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
Similar Threads
Reputation Points: 58
Solved Threads: 1
Posting Whiz in Training
cancer10 is offline Offline
234 posts
since Dec 2004
May 11th, 2007
0

Re: PHP - Converting IP to Country

Heres a search for it on sourceforge.

http://sourceforge.net/search/?type_...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]
Last edited by digital-ether; May 11th, 2007 at 7:43 am.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Jan 4th, 2009
0

Re: PHP - Converting IP to Country

Reputation Points: 10
Solved Threads: 0
Newbie Poster
smithveg is offline Offline
7 posts
since Oct 2007
Jul 23rd, 2009
-1

Re: PHP - Converting IP to Country

Heres a search for it on sourceforge.

http://sourceforge.net/search/?type_...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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
naroito is offline Offline
1 posts
since Jul 2009
Dec 28th, 2010
0
Re: PHP - Converting IP to Country
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 Syntax (Toggle Plain Text)
  1. <?php
  2. $country = file_get_contents('http://stonito.com/script/geoip/?ip='.$IP);
  3. ?>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
stonito is offline Offline
1 posts
since Dec 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: error in mysql_fetch_array();
Next Thread in PHP Forum Timeline: mysql_query problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC