Jump to content

Most Optimised way of Blocking Whole Country...


natasha_thomas

Recommended Posts

Folks,

 

I want to block China, Korea, Russia & India from visiting my sites.

 

If i do it the .htaccesss way then there will be more than 2000 Lines of IP Ranges in my .htaccess.

 

Sample:

 

My Question is, will it not slow down my site load as each load will do a Lookup of IP first. No?

 

If so, is there a better way to Block these countries from visiting my sites?

 

I am on Shared hosting account and i have one VPS too, i want this solution for both of these Servers.

 

Cheers

Natasha T

Link to comment
Share on other sites

Natasha, I need to do exactly the same thing. I need to block certain countries because the site is not ready for them yet. I am still to get round to doing it though as I have other matters that need resolving first. However, I tested blocking one country through the .htaccess once and I found load times to increase by a noticeable amount. There is a login page on the site I am working on and I thought of perhaps placing a script with all the banned country IP's into a php and calling it from the index page. This would mean the index page takes a longer time to load but the rest of the site would run fast and the rest of this site cannot be accessed without logging in. I have done no further testing or research into if this is possible however. Good luck and keep us posted. I would love to know how/if you succeed in doing this.

Link to comment
Share on other sites

Natasha, I need to do exactly the same thing. I need to block certain countries because the site is not ready for them yet. I am still to get round to doing it though as I have other matters that need resolving first. However, I tested blocking one country through the .htaccess once and I found load times to increase by a noticeable amount. There is a login page on the site I am working on and I thought of perhaps placing a script with all the banned country IP's into a php and calling it from the index page. This would mean the index page takes a longer time to load but the rest of the site would run fast and the rest of this site cannot be accessed without logging in. I have done no further testing or research into if this is possible however. Good luck and keep us posted. I would love to know how/if you succeed in doing this.

 

Yes, this workaround make sense for that purpose of urs, but mine is more of a generic purpose where all content are public, i believe it will imapct site load noticably.

 

I really am looking for some expert advise on this requirement.

 

Anyone out there please pitch in to guide us....

 

Cheers

NT

Link to comment
Share on other sites

I am not sure if this could be a good way, but what if you use the user language instead of a zillion ip addresses.

 

If you know how to detect the language you only have to check if it's one of those 4 cases. Ofcourse people can spoof the useragent, but so can they with ipaddresses.

 

I found a script for you, it's meant for something different, but i bet the right components are in there to get it started.

http://techpatterns.com/downloads/scripts/php_language_detection.txt

 

Hope this helps!

Link to comment
Share on other sites

I am not sure if this could be a good way, but what if you use the user language instead of a zillion ip addresses.

 

If you know how to detect the language you only have to check if it's one of those 4 cases. Ofcourse people can spoof the useragent, but so can they with ipaddresses.

 

I found a script for you, it's meant for something different, but i bet the right components are in there to get it started.

http://techpatterns.com/downloads/scripts/php_language_detection.txt

 

Hope this helps!

 

 

Cheers Freakiy... Looks interesting....

 

Not sure how reliable/stable this solution is but its creative.

 

 

Link to comment
Share on other sites

Well i am pretty sure it's faster! and has the same level of reliability, Ip addresses aren't reliable. So if you realy want to block people, let them pay  for access

 

and is this done for spam attacks, use tokens and captcha and logins

edit: and airstrikes ::)

Link to comment
Share on other sites

I quickly made this hope it can be of help.

 

<?php
if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) ){

                $languages = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] );
	// $languages = ' fr-ch;q=0.3, da, en-us;q=0.8, en;q=0.5, fr;q=0.3';
	// need to remove spaces from strings to avoid error
	$languages = str_replace( ' ', '', $languages );
	$languages = explode( ",", $languages );
                // do something when you don't like the language
                switch ($languages[0]) { // first array item
                    case 'ko': // korea
                        echo "do something or set a variable";
                        break;
                    case 'ru-md'||'ru': // russia
                        echo "do something or set a variable";
                        break;
                    case 'zh-cn'||'zh-hk'||'zh-mo'||'zh-sg'||'zh-tw'||'zh': // china
                        echo "do something or set a variable";
                        break;
                    case 'ne': // india
                        echo "do something or set a variable";
                        break;
                    default:
                       echo "good";                         
                }
}


?>

 

-edit i used that script i linked, as far as the language codes, there are quite some more in there, so in case you might want to have a look at it.

Link to comment
Share on other sites

Hmm, the script i posted above doesn't work for some reason. Here is one that does ::)

I used in_array() instead.

 

<?php
if ( isset( $_SERVER["HTTP_ACCEPT_LANGUAGE"] ) ){

                $languages = strtolower( $_SERVER["HTTP_ACCEPT_LANGUAGE"] );
	// $languages = ' fr-ch;q=0.3, da, en-us;q=0.8, en;q=0.5, fr;q=0.3';
	// need to remove spaces from strings to avoid error
	$languages = str_replace( ' ', '', $languages );
	$languages = explode( ",", $languages );
                // do something when you don't like the language           
                $lang_array = array('ko','ru-md','ru','zh-cn','zh-hk','zh-mo','zh-sg','zh-tw','zh','ne');
                
                if (in_array($languages[0], $lang_array)){
                    echo 'bad stuff';
                }else{
                    echo 'good stuff';
                }
}

?>

Link to comment
Share on other sites

  • 2 weeks later...

I came upon your thread the other day when i hit upon a similar problem. .htaccess was slowing my site down quite considerably.

 

I have now implemented a different way to block countries from getting into my site.... this is the thread i started about the issue: http://www.phpfreaks.com/forums/index.php?topic=327949.msg1543877#msg1543877

 

Basically:

prep:

1. create a mysql table containing ip addresses and their country of origin (these guys i found ok: ip-to-country)

implement

1. use php to get the IP of the person visiting your page.

2. check the said ip to see if it falls in any of the ip ranges stored in the mysql table

3.

  i)if the result of step 2 was yes: check the country of origin then show

        a)limited content or redirect accordingly for the countries you wish to ban and set a session['ip_access'] = 0

        b)show the site as intended to the countries you wish and set session['ip_access'] = 1

  ii)if the resullt to step 2 was no: decide whether to show the site or not.

4. next time the user loads one of the pages, first check if(!empty(session['ip_access']))

  i) if not empty then if == 1 show site, if == 0 do not show site..... simples

 

This way i have found to be a million miles quicker than using .htaccess

 

thanks to the guys posting on my thread, got it working with relatively no problems

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.