Ajax software
Free javascripts
↑
Main Page
// load configuration file
require_once(‘config.inc.php’);
// simple geo-targeting class
class SimpleGeoTarget
{
// returns true if the specified ip is located in $country_code, false otherwise
function getRegion($ip = ‘’)
{
// retrieve the IP of the visitor if one wasn’t provided
$ip = ($ip) ? $ip : $_SERVER[‘REMOTE_ADDR’];
// transform the IP into its long version
$ip = sprintf(“%u”, ip2long($ip));
// build the SQL query that obtains the country code of the specified IP
$q = “SELECT geo_target_data.* FROM geo_target_data WHERE “ .
“start_ip_numeric <= $ip AND end_ip_numeric >= $ip”;
// connect to MySQL server
$dbLink = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD)
or die(“Could not connect: “ . mysql_error());
// connect to the seophp database
mysql_select_db(DB_DATABASE) or die(“Could not select database”);
// execute the query
$tmp = mysql_query($q);
$result = mysql_fetch_assoc($tmp);
// close database connection
mysql_close($dbLink);
// return false if no database records for that IP were found
if (!$result) return false;
// return the region
return ($result[‘country_code’]);
}
// returns true if the specified IP is located in $country_code, false otherwise
function isRegion($country_code, $ip = ‘’)
{
// retrieve the region
$visitor_country_code = SimpleGeoTarget::getRegion($ip);
// return false if the region code couldn’t be found
if (!$visitor_country_code) return false;
// return true if the IP and the country code match, false otherwise
return ($country_code == $visitor_country_code);
}
237
Chapter 11: Cloaking, Geo-Targeting, and IP Delivery
c11.qxd:c11 11:01 237
Ajax software
Free javascripts
→