Obtener pais en función de dirección IP

En: PHP

13 Nov 2009

WP Greet Box icon
Hola! Si estás nuevo por aquí y te gusta lo que ves, puedes disfrutar de una actualización automática y gratuita por RSS para leer nuestros artículos completos cuando lo deseas en tu lector RSS favorito.

Este script permite obtener el pais del cual proviene una dirección ip, usando PHP

/**
*    getCountryByIP, get country by api, use http://www.iptoc.nl.ae/ API
*    @autor    Pablo Veintimilla <pveintimilla@undermedia.com.ec>
*    @param    ip    string    IP address to get country. Default The IP address from which the user is viewing the current page.
*    @param    type    int        You can choose between 3 types. When set to 1 you'll get a 2 character shortcut of the country, when set to 2 you'll get the 3 character shortcut of the country, when set to 3 you'll get the full country name. Default is 1.
*/
function getCountryByIP($ip = false, $type = 3){
$country = false;
$ip = $ip ? $ip : $_SERVER['REMOTE_ADDR'];
$api = "http://nl.ae/iptocapi.php?type=$type&ip=$ip";
@$response = file_get_contents($api);
if($response != '')
$country = $response;
return $country;
}

Ejemplos de uso:

echo getCountryByIP();    //Retorna pais, del visitante
echo getCountryByIP('190.152.189.118');    //Retorna pais del ip 190.152.189.118
echo getCountryByIP(false, 1);    //Retorna 2 letras del pais del visitante

Otros post

Este post ha sido visto 1313 veces

Deja tu comentario!

Acerca de UnderMedia

Inspiración y Determinación |
En Blog UnderMedia se recopilan y publican periódicamente noticias, artículos o comentarios generados dentro de UnderMedia S.A.

Twitter UnderMedia

Comunidad

Ultimas visitas

Raking

  • Blogalaxia
  • Top Blogs Ecuador

Switch to our mobile site