| // +----------------------------------------------------------------------+ */ // 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); } /* methods used for importing the geo-targeting database */ // imports MaxMind's geo-targeting database into the geo_target_data table function importGeoTargetingData() { // open the geo-targeting file $csv_file_handle = fopen(GEO_TARGETING_CSV, 'r'); // continue only if the geo db file was opened successfully if (!$csv_file_handle) { echo "Could not open the geodb file."; return; } // 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"); // lock the simple_geo_target file for writing mysql_query('LOCK TABLES simple_geo_target WRITE'); // remove all existing entries from the table $q = "DELETE FROM geo_target_data"; mysql_query($q); // parse each record from the geo-targeting file and save it to the database while (($data = fgetcsv($csv_file_handle, 10000, ",")) !== false) { SimpleGeoTarget::_insert($data[0], $data[1], $data[2], $data[3], $data[4], $data[5]); } // unlock the tables mysql_query('UNLOCK TABLES'); // close database connection mysql_close($dbLink); // close the file handler fclose($csv_file_handle); } function _insert($start_ip_text, $end_ip_text, $start_ip_numeric, $end_ip_numeric, $country_code, $country_name) { // escape input data $start_ip_text = mysql_escape_string($start_ip_text); $end_ip_text = mysql_escape_string($end_ip_text); $start_ip_numeric = mysql_escape_string($start_ip_numeric); $end_ip_numeric = mysql_escape_string($end_ip_numeric); $country_code = mysql_escape_string($country_code); $country_name = mysql_escape_string($country_name); // build and execute the INSERT query $q = "INSERT INTO geo_target_data (start_ip_text, end_ip_text " . ", start_ip_numeric, end_ip_numeric, country_code, country_name)" . "VALUES ('$start_ip_text', '$end_ip_text', '$start_ip_numeric', " . "'$end_ip_numeric', '$country_code', '$country_name')"; mysql_query($q); } } ?>