Home > PHP, Tutorials > PHP – Converting IP to Country

PHP – Converting IP to Country

December 20th, 2011 Leave a comment Go to comments

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);
}
?>

Categories: PHP, Tutorials Tags:
  1. No comments yet.
  1. No trackbacks yet.