Se caracteriza por ser muy rapida
Cuando no encuentra referencia para esa IP, doit() devuelve FALSE
Código PHP:
<?php
include "../../my/config.php";
// datos de Copyright (C) 2007 MaxMind LLC
// Autor: Pablo Bozzolo (2011)
$loc= new geo();
$loc->host = $db_info['host'];
$loc->user = $db_info['user'];
$loc->pass = $db_info['pass'];
//$loc->IPremote();
$loc->ip = '148.210.232.247';
$out = $loc->doit();
var_dump($out);
echo $loc->country;
class Geo {
private $con;
private $data = array(
// DB DATA
'host','user','pass',
// GEO
'ip'=>null, 'locId'=>null,'country'=>null,'region'=>null,'city'=>null,
'postalCode'=>null,'latitude'=>null,'longitude'=>null,
'metroCode'=>null,'areaCode'=>null);
public function Geo(){
}
public function __set($name, $value) {
//echo "Estableciendo '$name' a '$value'\n";
$this->data[$name] = $value;
}
public function __get($name) {
//echo "Consultando '$name'\n";
if (array_key_exists($name, $this->data)) {
return $this->data[$name];
}
$trace = debug_backtrace();
trigger_error(
'Propiedad indefinida mediante __get(): ' . $name .
' en ' . $trace[0]['file'] .
' en la línea ' . $trace[0]['line'],
E_USER_NOTICE);
return null;
}
/** Desde PHP 5.1.0 */
public function __isset($name) {
//echo "¿Está definido '$name'?\n";
return isset($this->data[$name]);
}
/** Desde PHP 5.1.0 */
public function __unset($name) {
//echo "Eliminando '$name'\n";
unset($this->data[$name]);
}
// NO uso Singleton!
private function connect(){
$this->con = mysql_connect($this->host,$this->user,$this->pass) or die ('Error conectando a mysql');
mysql_select_db('videosbu_geo') or die ('Error seleccionando base');
}
public function IPremote(){
$this->ip = $_SERVER['REMOTE_ADDR'];
}
public function doit(){
$this->connect();
$ipv4 = toIPv4($this->ip);
$sql = "
SELECT cl.country, cl.region, cl.city, cl.postalCode, cl.latitude, cl.longitude, cl.metroCode, cl.areaCode FROM CityLocation cl
INNER JOIN
(SELECT locId FROM CityBlocks WHERE ($ipv4 > startIpNum AND $ipv4 < endIpNum) ORDER BY startIpNum DESC LIMIT 1)
t1 ON cl.locid = t1.locId";
$res = mysql_query($sql) OR die(mysql_error());
$cant_rows = mysql_num_rows($res);
if ($cant_rows==1){
$row = mysql_fetch_assoc($res);
$this->country =$row['country'];
$this->region =$row['region'];
$this->city =$row['city'];
$this->postalCode =$row['postalCode'];
$this->latitude =$row['latitude'];
$this->longitude =$row['longitude'];
$this->metroCode =$row['metroCode'];
$this->areaCode =$row['areaCode'];
// retorno exito
return true;
}else{
// no hay resultados
return false;
}
}
} // end class
function toIPv4 ($dotted){
if (( $lngIP = ip2long ( $dotted)) < 0 ){
$lngIP += 4294967296 ;
}
return $lngIP;
}
?>
Para importar los CSV, lo mejor es usar algo asi:
Código PHP:
<?php
/* cargo datos de conexion */
$con = mysql_connect($dbhost,$dbuser,$dbpass) or die ('Error conectando a mysql');
mysql_select_db($dbname) or die ('Error seleccionando base');
if (($handle = fopen("2.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000 , ",")) !== FALSE) {
if (strlen($data[1])==0) $data[1]='';
if (strlen($data[2])==0) $data[2]='';
if (strlen($data[3])==0) $data[3]='';
if (strlen($data[4])==0) $data[4]='';
if ($data[5]==0) $data[5]=0;
if ($data[6]==0) $data[6]=0;
if ($data[7]==0) $data[7]=0;
if ($data[8]==0) $data[8]=0;
$sql = "INSERT INTO `citylocation` (locId,country,region,city,postalCode,latitude,longitude,metroCode,areaCode) VALUES
({$data[0]},'{$data[1]}','{$data[2]}','{$data[3]}','{$data[4]}',{$data[5]},{$data[6]},{$data[7]},{$data[8]});";
mysql_query ($sql);
}
fclose($handle);
}
?>