by mikcheck » Tue Apr 06, 2010 5:53 pm
tnx, here's working code, topic solved :D
//******************************************//
<?php
function locateIp($ip){
$d = file_get_contents("http://www.ipinfodb.com/ip_query.php?ip=$ip&output=xml&timezone=false");
//Use backup server if cannot make a connection
if (!$d){
$backup = file_get_contents("http://backup.ipinfodb.com/ip_query.php?ip=$ip&output=xml&timezone=false");
$answer = new SimpleXMLElement($backup);
if (!$backup) return false; // Failed to open connection
}else{
$answer = new SimpleXMLElement($d);
}
$ip = $answer->Ip;
$country_code = $answer->CountryCode;
$country_name = $answer->CountryName;
$region_name = $answer->RegionName;
$city = $answer->City;
$zippostalcode = $answer->ZipPostalCode;
$latitude = $answer->Latitude;
$longitude = $answer->Longitude;
//Return the data as an array
return array('ip' => $ip, 'country_code' => $country_code, 'country_name' => $country_name, 'region_name' => $region_name, 'city' => $city, 'zippostalcode' => $zippostalcode, 'latitude' => $latitude, 'longitude' => $longitude);
}
//Usage example
//REDIR USER BY CITY
//RETRIVE USER IP
$ip = $_SERVER['REMOTE_ADDR'];
$ip_data = locateIp($ip);
//echo "IP : " . $ip_data['ip'] . "\n";
//echo "Country code : " . $ip_data['country_code'] . "\n";
//echo "Country name : " . $ip_data['country_name'] . "\n";
//echo "Region name : " . $ip_data['region_name'] . "\n";
//echo "City : " . $ip_data['city'] . "\n";
//echo "Zip/postal code : " . $ip_data['zippostalcode'] . "\n";
//echo "Latitude : " . $ip_data['latitude'] . "\n";
//echo "Longitude : " . $ip_data['longitude'] . "\n";
//CHECK CITY NAME THEN REDIR
$city2redir = $ip_data['city'];
//echo "" . $city2redir . "\n";
if ($city2redir == "Milan") {
header('Location: ./milano/');
}
elseif ($city2redir == "Naples") {
header('Location: ./napoli/');
}
elseif ($city2redir == "Rome") {
header('Location: ./rome/');
}
else {
header('Location: ./no_city_in_db/');
}
//***************************//