Results 1 to 5 of 5

Thread: How to block a country in wordpress?

  1. #1
    iowadawg's Avatar
    iowadawg is offline Free Cell Champion
    Join Date
    May 2010
    Location
    Not in Texas
    Posts
    2,142
    Blog Entries
    4
    Thanks
    170
    Thanked 365 Times in 314 Posts

    How to block a country in wordpress?

    Seems like my blogs are favorites of the Chinese.

    So how do I block China from my blogs?

  2. #2
    TopDogger's Avatar
    TopDogger is offline Über Hund
    Join Date
    Jan 2009
    Location
    Hellfire, AZ
    Posts
    3,103
    Thanks
    349
    Thanked 918 Times in 702 Posts
    I had to block much of China on my server using the firewall. Don't do it with a plugin. Using a plugin is much slower than using a firewall or the .htaccess file.

    You can block IPs using the .htaccess file, but China has so many IP addresses that it will negatively impact your site's performance. It takes time to check the IP every time a page is requested.

    If you are getting hit with comment spam. About 95% originates in China, regardless of the spam type. WordPress does store the IP address for each comment. Unfortunately, you can only see the IPs if you use phpMyAdmin or another database manager.

    If you want to try blocking all of China, here is how to do it using the .htaccess file.

    Block China Web Traffic IP Addresses and Chinese Hackers

    My recommendation is to identify the specific IPs involved in the attack and only block those. Almost all will be coming from China's university cities.

    The web could be greatly improved if ALL Chinese IP were blocked on the Internet backbone. There is zero value in this traffic and most hacks and malicious email could be stopped.
    "Democracy is two wolves and a lamb voting on what to have for lunch. Liberty is a well-armed lamb contesting the vote." -- Benjamin Franklin


  3. #3
    iowadawg's Avatar
    iowadawg is offline Free Cell Champion
    Join Date
    May 2010
    Location
    Not in Texas
    Posts
    2,142
    Blog Entries
    4
    Thanks
    170
    Thanked 365 Times in 314 Posts
    Thought so.
    Bottom line is that it is a pain in the butt.

    Seems the china traffic is more for the pictures and graphics than anything else.
    Checked and nothing is hotlinked.
    But page specific urls are used.

    Will continue on.

    Thank you for your response.

  4. #4
    UncleP's Avatar
    UncleP is offline The perfect face for radio
    Join Date
    Nov 2009
    Location
    Blighty
    Posts
    218
    Thanks
    20
    Thanked 91 Times in 61 Posts
    You could use blockscript, the free version will still redirect any country specified even after the trial period expires, there is a worpress plugin version I've used myself in the past, don't know how or if it'll impact site performance though.

    Or you could try the 'proxy' method using maxmind geoip, I've just tried this on a wordpress site and it seems to work for me, but try at your own risk and make backup copies of any files changed of course

    First upload the maxmind files, GeoIP.dat + geoip.inc (zip file attached or download both from maxmind.com) into your domain root folder.

    Then edit your /index.php file in the sites root folder and add this code at the beginning of the file, underneath the the "<?php" part:
    Code:
    // THIS IS THE GEOIP COUNTRY BLOCKING CODE
    
    include('geoip.inc'); //this file must exist in your directory
    
    $gi = geoip_open('GeoIP.dat',GEOIP_STANDARD);
    
    // get the ip of the visitor
    $addr = getenv('REMOTE_ADDR');
    // translate ip to a country
    $country = geoip_country_name_by_addr($gi, $addr);
    // close the geo database
    geoip_close($gi);
    
    $badcountry = array("China", "Anonymous Proxy", "Satellite Provider", "Other");
    
    // redirect them if country in above string
    if(in_array($country, $badcountry))
    header('Location: http://www.baidu.com'); //enter a url or page on your site
    
    // END OF COUNTRY BLOCKING CODE
    It will redirect all Chinese (and anonymous) traffic to baidu.com, though you can change the redirect url to one of your choosing, just edit. You may have to add the code again after any WP updates if they replace the index file, but it is currently installed on thesitecheck.com and that still works. I tried redirecting UK traffic (where I am) and it did the job, so I took that bit out and left the rest. You can also add any other country you need or want to block as well just by editing the file and adding the country name in quotes, seperated by commas.

    I can't say if this will impact site performance either tbh, I have no way to measure it, but if you get a lot of Chinese traffic and it stops that then overall site performance may well improve because of it, plus it'll save a heap of bandwidth.

    There's a couple of other plugins I use too, one is Bad Behavior which keeps out bad bots, the other is WP-Copyright-Protection which disables any right click copy'n paste efforts.

    Edit: Not sure it'll stop specific pages loading, maybe there's a better place to put the code to work on all pages, I did try the template header.php but that didn't work, but then I'm not a wordpress kinda guy, perhaps someone else has a better idea for it.

    'Nother edit: Just tried it with different pages and it seems to redirect those as well, so could be an easy solution.
    Attached Files Attached Files
    If I can't be a good example, I'll just have to be a terrible warning...

  5. #5
    iowadawg's Avatar
    iowadawg is offline Free Cell Champion
    Join Date
    May 2010
    Location
    Not in Texas
    Posts
    2,142
    Blog Entries
    4
    Thanks
    170
    Thanked 365 Times in 314 Posts
    Will try these and see what happens.

    Thank you.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •