How to Block a Whole Country on Your Joomla Website?

We have explained before on how to block a specific IP or range of IPs from accessing your Joomla website. Since we have published that article, we had many requests asking us on how to block a specific country from accessing a Joomla website.

Now why would anyone want to block a whole country (or even several countries) from accessing his or her Joomla website?

There are several reasons for this, the most important ones are:

  • There is too much spam coming from that particular country: The owner’s website is flooded with spam (such as comment spam and referral spam) coming from that country.
  • There is too much fraud coming from that particular country: For example, the owner of the Joomla website runs a VirtueMart store and most transactions coming from that country are fraudulent.

  • Too many invalid clicks on ads: Some Joomla websites are content websites, where owners write content and make money when people click on the ads that are placed on their pages. If the owner receives a lot of invalid clicks, then his revenue will be affected (the invalid clicks will not only be discounted, but will also affect the valid clicks). Some countries are known for sending mostly invalid clicks.

  • Preventive measure against hacking: A few countries (because of the lax regulations when it comes to enforcing Internet security) are infamous of being the source of hacking attacks on many websites.

  • The country is sending too much automated traffic: This automated traffic can be automated spam, scraper bots, etc…

There are also many other reasons for a Joomla website owner to block traffic from a specific country…

Now, the question is, can this be done? And if yes, then how can it be done?

Well, we’re happy to say that this can be done, but it’s not easy… Here’s a step by step guide on how to block traffic from a whole country to your Joomla website:

  1. Download the IP to Country (also referred to as Geo IP) database available here.
  2. Import the above file into a MySQL table (an empty MySQL table).

  3. Copy the following code into your index.php file:

    function ipToNumerical($ip){
    		$arrIP = explode('.', $ip);
    		$multiplier = 1;
    		$numericalIP = 0;
    		for ($i = count($arrIP) - 1; $i >= 0; $i--){
    			$numericalIP += $arrIP[$i] * $multiplier;
    			$multiplier *= 256;
    		}
    		return $numericalIP;
    }

    The above script will translate an IP address to its numerical equivalent (for example: the numerical value of 174.91.225.215 is 2925257175 [215 x 1 + 225 x 256 + 91 x 256 x 256 + 174 x 256 x 256 x 256])

  4. Now that you have the script to get the numerical value of the IP, add another script to your index.php that will get the IP of the visiting user, translate that IP into its numerical value, and check the country that this value is assigned to (this can be done by doing a small query that will check which row has the range that this numerical value falls in). If you get a country that you are banning, then just call the function die.

  5. If you need help doing the above, then we can do it for you, it’ll only take us 8 hours and our fees are very fair! Just contact us and see how fast, efficient, and friendly we are!

    Note: An even better way of doing this is doing it through a plugin that runs on every page of the website before everything else. This is how we will do it for you! We have mentioned the above process because it’s simply easier.

3 Responses to “How to Block a Whole Country on Your Joomla Website?”
  1. Pingback by How to Protect Your Content on Your Joomla Website | itoctopus — January 25, 2012 @ 4:36 pm

    […] to know which IP is stealing content from your Joomla website. You can either block that IP or block the country that hosts this IP altogether. Again, if the person is persistent, then he might try to use a proxy to illegally gain […]

  2. Comment by Ken — March 8, 2016 @ 1:08 am

    Hi,

    Is there a simpler way in case I just want to allow U.S. IP address (50 states) to access a site hosted in U.S.?

    Thanks,
    Ken

  3. Comment by Fadi — April 13, 2016 @ 3:09 pm

    Hi Ken,

    There are extensions that can do that for you. Search the JED (the Joomla Extensions Directory). Keep in mind that any extension doing the blocking must be updated constantly since some IPs rotate between countries.

Leave a comment