PHP - Converting IP to Country

Reply

Join Date: Dec 2004
Posts: 234
Reputation: cancer10 is an unknown quantity at this point 
Solved Threads: 0
cancer10's Avatar
cancer10 cancer10 is offline Offline
Posting Whiz in Training

PHP - Converting IP to Country

 
0
  #1
May 11th, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,098
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 67
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: PHP - Converting IP to Country

 
0
  #2
May 11th, 2007
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.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 8
Reputation: smithveg is an unknown quantity at this point 
Solved Threads: 0
smithveg smithveg is offline Offline
Newbie Poster

Re: PHP - Converting IP to Country

 
0
  #3
Jan 4th, 2009
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 1
Reputation: naroito is an unknown quantity at this point 
Solved Threads: 0
naroito naroito is offline Offline
Newbie Poster

Re: PHP - Converting IP to Country

 
0
  #4
Jul 23rd, 2009
Originally Posted by digital-ether View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 11877 | Replies: 3
Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC