WordPress Plugin: “Block Countries”

As was mentioned in my "To the jerk stealing my content" rant, I've recently developed a WordPress plugin that can be used to block certain countries from accessing a blog. After a few days of tweaking, I believe that it's finally ready for a public release. I give you... "Block Countries"!

I'm actually surprised at how quickly this plugin was completed. I started with ZERO idea as to how to build a plugin for WordPress but within a couple hours I had a bare-bones plugin that accomplished what I needed it to do. Of course, it then took most of the weekend to clean it up to the point where I would feel comfortable telling someone, "Why don't you try this out on your blog?".

The zip file with the plugin can be found at the bottom of this post. You'll need to unzip the archive into your "wp-content/plugins" directory and then download the newest IP-to-Country CSV file from IP-to-Country.com (direct link to download page) and unzip that to the "block-countries" folder that should have been created inside your "plugins" folder when you unpacked my plugin.

The next step for installation on your blog is to enter wp-admin and enable the plugin on the "Plugins" page. When you do that, an additional table should automatically be created in your wordpress database. This table is going to be used to store the data from the above-mentioned CSV file for faster access.

After the plugin has been activated, select the "Options" menu item, followed by "Block Countries". Assuming that you successfully uploaded both the plugin PHP file and the CSV data file, you should receive a message to the effect of, "the table is empty but that the CSV was found". If that is the case, click the "Update" button and the CSV should be loaded into your database.

Once the screen refreshes, you should have access to 3 true/false option boxes and one large list box filled with a list of 200 or so country names. At this point, all that remains to do is to activate the scanning options by changing their values to "True" (the top one should catch +95% of users, the last two are to try and block users using proxy servers) and then to select which countries you wish to ban from your blog pages. Click "Update" again once you're done.

One thing worth mentioning, although I doubt that this will affect many of you, but if your site is behind a reverse proxy or load balancer then make sure that it is either forwarding the originating IP addresses or that you activate the second and third scanning options in my plugin, otherwise you may not be able to block anyone.

Finally, if you find this plugin useful, consider providing a link back to my site so that others can find it.

UPDATE (2/12/2008): Updated to 1.01 to handle the changed line endings in the CSV.

Download the "Block Countries" WordPress Plugin

