Ver Mensaje Individual
  #3 (permalink)  
Antiguo 12/02/2011, 09:16
Avatar de Italico76
Italico76
 
Fecha de Ingreso: abril-2007
Mensajes: 3.303
Antigüedad: 17 años
Puntos: 292
Respuesta: [APORTE] clase de GeoLocalizacion

Ultima version (es funcionalmente equivalente a la primera) pero es milesimas de segundo mas rapida e incorpora getCountrybyName()

Código PHP:
<?php
include "../../my/config.php";


$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';
$succ $loc->doit();

if (
$succ){
  echo 
$loc->country;
  
//echo $loc->getCountrybyName(); # SUBIR TABLA !!!
}  


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){
      
$this->data mysql_fetch_assoc($res);
            
      
      
// retorno exito
      
return true;
    }else{
      
// no hay resultados
      
return false
    }    
       
  }
  
  
// convierte el codigo de pais (AR,MX,..) en nombre como "South Georgia and the South Sandwich Islands"
  
function getCountrybyName(){
    
$sql "SELECT name FROM countrynames WHERE id='{$this->country}'";
    
$res mysql_query($sql) OR die(mysql_error());
    
$row mysql_fetch_array($res);
    return 
$row[0];
  }
  
  
// end class
  

function toIPv4 ($dotted){
  if (( 
$lngIP ip2long $dotted)) < ){ 
    
$lngIP += 4294967296 ;
  }
  return 
$lngIP;
}
?>
__________________
Salu2!