Example Code in PHP

script, php, javascript, code, container get_ip_address()

It is not so easy to get the client's IP.

	
<?PHP				
function get_ip_address() {

    foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED',
       'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR')
          as $key) {
     
        if (array_key_exists($key, $_SERVER) === true) {
            foreach (explode(',', $_SERVER[$key]) as $ip) {
                if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
                    return $ip;
                }
            }
        }
    }
}
?>

database code, MySQL, fetch query How to retreive results back from the IP database


<?PHP
   if ($_GET["ip"]) $ip = $_GET["ip"];  
   else $ip = get_ip_address();

   $hostname =  gethostbyaddr($ip); 

			
   $db_name = "i382870_ip2country"; 

   $con = mysql_connect("mysql-i", "username", "password");	
										  					
   mysql_select_db($db_name,$con);

   
   $result = mysql_query("SELECT * FROM `ip_group_country` WHERE `ip_start` 
           <= INET_ATON('" . $ip . "') ORDER BY ip_start desc LIMIT 1",$con);			    
   $row = mysql_fetch_assoc($result);
									 

   $country_code = $row["country_code"];
   $country_name = $row["country_name"]; 
?>

script, feed, XML, JSON, open source IP2C Feed Script (ip2c.php)

Download the source code for the complete feed endpoint script.

You want to call THIS script (ex: http://ip2country.sourceforge.net/ip2c.php?format=JSON) from your app or site. (You do not need to install this script, we welcome open access to all.)

This is the complete PHP script (everything you need), it uses the described functions above, plus it returns the results in in JSON or XML format.

examples on consuming the JSON/XML feed


 <?PHP

function get_ip_address() {

    foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 
              'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
        if (array_key_exists($key, $_SERVER) === true) {
            foreach (explode(',', $_SERVER[$key]) as $ip) {
                if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
                    return $ip;
                }
            }
        }
    }
}



if ($_GET["ip"]) $ip = $_GET["ip"];
 else $ip = get_ip_address();

$hostname =  gethostbyaddr($ip); 


if ($_GET["format"]){
  $feed_format = $_GET["format"];
} else {
  $feed_format ="JSON";
}
			
$db_name = "i382870_ip2country"; 

$con = mysql_connect("mysql-i", "username_here", "password_here");	
										  					
mysql_select_db($db_name,$con);

   
$result = mysql_query("SELECT * FROM `ip_group_country` WHERE `ip_start` 
          <= INET_ATON('" . $ip . "') ORDER BY ip_start desc LIMIT 1",$con);			    
$row = mysql_fetch_assoc($result);
									 

// ### create the JSON feed:    

if ($feed_format == "JSON") {

    header("Content-Type: text/plain");   
     
   $strJSONoutput = '{ip: "' . $ip . '",hostname: "' . $hostname . '",country_code: "'
	   . $row["country_code"] . '",country_name: "' . $row["country_name"] . '"}';

 echo $strJSONoutput;

} else {
	
	
   // ### create XML feed:

   header("Content-Type: application/xml"); 
     
      
   $dom = new DOMDocument("1.0");

   // create root element
   $root = $dom->createElement("lookup");
   $dom->appendChild($root);

   // create child element
   $item = $dom->createElement("ip");
   $root->appendChild($item);

   $text = $dom->createTextNode($ip);
   $item->appendChild($text);

   $item = $dom->createElement("hostname");
   $root->appendChild($item);

   $text = $dom->createTextNode($hostname);
   $item->appendChild($text);

   $item = $dom->createElement("country_code");
   $root->appendChild($item);

   $text = $dom->createTextNode($row["country_code"]);
   $item->appendChild($text);

   $item = $dom->createElement("country_name");
   $root->appendChild($item);

   $text = $dom->createTextNode($row["country_name"]);
   $item->appendChild($text);

  echo $dom->saveXML();

}

?>   

Coming Soon...

Future API updates will include:

The API will be updated soon to return:

  • State/Province
  • City
  • Latitude
  • Longitude