Advertisement
Guest User

Untitled

a guest
Oct 17th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.51 KB | None | 0 0
  1. <?php
  2. ini_set('display_errors', 1); // enable php error display for easy trouble shooting
  3. error_reporting(E_ALL); // set error display to all
  4.  
  5. function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE)
  6. {
  7.     $output = NULL;
  8.     if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
  9.         $ip = $_SERVER["REMOTE_ADDR"];
  10.         if ($deep_detect) {
  11.             if (filter_var($_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
  12.                 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  13.            
  14.             if (filter_var($_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
  15.                 $ip = $_SERVER['HTTP_CLIENT_IP'];
  16.         }
  17.     }
  18.    
  19.     $purpose    = str_replace(array(
  20.         "name",
  21.         "\n",
  22.         "\t",
  23.         " ",
  24.         "-",
  25.         "_"
  26.     ), NULL, strtolower(trim($purpose)));
  27.     $support    = array(
  28.         "country",
  29.         "countrycode",
  30.         "state",
  31.         "region",
  32.         "city",
  33.         "location",
  34.         "address"
  35.     );
  36.     $continents = array(
  37.         "AF" => "Africa",
  38.         "AN" => "Antarctica",
  39.         "AS" => "Asia",
  40.         "EU" => "Europe",
  41.         "OC" => "Australia (Oceania)",
  42.         "NA" => "North America",
  43.         "SA" => "South America"
  44.         // "IND" => "India"
  45.     );
  46.     if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
  47.         $ipdat = json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
  48.         if (strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
  49.             switch ($purpose) {
  50.                 case "location":
  51.                     $output = array(
  52.                         "city" => $ipdat->geoplugin_city,
  53.                         "state" => $ipdat->geoplugin_regionName,
  54.                         "country" => $ipdat->geoplugin_countryName,
  55.                         "country_code" => $ipdat->geoplugin_countryCode,
  56.                         "continent" => $continents[strtoupper($ipdat->geoplugin_continentCode)],
  57.                         "continent_code" => $ipdat->geoplugin_continentCode
  58.                     );
  59.                     break;
  60.                
  61.                 case "address":
  62.                     $address = array(
  63.                         $ipdat->geoplugin_countryName
  64.                     );
  65.                     if (strlen($ipdat->geoplugin_regionName) >= 1)
  66.                         $address[] = $ipdat->geoplugin_regionName;
  67.                     if (strlen($ipdat->geoplugin_city) >= 1)
  68.                         $address[] = $ipdat->geoplugin_city;
  69.                     $output = implode(", ", array_reverse($address));
  70.                     break;
  71.                
  72.                 case "city":
  73.                     $output = $ipdat->geoplugin_city;
  74.                     break;
  75.                
  76.                 case "state":
  77.                     $output = $ipdat->geoplugin_regionName;
  78.                     break;
  79.                
  80.                 case "region":
  81.                     $output = $ipdat->geoplugin_regionName;
  82.                     break;
  83.                
  84.                 case "country":
  85.                     $output = $ipdat->geoplugin_countryName;
  86.                     break;
  87.                
  88.                 case "countrycode":
  89.                     $output = $ipdat->geoplugin_countryCode;
  90.                     break;
  91.             }
  92.         }
  93.     }
  94.     return $output;
  95. }
  96. // exp 1
  97. $ip = $_SERVER['REMOTE_ADDR'];
  98. echo $ip . '<br>';
  99. // exp 1
  100. print_r(ip_info("$ip", "Location"));
  101.  
  102. // exp 2
  103. print_r(ip_info("196.185.85.31", "Location"));
  104. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement