How to block Chinese IPs and Chinese browsers from accessing your self-built website

Image[1]-How to block Chinese IP and Chinese browsers from accessing your self-built website - Photon Flux | Professional WordPress Repair Service, Global Reach, Fast Response

Blocking Chinese IP Addresses

Before blocking Chinese IP addresses, you need to know your own IP address. This can be done by visiting WhatIsMyIPAddress to get your IP address. After determining your IP address, you can follow the steps below to block Chinese IP addresses.

Step 1: Get a list of China IP addresses

There are two ways to get a list of China IP addresses:

  1. Manual collection. You can search the Internet for a list of Chinese IP addresses and collect them manually, which can be tedious.
  2. Use the online service. It is possible to use ip2location maybe IPIP.NET and other online services to get a list of IP addresses in China. These services will provide a text file containing a list of all IP addresses in China.

Step 2: Configure the firewall on the server

After obtaining the list of Chinese IP addresses, you need to configure a firewall on your server. If you are using Linux, you can configure the firewall using the iptables command to configure the firewall. The following is a sample command:

cssCopy codesudo iptables -A INPUT -s 1.0.0.0/8 -j DROP

This command will block the 1.0.0.0 until (a time) 1.255.255.255 All IP addresses between the

You can use a loop to iterate through all Chinese IP addresses and block them all. Below is a sample script:

bashCopy code#!/bin/bash

# Get a list of Chinese IP addresses
wget -O cn.txt "https://www.ip2location.com/download/?token=YOUR_TOKEN&file=px5"

# Block all Chinese IP addresses
while read ip; do
  sudo iptables -A INPUT -s "$ip" -j DROP
done < cn.txt

Save the above script as block_cn_ips.sh file, then run it to block all IP addresses in China.

Blocking Chinese Browsers

If you want to block Chinese browsers, you can do so by checking the request's Accept-Language header to achieve this. Below is a sample PHP code that can be placed in the header of your website's homepage.

phpCopy code<?php
// 检查Accept-Language头,如果包含"zh"则重定向到其他页面
if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) && strstr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 'zh')) {
    header('Location: /other-page');
    exit;
}
?>

The code above checks the request for the Accept-Language Whether the header contains "zh" or not, if it does it will redirect to another page. You can redirect other pages


Contact Us
Can't read the tutorial? Contact us for a free answer! Free help for personal, small business sites!
Customer Service
Customer Service
Tel: 020-2206-9892
QQ咨询:1025174874
(iii) E-mail: info@361sale.com
Working hours: Monday to Friday, 9:30-18:30, holidays off
© Reprint statement
This article was written by Harry
THE END
If you like it, support it.
kudos1 share (joys, benefits, privileges etc) with others
commentaries sofa-buying

Please log in to post a comment

    No comments