topical media & game development
professional-search-11-seophp-include-simple-geo-target.inc.php / php
<?php
/*
// +----------------------------------------------------------------------+
// | SimpleGeoTarget |
// | Class for targeting content for specific geographical regions |
// | http://www.SEOEgghead.com |
// +----------------------------------------------------------------------+
// | Copyright (c) 2004-2006 Jaimie Sirovich <jsirovic@gmail.com> |
// +----------------------------------------------------------------------+
*/
// load configuration file
require_once('config.inc.php');
// simple geo-targeting class
class SimpleGeoTarget
{
// returns true if the specified ip is located in ip = '')
{
// retrieve the IP of the visitor if one wasn't provided
ip) ? _SERVER['REMOTE_ADDR'];
// transform the IP into its long version
ip));
// build the SQL query that obtains the country code of the specified IP
ip AND end_ip_numeric >= 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
q);
tmp);
// close database connection
mysql_close(result) return false;
// return the region
return (country_code, false otherwise
function isRegion(ip = '')
{
// retrieve the region
ip);
// return false if the region code couldn't be found
if (!country_code == csv_file_handle = fopen(GEO_TARGETING_CSV, 'r');
// continue only if the geo db file was opened successfully
if (!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");
// lock the simple_geo_target file for writing
mysql_query('LOCK TABLES simple_geo_target WRITE');
// remove all existing entries from the table
q);
// parse each record from the geo-targeting file and save it to the database
while ((csv_file_handle, 10000, ",")) !== false)
{
SimpleGeoTarget::_insert(data[1], data[3], data[5]);
}
// unlock the tables
mysql_query('UNLOCK TABLES');
// close database connection
mysql_close(csv_file_handle);
}
function _insert(end_ip_text, end_ip_numeric, country_name)
{
// escape input data
start_ip_text);
end_ip_text);
start_ip_numeric);
end_ip_numeric);
country_code);
country_name);
// build and execute the INSERT query
start_ip_text', 'start_ip_numeric', " .
"'country_code', 'q);
}
}
?>
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.