24

2025

-

08

限制国内访问的功能

当访问者来自 中国大陆 IP 时,网站会清空页面,并提示一句话: “此网站不对中国大陆开放访问”。 当访问者来自 非大陆 IP 时,网站会正常显示内容。


作者:

林剑伟

来源:

中企跨境

如果你打开的IP是国内那么就会清空页面显示【此网站不对中国大陆开放访问】

<script>
(function () {
  const API_URL = 'https://ipwho.is/';
  fetch(API_URL)
    .then(res => res.json())
    .then(data => {
      if (data && data.country_code === 'CN') {
        document.body.innerHTML = '<div style="padding:100px;text-align:center;font-size:18px;color:#333">此网站不对中国大陆开放访问</div>';
      }
    })
    .catch(() => {
      // 请求失败时的处理(可选)
      console.warn('[访问限制] IP 检测失败,跳过限制处理');
    });
})();
</script>

 

这个是你第一次进行了翻墙那么就会记录你是国外IP打开就会显示正常 (就算你关了翻墙也可以正常打开 因为他记录了你是国外的IP)

<script>
(function () {
  const API_URL = 'https://ipwho.is/';
  const CACHE_KEY = 'user_country_code';
  const cached = localStorage.getItem(CACHE_KEY);
  if (cached === 'CN') {
    document.body.innerHTML = '<div style="padding:100px;text-align:center;font-size:18px;color:#333">此网站不对中国大陆开放访问</div>';
    return;
  }
  if (cached) return; 
  fetch(API_URL)
    .then(res => res.json())
    .then(data => {
      if (data && data.country_code) {
        localStorage.setItem(CACHE_KEY, data.country_code);
        if (data.country_code === 'CN') {
          document.body.innerHTML = '<div style="padding:100px;text-align:center;font-size:18px;color:#333">此网站不对中国大陆开放访问</div>';
        }
      }
    })
})();
</script>