Advertisement
ClarusDignus

geoiptest.php V2

Mar 30th, 2016
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.24 KB | None | 0 0
  1. <?php
  2.  
  3. function ip_info($ip = NULL, $purpose = "location", $deep_detect = TRUE) {
  4.     $output = NULL;
  5.     if (filter_var($ip, FILTER_VALIDATE_IP) === FALSE) {
  6.         $ip = $_SERVER["REMOTE_ADDR"];
  7.         if ($deep_detect) {
  8.             if (filter_var(@$_SERVER['HTTP_X_FORWARDED_FOR'], FILTER_VALIDATE_IP))
  9.                 $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  10.             if (filter_var(@$_SERVER['HTTP_CLIENT_IP'], FILTER_VALIDATE_IP))
  11.                 $ip = $_SERVER['HTTP_CLIENT_IP'];
  12.         }
  13.     }
  14.     $purpose    = str_replace(array("name", "\n", "\t", " ", "-", "_"), NULL, strtolower(trim($purpose)));
  15.     $support    = array("country", "countrycode", "state", "region", "city", "location", "address");
  16.     $continents = array(
  17.         "AF" => "Africa",
  18.         "AN" => "Antarctica",
  19.         "AS" => "Asia",
  20.         "EU" => "Europe",
  21.         "OC" => "Australia (Oceania)",
  22.         "NA" => "North America",
  23.         "SA" => "South America"
  24.     );
  25.     if (filter_var($ip, FILTER_VALIDATE_IP) && in_array($purpose, $support)) {
  26.         $ipdat = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=" . $ip));
  27.         if (@strlen(trim($ipdat->geoplugin_countryCode)) == 2) {
  28.             switch ($purpose) {
  29.                 case "location":
  30.                     $output = array(
  31.                         "city"           => @$ipdat->geoplugin_city,
  32.                         "state"          => @$ipdat->geoplugin_regionName,
  33.                         "country"        => @$ipdat->geoplugin_countryName,
  34.                         "country_code"   => @$ipdat->geoplugin_countryCode,
  35.                         "continent"      => @$continents[strtoupper($ipdat->geoplugin_continentCode)],
  36.                         "continent_code" => @$ipdat->geoplugin_continentCode
  37.                     );
  38.                     break;
  39.                 case "address":
  40.                     $address = array($ipdat->geoplugin_countryName);
  41.                     if (@strlen($ipdat->geoplugin_regionName) >= 1)
  42.                         $address[] = $ipdat->geoplugin_regionName;
  43.                     if (@strlen($ipdat->geoplugin_city) >= 1)
  44.                         $address[] = $ipdat->geoplugin_city;
  45.                     $output = implode(", ", array_reverse($address));
  46.                     break;
  47.                 case "city":
  48.                     $output = @$ipdat->geoplugin_city;
  49.                     break;
  50.                 case "state":
  51.                     $output = @$ipdat->geoplugin_regionName;
  52.                     break;
  53.                 case "region":
  54.                     $output = @$ipdat->geoplugin_regionName;
  55.                     break;
  56.                 case "country":
  57.                     $output = @$ipdat->geoplugin_countryName;
  58.                     break;
  59.                 case "countrycode":
  60.                     $output = @$ipdat->geoplugin_countryCode;
  61.                     break;
  62.             }
  63.         }
  64.     }
  65.     return $output;
  66. }
  67.  
  68. ?>
  69. <?php
  70. $ipinfo = ip_info('50.203.165.214');
  71. echo ip_info("Visitor", "Country"); // India
  72. echo ip_info("Visitor", "Country Code"); // IN
  73. echo ip_info("Visitor", "State"); // Andhra Pradesh
  74. echo ip_info("Visitor", "City"); // Proddatur
  75. echo ip_info("Visitor", "Address"); // Proddatur, Andhra Pradesh, India
  76. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement