WordPress Plugin: “Block Countries”
April 16, 2007 by Jason · 66 Comments
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.
don't know why you block Vietnam???
safari,
http://www.jasonlitka.com/2007/04/11/to-the-jerk-stealing-my-content/
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.
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...
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.
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.
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!
Thanks Dave.
Thanks!
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!
@Ante,
I'm glad to hear that you got it working.
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.
@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
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!
@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.
Thanks Jason!
@Rod,
I've updated the zip file. The code now works with the 2/3/2008 database (and I would assume, future updates).
i updating both files, but i only see united kingdom too.
Yeah - I figured I was just doing something wrong and was going to try a re-install - because I still have the same problem.
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
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.
@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".
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.
@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.
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
@Xem,
I'm not real familiar with SOCKS proxies so that may be true.
Block Countries doesn't seem to work with WordPress 2.5.
Any plans for update?
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!
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?
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?
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!
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?
i'm really please i found this website. I have learned a lot by reading here. keep up good work.
this is certainly pleased
machines wheel of slot fortune of wheel slot fortune download
Awesome, just what I wanted. Some scum bags are stealing my content as well. Nice plug-in. This would do the trick on the theft. Thanks for the stuff. Keep up the awesome work.
Great, I needed this one. Had few troubles with my earlier blog and I had to move to a new one. And this is one plug-in I really needed. Had a hard time with the earlier blog. Too bad I didn’t come across this earlier. It would have saved me so much of time and money. Sigh! Oh well, I have it now. Thanks a lot
Nice plug-in. I was looking for something similar. I had this problem with few people spamming my site from few countries. Didn't know how to stop it until now 🙂 thanks or the useful plug-in. Keep it up. You rock man.Cheers
Neat. that's one awesome plug-in. I was looking for a similar plug-in for my personal blog. Spamming sucks. And manually deleting them sucks more. Anyway, thanks again. Cheers
Nice post. Spam comments are a headache. This plugin will help to reduce them. Very useful. Thanks for sharing the plugin with us. Keep up the good work.
Interesting plug-in. Well it's somewhat not fair to use such plug-in and block the whole country because of few certain people. but there are sometimes you just need to do it. This would be needed for a similar instance.
That's a really useful plug-in. At times there is not other alternative other than to ban the whole country from the blog. This is a really good plug-in for that use.
well i would block romainia because most of the spam comments comes from that country.
also most spam comes from small countries.
Reasons why I would use this.