41 Responses to “WordPress Plugin: “Block Countries””


  1. 1 safari

    don't know why you block Vietnam???

  2. 2 Jason
  3. 3 testing

    I'm trying this and get the following error when trying to load the csv:
    LOAD DATA LOCAL INFILE '/home/.../public_html/val/wp-content/plugins/block-countries/ip-to-country.csv' INTO TABLE `wp_iptocountry` FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY ' ';

    Just wondering if maybe the csv was corrupted in download since it's big and I'm on a dialup....or if you have any ideas.

    Thanks.

  4. 4 Ozh

    Blocking a country... This has to be the dumbest idea of the month.

    Not only it's totally vain since it's easy as pie to use a public anonymous or less public open proxy to defeat IP based filtering, but it's also uber-jerkish to want to block an entire country when a single jerk annoys you.

    Jeez, people sometimes...

  5. 5 Jason

    If you don't like it, don't install it.

    As to blocking an entire country to get rid of one user, I don't consider that a problem. This person was stealing content from my site and passing it off as his own. On top of that, he was hot-linking files from my server.

  6. 6 Jason

    testing,

    That doesn't seem like the entire error. What was the rest of the message? Did it tell you why it failed on that command? Also, were those "..." part of the message or did you put those there to censor the post?

    It could be that the download was corrupt (although if the zip file extracted it probably wasn't) but it's more likely that your MySQL user account doesn't have the "FILE" permission which is required for the "LOAD DATA LOCAL INFILE" statement.

  7. 7 Dave

    I think having the option to block countries is a good idea for those that want to protect their data, not to mention this was the type of thing that was done just to say it could be done and Jason was the one to do it. Good Job Jason!

  8. 8 Jason

    Thanks Dave.

  9. 9 Tia O'Connor

    Thanks!

  10. 10 Ante

    Had the same problem as "testing" above (19th April), i.e. I didn't have "FiLE" permission. However I solved it by entering phpmyadmin and manually import the .csv-file. After some trial and error I had to change from the default ";" to "," (without quotes) as field divider. Now everything works fine. Thanks Jason!

  11. 11 Jason

    @Ante,

    I'm glad to hear that you got it working.

  12. 12 mark

    Great plugin! I modified it to allow a login page to override it for someone in a blocked country. Basically, I have a website for another country and I need the country I'm in to be blocked. Well, naturally that blocked me as well.

    So with a bit of modification and a new seperate login page, I now go to the login page and put in my username and password, it then creates a cookie which my modifications check for to see if I am allowed in the website despite being in the blocked country.

    A simple adjustment allows for the expiration time of the cookie to be changed.

    Let me know if you would like my modifications, I can either email them to you, or I could post the simple instructions on the comments if you would like.

  13. 13 Jason

    @Mark,

    Sure, send over the changes you've made and I'll see about integrating them. Use the comment form on my "About" page.

    http://www.jasonlitka.com/about

  14. 14 Rod

    Can't wait to get this to work as someone is stealing my content as well. Unfortunately, only one country is showing up in the list (United Kingdom). Any idea as to what I'm doing wrong?

    Thanks for taking the time to make this plug in!

  15. 15 Jason

    @Rod,

    It's possible that that the newest version of their DB has changed formats slightly. I'll take a look. If it has, I'll post a link to the version I have and then update my code to work with their newer DB.

  16. 16 Rod

    Thanks Jason!

  17. 17 Jason

    @Rod,

    I've updated the zip file. The code now works with the 2/3/2008 database (and I would assume, future updates).

  18. 18 mark

    i updating both files, but i only see united kingdom too.

  19. 19 Rod

    Yeah - I figured I was just doing something wrong and was going to try a re-install - because I still have the same problem.

  20. 20 ed seward

    What do you mean when you state "it's more likely that your MySQL user account doesn't have the "FILE" permission which is required for the "LOAD DATA LOCAL INFILE" statement."

    I have the MySQL user permissions set to "dba". Does this take care of this problem?

    I get the "An error occurred while processing the CSV file." error message when I attempt to use the update function.

    Thanks,

    Ed

  21. 21 JC

    My website provide services in my city, locally. The other day I noticed in my logs that a third party WP plugin was being exploited. Most of the IP ranges were from outside the U.S. Since my website caters to the U.S. only, I had no problem blocking other countries. Thanks for the plugin.

  22. 22 Jason

    @ed,

    There is no 'dba' permission in MySQL. Are you sure you're not using MSSQL or something else?

    @mark & Rod,

    What version does your plugin's page say? It should be 1.01. If that is the case, download the IP-to-Country database, place it in the plugin folder, check the little box on the Plugin properties page, and then hit "Update".

  23. 23 Rod

    Hey Jason,

    Still no luck - and it says it's 1.01. Maybe I'm doing something else wrong - but it still only shows the United Kingdom.

  24. 24 Jason

    @Rod,

    You're using the 2/4/2008 database and you've told it to reload? I just tested it again and it worked for me so there's really no reason why running the update shouldn't fix it for you.

    About the only thing I can think of is to try removing the plugin and database table and then reinstalling the plugin.

  25. 25 Xem Phim Online

    I think it is a good ideal, but i am not sure if it "actualy work" since those jerks can still use Sock Proxy to get in, and you are some what limit legits people to visit your sites.

    Just my 2cents

  26. 26 Jason

    @Xem,

    I'm not real familiar with SOCKS proxies so that may be true.

  27. 27 Dr. Lead Based Pain

    Block Countries doesn't seem to work with WordPress 2.5.

    Any plans for update?

  28. 28 circuits

    I think having the option to block countries is a good idea for those that want to protect their data, not to mention this was the type of thing that was done just to say it could be done and Jason was the one to do it. Good Job Jason!

  29. 29 Jason

    I haven't tested my plugins (or my site) with WP 2.5 yet but plan to over the next couple days. Once I do, I'll post an update.

    So I know going in, what errors, if any, are you receiving under WordPress 2.5?

  30. 30 Klaids

    In WordPress 2.5.1 Block Countries Options - only one country is possible to select and it`s Great Britain :) How to change country and select multiple?

  31. 31 April

    This is very strange. I have never thought about installing any kind of plugin like this. However I have had people hotlink images without permission but these were people in Western countries adding my photos to their myspace pages etc. They don't seem to realize how anoying that it!

  32. 32 Lisa

    I too am installing this and getting UK showing up. I installed the plug-in, installed the csv. file into that, went to settings and activated everything. No luck. Do I need to add this csv to SQL or anything?

  33. 33 marketing

    i\'m really please i found this website. I have learned a lot by reading here. keep up good work.

  1. 1 Weblog Tools Collection » Blog Archive » WordPress Plugin Releases for 04/18
  2. 2 WPのプラグイン:Block Countries
  3. 3 New Wordpress theme & plugin releases. Something ‘fresh’ | RandLIFE
  4. 4 Blogtology » Blog Archive » WP Plugs April 21, 2007
  5. 5 WeblogToolsCollection Español » Blog Archive » Plugins de WordPress publicados 18/4
  6. 6 Listado de plugins del dia at Escuela De Blogs
  7. 7 Block Countries | ????????
  8. 8 Wordpress spraudnis “Block Countries” | Klaid`s blog

Leave a Reply

Note: This post is over a year old. You may want to check later in this blog to see if there is new information relevant to your comment.

By submitting a comment here you grant this site a perpetual license to reproduce your words and name/web site in attribution. Use of a non-personal web site or blog in the "Web Site" field and/or leaving a comment that is off-topic or inappropriate may result in the comment being edited or removed at the discretion of the site owner.






Close
Powered by ShareThis