Important alert: (current site time 7/16/2013 7:30:50 AM EDT)
 

VB icon

ip 2 location

Email
Submitted on: 9/20/2012 3:31:41 PM
By: Oliver Leuyim Angel / Curda  
Level: Beginner
User Rating: Unrated
Compatibility: PHP 3.0, PHP 4.0, PHP 5.0
Views: 1442
author picture
(About the author)
 
     En: This code retrieves the country of the geo location of an IP adress // Very easy
Es: Este cogido obtiene el país de la geo localización de una direccion IP // muy facil
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: ip 2 location
// Description:En: This code retrieves the country of the geo location of an IP adress // Very easy <br>
Es: Este cogido obtiene el país de la geo localización de una direccion IP // muy facil
// By: Oliver Leuyim Angel / Curda
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=3074&lngWId=8//for details.//**************************************

<?php
/*
To get results you need an API key.
How do I get this API key?
This API key is free for everyone. You just have to go to here to register and a unique API key will sent to your 
email address.
 
Registration for api in: ipinfodb.com
Accents encoding
Depending on the locales of your server, you might have trouble diplaying accent in country, region and city name. 
Using PHP, you might need to utf8_encode the answer from our API. For other languages, look at the documentation 
how to encode in UTF-8.
Query limit
We do not have a specific daily limit but queries that are at a rate faster than 2 per second will be put in "queue". 
If you stay below 2 queries/second everything will be normal. If you go over the limit, you will still get an answer 
for all queries but they will be slowed down to about 1 per second. Good programming practices such as not querying our 
API for all page views (you can store the data in a cookie or a database) will also help not reaching the limit.
*/
function detect_location($ip,$api_key,$return = 'country')
{
$default = 'Unknown';
$url="http://api.ipinfodb.com/v3/ip-country/?key={$api_key}&ip={$ip}";
$content = file_get_contents($url);
$vars = explode(";",$content);
list($status,$em,$Gip,$av,$country) = $vars;
if(!empty($country)){
switch ($return){
case 'country':
return $country;
break;
case 'small':
return $av;
break;
case 'ip':
return $Gip;
break;
case 'status':
return $status;
break;
default:
return $default;
break;
}
}else{
return $default;
}
}
/////////////////////////////EXAMPLE FOR USE REMEMBER U NEED API KEY
//Default (country) 
echo detect_location('189.177.29.31','apikeyhere'); //REMEMBER U NEED ur API KEY
/***br***/echo "<br>";
//Small
echo detect_location('189.177.29.31','apikeyhere','small'); //REMEMBER U NEED ur API KEY
/***br***/echo "<br>"; 
//IP
echo detect_location('189.177.29.31','apikeyhere','ip'); //REMEMBER U NEED ur API KEY
/***br***/echo "<br>"; 
//Status
echo detect_location('189.177.29.31','apikeyhere','status'); //REMEMBER U NEED ur API KEY
 
 
?>


Other 10 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.