首页 /技术区/源码技术
只屏蔽指定IP段,不查地区、不需要外部接口
10月25日 09:40
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<%
Dim ip
ip = Request.ServerVariables("REMOTE_ADDR")
' 屏蔽单个IP示例
Dim banIP
banIP = Array("39.184.208.56","2.2.2.2")
Dim i
For i=0 To UBound(banIP)
    If ip=banIP(i) Then
        Response.Status="403 Forbidden"
        Response.Write "禁止访问"
        Response.End
    End If
Next
%>

全部回复(5)
  • 引用沙发
    网站通过配置web.config屏蔽IP访问

    网站通过配置web.config屏蔽IP访问
    将以下代码复制另存为web.config文件,其中的IP可以替换和增加为需要屏蔽的IP,支持IP段屏蔽,上传到网站根目录即可生效。(仅限windows IIS系统)

    需要屏蔽的IP地址只需要在文件中增加一行 <add ipAddress="需要屏蔽的IP"/>保存即可。

    其中 <add ipAddress="127.255.255.0" subnetMask="255.255.255.0"/> 表示屏蔽了整个 127.255.255.0/24 段的访问

    <?xml version="1.0" encoding="UTF-8"?>

    <configuration>

        <system.webServer>

    <security>

       <ipSecurity allowUnlisted="true">

           <clear/>

           <add ipAddress="127.0.0.1"/>

           <add ipAddress="127.255.255.0" subnetMask="255.255.255.0"/>

       </ipSecurity>

    </security>

    </system.webServer>

    </configuration>

  • 引用板凳
    屏蔽某个地区不让进网站代码
    <script type="text/javascript" src="https://r.inews.qq.com/api/ip2city?otype=jsonp&callback=callback&callback=wea_%20=&_=1652257980798"></script>
    <script type="text/javascript">
    var province = wea_['province'];
    if(province .indexOf('浙江') >= 0)
    {
    window.location.href="http://locahost11111111:1234";
    }
    else
    {
    }
    </script>
  • 引用3楼

    **IIS web.config 规则(只允许中国大陆 + 港澳台访问,国外直接 403)**,不需要改 ASP 页面源码,性能远好于 ASP 脚本调用接口。


    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
      <system.webServer>
        <rewrite>
          <rules>
            <rule name="BlockOverSeaIP" stopProcessing="true">
              <match url=".*" />
              <conditions>
                <!-- 这里依赖:CDN/防火墙传入IP地区标记,原生IIS本身不带IP库! -->
                <!-- 纯IIS原生是无法仅凭这个配置识别国家,必须搭配支持IP地理的CDN(如阿里云、腾讯云CDN),由CDN传递国家头 -->
                <add input="{HTTP_CDN_IP_COUNTRY}" pattern="^(?!CN|HK|MO|TW)$" />
              </conditions>
              <action type="CustomResponse" statusCode="403" statusReason="Forbidden" statusDescription="仅允许国内访问" />
            </rule>
          </rules>
        </rewrite>
      </system.webServer>
    </configuration>

  • 引用4楼

    如果你没有 CDN,只能继续用 ASP 版本,给你【包含港澳台放行版】,直接复制放 ASP 头部:

    <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
    <%
    Dim ip,xmlhttp,url,json,country
    ip = Request.ServerVariables("REMOTE_ADDR")
    url = "https://ipapi.co/" & ip & "/json/"

    Set xmlhttp = Server.CreateObject("MSXML2.XMLHTTP.6.0")
    xmlhttp.Open "GET",url,False
    xmlhttp.setRequestHeader "User-Agent","Mozilla/5.0"
    On Error Resume Next
    xmlhttp.Send
    If Err.Number<>0 Then
        Set xmlhttp=Nothing
    End If

    country = ""
    If xmlhttp.Status=200 Then
        json = xmlhttp.ResponseText
        Dim s,e
        s = InStr(json,"country_code"":""") + Len("country_code"":""")
        e = InStr(s,json,"""")
        If s>0 And e>s Then
            country = Mid(json,s,e-s)
        End If
    End If
    Set xmlhttp=Nothing

    ' CN大陆 HK香港 MO澳门 TW台湾 放行,其他国家拦截
    Dim allowCode
    allowCode = Array("CN","HK","MO","TW")
    Dim isAllow,i
    isAllow=False
    For i=0 To UBound(allowCode)
        If UCase(country)=allowCode(i) Then
            isAllow=True
            Exit For
        End If
    Next

    If Not isAllow Then
        Response.Status="403 Forbidden"
        Response.Write "<h2>抱歉,仅允许国内及港澳台地区访问</h2>"
        Response.End
    End If
    %>


  • 引用5楼
    只屏蔽指定 IP 段
    <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
    <%
    Dim ip
    ip = Request.ServerVariables("REMOTE_ADDR")
    ' 屏蔽单个IP示例
    Dim banIP
    banIP = Array("1.1.1.1","2.2.2.2")
    Dim i
    For i=0 To UBound(banIP)
        If ip=banIP(i) Then
            Response.Status="403 Forbidden"
            Response.Write "禁止访问"
            Response.End
        End If
    Next
    %>




    网站通过配置web.config屏蔽IP访问

    网站通过配置web.config屏蔽IP访问
    将以下代码复制另存为web.config文件,其中的IP可以替换和增加为需要屏蔽的IP,支持IP段屏蔽,上传到网站根目录即可生效。(仅限windows IIS系统)

    需要屏蔽的IP地址只需要在文件中增加一行 <add ipAddress="需要屏蔽的IP"/>保存即可。

    其中 <add ipAddress="127.255.255.0" subnetMask="255.255.255.0"/> 表示屏蔽了整个 127.255.255.0/24 段的访问

    <?xml version="1.0" encoding="UTF-8"?>

    <configuration>

        <system.webServer>

    <security>

       <ipSecurity allowUnlisted="true">

           <clear/>

           <add ipAddress="127.0.0.1"/>

           <add ipAddress="127.255.255.0" subnetMask="255.255.255.0"/>

       </ipSecurity>

    </security>

    </system.webServer>

    </configuration>

  • 首页 | 电脑版 |