3

I am having problem with blocking countries with geoip. When I use my hosting account utility to block countries, it creates the below script in a .htaccess. The problem is it does not seem to be working (added US but was not blocked).

GeoIPEnable on
RewriteEngine on
   RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AU$   [OR]
   RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$   [OR]
   RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CN$   
   RewriteRule ^.*$ - [F] 

So, I added [OR] to the last country in the list but now it blocks everything including countries not in the list. Here I tried deleting US but still received 403 message.

GeoIPEnable on
RewriteEngine on
   RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^AU$   [OR]
   RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CA$   [OR]
   RewriteCond %{ENV:GEOIP_COUNTRY_CODE} ^CN$   [OR]
   RewriteRule ^.*$ - [F]
Francesco Casula
  • 26,184
  • 15
  • 132
  • 131
E G
  • 37
  • 1
  • 10
  • Or to the end of the list behaved as it should. How did you verify that it was not working? – Bailey Parker May 05 '13 at 02:57
  • US is not in the list but I get the 403 forbidden message when try to access the site. (I am in US). – E G May 05 '13 at 05:28
  • Is that with or without the trailing `[OR]`? – Bailey Parker May 05 '13 at 06:04
  • @PhpMyCoder That is with the [OR] at the end of the list. When without OR at the end of the list it does not block at all (added US to test). When with the OR at the end of the list it blocks even countries not in the list (deleted US to test). – E G May 06 '13 at 04:10
  • 1
    Are you sure that your IP address shows up as being based in the US? You might have an IP that GeoIP can't resolve (especially if your web host is using the free version which is not as accurate). Try adding the following rule to your test setup: `RewriteRule .* /test?code=%{ENV:GEOIP_COUNTRY_CODE} [R=temporary]` and see what value of code you get in the query string when your browser is redirected to this fake path. If you don't see `code=US` in the address bar, then your IP does not resolve to "US" under GeoIP on your web host. – Bobulous May 06 '13 at 13:18

1 Answers1

1

I did it without using .htaccess. I just placed the following snippet of code in header file. This might helpful for you:

<?php 
include("includes/lcs/geoip.inc"); //Also load this file for its functions.
$serverIP=$_SERVER['REMOTE_ADDR']; //Get the IP address.
//echo $serverIP;
$gi = geoip_open("includes/lcs/GeoIP.dat",GEOIP_STANDARD); //Get the 2 letter country designation for the IP address.
$restricted_countries = array("US"); // list of restricted countries like array("NP", "US"); 
$country = geoip_country_code_by_addr($gi, $serverIP);


$restricted_pages = array("country_login","country_contact","country_password_forgotten","account"); 


if( (in_array($country,$restricted_countries)) and (!in_array($_GET['main_page'],$restricted_pages) ) )
{   
    header("location: https://www.yoursite.com/country_contact.html");

}
?>

This simply redirects to the country_contact.html for restricted country.

Vijaya Pandey
  • 4,252
  • 5
  • 32
  • 57
  • 1
    Wow! Thanks so much... Is there a new version to this? I don't know if the old GeoIP.dat database is still updated, GeoIP2 is more up-to-date - do you have the code for that? – Bob Aug 21 '16 at 00:10