Advertisement
Vladimir3261

Untitled

Apr 19th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.40 KB | None | 0 0
  1. <?php
  2. // Setup maxmind HTTP service endpoint
  3. define('MAXMIND_SERVICE_URI', 'http://skyspacex.com:3001');
  4. define('MAXMIND_REQUEST_TIMEOUT_MS', 500);
  5.  
  6. /**
  7.  * Check if current request IP address belongs to specified country ISO code.
  8.  *
  9.  * @param $isoCode
  10.  * @return bool
  11.  */
  12. function isCountry($isoCode)
  13. {
  14.     try {
  15.         // NOTICE! Getting IP address from GET params is unsafe! So, this is for test ONLY!
  16.         if (!empty($_GET['ip']) && filter_var($_GET['ip'], FILTER_VALIDATE_IP)) {
  17.             $ip = $_GET['ip'];
  18.         } else if (!empty($_SERVER['HTTP_CLIENT_IP']) && filter_var($_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP)) {
  19.             $ip = $_SERVER['HTTP_CLIENT_IP'];
  20.         } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']) && filter_var($_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP)) {
  21.             $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  22.         } else {
  23.             $ip = $_SERVER['REMOTE_ADDR'];
  24.         }
  25.  
  26.         if (!extension_loaded('curl')) {
  27.             throw new Exception('Curl extension is not installed');
  28.         }
  29.  
  30.         $ch = curl_init(MAXMIND_SERVICE_URI . "?ip=$ip");
  31.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  32.         curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
  33.         curl_setopt($ch, CURLOPT_TIMEOUT_MS, MAXMIND_REQUEST_TIMEOUT_MS);
  34.         $response = curl_exec($ch);
  35.         $curl_errno = curl_errno($ch);
  36.         $curl_error = curl_error($ch);
  37.         curl_close($ch);
  38.  
  39.         if ($curl_errno > 0) {
  40.             throw new Exception("Curl error ($curl_errno): $curl_error");
  41.         }
  42.         $data = json_decode($response, true);
  43.  
  44.         if (!$data) {
  45.             throw new Exception('Invalid json response from server');
  46.         }
  47.  
  48.         if ($data['Error']) {
  49.             throw new Exception("Data error: {$data['Error']}");
  50.         }
  51.  
  52.         if (!$data['Data'] || !is_array($data['Data']) || !array_key_exists('Country', $data['Data'])) {
  53.             throw new Exception('No country data');
  54.         }
  55.  
  56.         return $data['Data']['Country']['IsoCode'] === $isoCode;
  57.     } catch (Exception $e) {
  58.         // @TODO log error message
  59.         //echo "Something went wrong on ip detection: {$e->getMessage()}";
  60.         return false;
  61.     }
  62. }
  63.  
  64. // Usage.
  65. if (isCountry('UA')) {
  66.     echo "Received request from Ukraine";
  67.     // Paste your JS script here
  68. } else {
  69.     echo "Request outsie Ukraine.";
  70.     // Here will works common ad's.
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement