The following is a simple PHP example on how to retrieve the country code from an IP address:


// If "ip" is passed either via GET or POST use that IP address
if (isset($_REQUEST["ip"] )) $ip = strip_tags(trim($_REQUEST["ip"]));

// If not, use the client's (visitor's) IP address instead
else $ip = $_SERVER["REMOTE_ADDR"];

// Reverse the IP address order to prepare for the DNS lookup: becomes
$reverse_ip = implode('.',array_reverse(explode('.',$ip)));

// Define the DNS resolver
$DNS_resolver = '.lookup.ip2.cc';

// Lookup the TXT record storing the country code of the IP address and suppress any outputted errors
$lookup = @dns_get_record($reverse_ip.$DNS_resolver, DNS_TXT);

// PHP's function assigns the TXT record to a child array, reassign to a variable
$cc = $lookup[0]['txt'];

// Output the country code stored in the variable
echo $cc;