ClarusDignus

geoiptest2.php

Mar 30th, 2016
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.83 KB | None | 0 0
  1. <?php
  2. function get_continent_by_ip($ip = false) {
  3.     $code = false;
  4.  
  5.     if (!$ip) {
  6.         $client = @$_SERVER['HTTP_CLIENT_IP'];
  7.         $forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
  8.         $remote = @$_SERVER['REMOTE_ADDR'];
  9.  
  10.         if (filter_var($client, FILTER_VALIDATE_IP)) {
  11.             $ip = $client;
  12.         } elseif (filter_var($forward, FILTER_VALIDATE_IP)) {
  13.             $ip = $forward;
  14.         } else {
  15.             $ip = $remote;
  16.         }
  17.     }
  18.  
  19.     $response = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip={$ip}"));    
  20.  
  21.     if ($response && isset($response->geoplugin_continentCode)) {
  22.         $code = $response->geoplugin_continentCode;
  23.     }
  24.  
  25.     return $code;
  26. }
  27. //$detectedip = $_SERVER['REMOTE_ADDR'];
  28. //get_continent_by_ip($detectedip);
  29. get_continent_by_ip('8.8.8.8');
  30. ?>
Advertisement
Add Comment
Please, Sign In to add comment