Ver Mensaje Individual
  #2 (permalink)  
Antiguo 02/08/2015, 20:42
0fran
 
Fecha de Ingreso: mayo-2015
Ubicación: Localhost
Mensajes: 13
Antigüedad: 8 años, 11 meses
Puntos: 1
Respuesta: Bloquear visitas de países con geoplugin

Primero crea un archivo con el nombre: Geoplugin.class.php
en el coloca lo siguiente:
Código PHP:
Ver original
  1. <?php
  2.  
  3. class geoPlugin {
  4.    
  5.     //the geoPlugin server
  6.     var $host = 'http://www.geoplugin.net/php.gp?ip={IP}&base_currency={CURRENCY}';
  7.        
  8.     //the default base currency
  9.     var $currency = 'USD';
  10.    
  11.     //initiate the geoPlugin vars
  12.     var $ip = null;
  13.     var $city = null;
  14.     var $region = null;
  15.     var $areaCode = null;
  16.     var $dmaCode = null;
  17.     var $countryCode = null;
  18.     var $countryName = null;
  19.     var $continentCode = null;
  20.     var $latitude = null;
  21.     var $longitude = null;
  22.     var $currencyCode = null;
  23.     var $currencySymbol = null;
  24.     var $currencyConverter = null;
  25.    
  26.     function geoPlugin() {
  27.  
  28.     }
  29.    
  30.     function locate($ip = null) {
  31.        
  32.         global $_SERVER;
  33.        
  34.         if ( is_null( $ip ) ) {
  35.             $ip = $_SERVER['REMOTE_ADDR'];
  36.         }
  37.        
  38.         $host = str_replace( '{IP}', $ip, $this->host );
  39.         $host = str_replace( '{CURRENCY}', $this->currency, $host );
  40.        
  41.         $data = array();
  42.        
  43.         $response = $this->fetch($host);
  44.        
  45.         $data = unserialize($response);
  46.        
  47.         //set the geoPlugin vars
  48.         $this->ip = $ip;
  49.         $this->city = $data['geoplugin_city'];
  50.         $this->region = $data['geoplugin_region'];
  51.         $this->areaCode = $data['geoplugin_areaCode'];
  52.         $this->dmaCode = $data['geoplugin_dmaCode'];
  53.         $this->countryCode = $data['geoplugin_countryCode'];
  54.         $this->countryName = $data['geoplugin_countryName'];
  55.         $this->continentCode = $data['geoplugin_continentCode'];
  56.         $this->latitude = $data['geoplugin_latitude'];
  57.         $this->longitude = $data['geoplugin_longitude'];
  58.         $this->currencyCode = $data['geoplugin_currencyCode'];
  59.         $this->currencySymbol = $data['geoplugin_currencySymbol'];
  60.         $this->currencyConverter = $data['geoplugin_currencyConverter'];
  61.        
  62.     }
  63.    
  64.     function fetch($host) {
  65.  
  66.         if ( function_exists('curl_init') ) {
  67.                        
  68.             //use cURL to fetch data
  69.             $ch = curl_init();
  70.             curl_setopt($ch, CURLOPT_URL, $host);
  71.             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  72.             curl_setopt($ch, CURLOPT_USERAGENT, 'geoPlugin PHP Class v1.0');
  73.             $response = curl_exec($ch);
  74.             curl_close ($ch);
  75.            
  76.         } else if ( ini_get('allow_url_fopen') ) {
  77.            
  78.             //fall back to fopen()
  79.             $response = file_get_contents($host, 'r');
  80.            
  81.         } else {
  82.  
  83.             trigger_error ('geoPlugin class Error: Cannot retrieve data. Either compile PHP with cURL support or enable allow_url_fopen in php.ini ', E_USER_ERROR);
  84.             return;
  85.        
  86.         }
  87.        
  88.         return $response;
  89.     }
  90.    
  91.     function convert($amount, $float=2, $symbol=true) {
  92.        
  93.         //easily convert amounts to geolocated currency.
  94.         if ( !is_numeric($this->currencyConverter) || $this->currencyConverter == 0 ) {
  95.             trigger_error('geoPlugin class Notice: currencyConverter has no value.', E_USER_NOTICE);
  96.             return $amount;
  97.         }
  98.         if ( !is_numeric($amount) ) {
  99.             trigger_error ('geoPlugin class Warning: The amount passed to geoPlugin::convert is not numeric.', E_USER_WARNING);
  100.             return $amount;
  101.         }
  102.         if ( $symbol === true ) {
  103.             return $this->currencySymbol . round( ($amount * $this->currencyConverter), $float );
  104.         } else {
  105.             return round( ($amount * $this->currencyConverter), $float );
  106.         }
  107.     }
  108.    
  109.     function nearby($radius=10, $limit=null) {
  110.  
  111.         if ( !is_numeric($this->latitude) || !is_numeric($this->longitude) ) {
  112.             trigger_error ('geoPlugin class Warning: Incorrect latitude or longitude values.', E_USER_NOTICE);
  113.             return array( array() );
  114.         }
  115.        
  116.         $host = "http://www.geoplugin.net/extras/nearby.gp?lat=" . $this->latitude . "&long=" . $this->longitude . "&radius={$radius}";
  117.        
  118.         if ( is_numeric($limit) )
  119.             $host .= "&limit={$limit}";
  120.            
  121.         return unserialize( $this->fetch($host) );
  122.  
  123.     }
  124.  
  125.    
  126. }
  127.  
  128. ?>
Ahora crear un archivo con el nombre : geoip.php
en el coloca lo siguiente:
Código PHP:
Ver original
  1. <?php
  2. require_once('geoplugin.class.php');
  3. $geoplugin = new geoPlugin();
  4. $geoplugin->locate();
  5. $country_code = $geoplugin->countryCode;
  6. switch($country_code) {
  7. case 'RU': //Rusia
  8. header('Location: http://google.ru'); //Aquí es donde los redireccionara.
  9. case 'ID': //Indonesia
  10. header('Location: http://google.co.id');
  11. case 'UA': //Ucrania
  12. header('Location: http://google.ua');
  13. }
  14. ?>

y por ultimo coloca lo siguiente, en las páginas que quieres que se aplique.
Código PHP:
Ver original
  1. <?php include("geoip.php"); ?>