The following is a simple PHP example on how to retrieve the country code from an IP address: <?php// If "ip" is passed either via GET or POST use that IP addressif (isset($_REQUEST["ip"] )) $ip = strip_tags(trim($_REQUEST["ip"]));// If not, use the client's (visitor's) IP address insteadelse $ip = $_SERVER["REMOTE_ADDR"];// Reverse the IP address order to prepare for the DNS lookup: 1.2.3.4 becomes 4.3.2.1$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 variableecho $cc;?> |
