ip-to-country's data Version: 2.0 Author: Priyadi Iman Nurcahyo Author URI: http://priyadi.net/ */ /* adapted from the ip to nation plugin from http://frenchfragfactory.net/ozh/archives/2004/08/27/ip-to-nation-plugin/ */ ### BEGIN CONFIGURATION $wp_ip2c_usecache = false; ### END CONFIGURATION function wp_ip2c_getdb_fromdb ($ip) { global $wpdb; $sql = "SELECT country_code2, country_code3, country_name FROM iptocountry WHERE IP_FROM<=inet_aton('$ip') AND IP_TO>=inet_aton('$ip')"; $row = $wpdb->get_row("$sql"); return $row; } function wp_ip2c_getdb_fromcache ($ip) { global $wpdb; $sql = "SELECT country_code2, country_code3, country_name FROM iptocountry_cache WHERE ip = INET_ATON('$ip')"; $row = $wpdb->get_row("$sql"); return $row; } function wp_ip2c_getdb_cachemiss ($ip) { global $wpdb; $data = wp_ip2c_getdb_fromdb($ip); $cc2 = $data->country_code2; $cc3 = $data->country_code3; $cname = $data->country_name; $sql = "REPLACE INTO iptocountry_cache ( ip, country_code2, country_code3, country_name ) VALUES ( INET_ATON('$ip'), '$cc2', '$cc3', '$cname' )"; $wpdb->query($sql); return $data; } function wp_ip2c_getdb ($ip) { global $wpdb, $wp_ip2c_usecache; if ($wp_ip2c_usecache) { $fromcache = wp_ip2c_getdb_fromcache($ip); $cc2 = $fromcache->country_code2; if ($cc2) { return $fromcache; } else { return wp_ip2c_getdb_cachemiss($ip); } } else { return wp_ip2c_getdb_fromdb($ip); } } function wp_ip2c_getCountryName ($display=0,$ip="") { global $wpdb, $wp_ip2c; $refresh = 1; if (!$ip) { $ip = $_SERVER['REMOTE_ADDR']; $refresh = 0; } if (!$wp_ip2c || $refresh) $wp_ip2c = wp_ip2c_getdb ($ip); $country_name = ucwords(strtolower($wp_ip2c->country_name)); if ($display) echo $country_name; return $country_name; } function wp_ip2c_getCountryCode2 ($display=0,$ip="") { global $wpdb, $wp_ip2c; $refresh = 1; if (!$ip) { $ip = $_SERVER['REMOTE_ADDR']; $refresh = 0; } if (!$wp_ip2c || $refresh) $wp_ip2c = wp_ip2c_getdb ($ip); $code = strtolower($wp_ip2c->country_code2); if ($display) echo $code; return $code; } function wp_ip2c_getCountryCode3 ($display=0,$ip="") { global $wpdb, $wp_ip2c; $refresh = 1; if (!$ip) { $ip = $_SERVER['REMOTE_ADDR']; $refresh = 0; } if (!$wp_ip2c || $refresh) $wp_ip2c = wp_ip2c_getdb ($ip); $code = strtolower($wp_ip2c->country_code3); if ($display) echo $code; return $code; } /** * SAMPLE USAGE : * You are from */