Pages

Tuesday, October 12, 2010

IP to Country

I was looking for a way to get the country of a site visitor. I found an API from ipinfodb.com that does the trick. Here's my snippet to get that data. (The api provides more, I'm just extracting the country code.)

<?php
$ip=$_SERVER['REMOTE_ADDR'];
$response=file_get_contents('http://ipinfodb.com/ip_query.php?ip='.$ip);
$country_code=substr($response,strpos($response,'<CountryCode>')+13,2);
echo($country_code);
//echo $response;
?>

Now, if blogger hasn't mangled my code too badly, maybe that will help someone.

Thanks to www.ipinfodb.com

No comments: