http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip
CREATE DATABASE country;
USE DATABASE country;
CREATE TABLE iptocountry
(ip_from int(4) unsigned,
ip_to int(4) unsigned,
country_code2 char(2),
country_code3 char(3),
country_name varchar(50));
<?
define("SOURCE_FILENAME", "ip-to-country.csv");
define("SEPERATOR_COL", ",");
define("SEPERATOR_ROW", "\n");
mysql_connect("localhost", "user", "pass");
mysql_select_db("country");
mysql_query("DELETE FROM iptocountry") or die (mysql_error());
/*
Open file for reading
*/
$fhFile = fopen(SOURCE_FILENAME, "r") or die ("Error opening ".SOURCE_FILENAME);
$sData = fread($fhFile, filesize(SOURCE_FILENAME));
$aData = explode(SEPERATOR_ROW, $sData);
foreach($aData as $sRow) {
$aRow = explode(SEPERATOR_COL, $sRow);
/* Prepare values for query */
foreach($aRow as $sKey => $sValue) {
$sValue = str_replace("\"", "", $sValue);
$sValue = addslashes($sValue);
$aRow[$sKey] = "'".$sValue."'";
}
list($sIpFrom, $sIpTo, $sCountryCode2, $sCountryCode3, $sCountryName) = $aRow;
$sQuery = "INSERT INTO iptocountry
(ip_from,
ip_to,
country_code2,
country_code3,
country_name)
VALUES
(".$sIpFrom.",
".$sIpTo.",
".$sCountryCode2.",
".$sCountryCode3.",
".$sCountryName."
);";
mysql_query($sQuery) or die (mysql_error());
}
?>
<?
$REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
$asia=array("AF","BD","BT","IO","BN","KH","CN","TP","GU","HK","IN","ID","JP","KZ","KP","KR","KG","LA","MO","MY","MV","MN","MM","NR","NP","MP","PK","PW","PH","SG","LK","TJ","TW","TH","TM","SU","UZ","VM");
// Establishing a database connection
$dbh=mysql_connect("localhost","user","pass");
mysql_select_db("country");
// Query for getting visitor countrycode
$country_query = "SELECT country_code2,country_name FROM iptocountry ".
"WHERE IP_FROM<=inet_aton('$REMOTE_ADDR') ".
"AND IP_TO>=inet_aton('$REMOTE_ADDR') ";
// Executing above query
$country_exec = mysql_query($country_query);
// Fetching the record set into an array
$ccode_array=mysql_fetch_array($country_exec);
// getting the country code from the array
$country_code=$ccode_array['country_code2'];
$numElements = count($asia);
for($counter=0; $counter < $numElements; $counter++)
{
if ($country_code==$asia[$counter]) {
header("Location: http://www.google.com/");
}
}
// Closing the database connection
mysql_close($dbh);
?>
*the exceptional businesses of our esteemed moderators