Tero

[+] Get details of any IP address. [Support IPV4 & IPV6]

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