Home > CRE Loaded > CRE Loaded Block unwanted countries traffic by IP

CRE Loaded Block unwanted countries traffic by IP

October 31st, 2009

One of site i worked on, was having a lot of crawler and normal visitor activities, from non targeted countries. For example it was having a lot of visitors and un friendly bots (crawlers) from India. This traffic was useless for the site because site was to serve Europe and US, still it was consuming a lot of processing and bandwidth.

The solution was to block all the traffic from such countries but could not find and extension to block specified countries from our site. Finally we decided to write a simple extension that can detect country of visitor and block the visitor if its country is in our block list.

The site was based on CRE Loaded so i thought it would be useful extension for other people who experiences similar issues. The extension is very simple and created using RCI, so it dose not get disturbed by any CRE Loaded updates.

The extension requires free country to IP database from MaxMind and its PHP API. These can be found on http://www.maxmind.com/app/geoip_country and http://geolite.maxmind.com/download/geoip/api/php/ respectively.

Installation of this extension is easy and include following steps

Step 1: Copy file GeoIP.dat (IP to country database) and geoip.inc (API) in includes/ directory under your installation.
Setp 2: Run following SQL queries on your database. This will add configuration to enable/disable this extension and will let you specify list of countries to block.

INSERT INTO configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, last_modified, date_added, use_function, set_function) VALUES ('Enable Block Countries', 'BLOCK_COUNTRIES_ENABLE', 'false', 'Block traffic and spiders from specific countries. by http://blog.wasimasif.com', '1', '999', NOW(), NOW(), '', 'tep_cfg_select_option(array(\'true\', \'false\'),') ;
INSERT INTO configuration (configuration_title, configuration_key, configuration_description, configuration_group_id, sort_order, last_modified, date_added, use_function, set_function) VALUES ('Blocked Countries List', 'BLOCK_COUNTRIES_LIST', 'Comma separated list of country codes to block traffic from. For example AL,AM will block all traffic from Albania and Armenia. By http://blog.wasimasif.com', '1', '1000', NOW(), NOW(), '', 'tep_cfg_textarea(') ;

Step 3: Create a new file blockcountry_applicationtop_bottom.php in includes/runtime/applicationtop/ folder and paste the following code in this file.

<?php
/**
* Block contries by http://blog.wasimasif.com/
* Blocks unwanted traffic on your site
* @since 2009-10-28
*/
if(BLOCK_COUNTRIES_ENABLE == 'true' && BLOCK_COUNTRIES_LIST != ''){ // check if block countries is enabled
if(!isset($_SESSION['cok']) || intval($_SESSION['cok']) == 0){ // We have not checked country yet
$ip_address = tep_get_ip_address(); // get IP
include(DIR_FS_INCLUDES."geoip.inc"); // include geoIP API
$gi = geoip_open(DIR_FS_INCLUDES."GeoIP.dat",GEOIP_MEMORY_CACHE); // open GeoIP database
$country_code = strtoupper(geoip_country_code_by_addr($gi, $ip_address)); // Get country code
geoip_close($gi); // Close the IP database

$blocked_countries = explode(‘,’,strtoupper(BLOCK_COUNTRIES_LIST));

for($i=0; $i < count($blocked_countries);$i++)
$blocked_countries[$i] = trim($blocked_countries[$i]); // avoid any trailing spaces etc

if($country_code != “” && in_array($country_code,$blocked_countries)){ // country code is in block list
header(‘HTTP/1.1 403 Forbidden’);
die(“<h1>Forbidden</h1>”);
}
else{
$_SESSION['cok'] = 1; // register this in session
}
}
}
// end block countries
?>

Now you are done with the installation. Now enable the extension and provide list of blocked country in Admin > Configuration > My Store. You have to specify a list of comma separated two latter country codes to block.

Use the code on your own risk no warranties what so ever is provided. The code is tested with CRE Loaded 6.4 Pro on linux but should work with 6.3.x and windows.

Categories: CRE Loaded Tags:
Comments are closed.