![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](https://www.361sale.com/wp-content/uploads/2023/03/proxy-4620558_1920-1024x766.jpg)
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:
- Manual collection. You can search the Internet for a list of Chinese IP addresses and collect them manually, which can be tedious.
- 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
Link to this article:https://www.361sale.com/en/4341The article is copyrighted and must be reproduced with attribution.






















![Emoji[wozuimei]-Photonflux.com | Professional WordPress repair service, worldwide, rapid response](https://www.361sale.com/wp-content/themes/zibll/img/smilies/wozuimei.gif)
![Emoticon[baoquan] - Photon Wave Network | Professional WordPress Repair Services, Worldwide Coverage, Rapid Response](https://www.361sale.com/wp-content/themes/zibll/img/smilies/baoquan.gif)

No comments