教你从左到右匹配一部分

原始代码从左到右匹配一部分:

dim aa, iplist, ip, found
aa = "11.9.67.180"
iplist = array("99.88", "110.52", "43.80.235", "11.9.")
found = false

for each ip in iplist
    if instr(aa, ip) > 0 then
        found = true
        exit for
    end if
next

if found then
    response.write "ok"
    response.end
end if
登录后复制

改动后只能完全匹配:

dim aa, iplist, ip, found, dbiplist
aa = request.servervariables("remote_addr")

dbiplist = rs("ip")

iplist = split(dbiplist, ",")

found = false

for each ip in iplist
    ip = trim(ip)
    if instr(aa, ip) > 0 then
        found = true
        exit for
    end if
next

if found then
    response.write "ok"
    response.end
end if
登录后复制

要同时处理两种数据库内容格式,需修改如下:

Dim aa, ipList, ip, found, dbIPList
aa = Request.ServerVariables("REMOTE_ADDR")

dbIPList = Rs("ip")

' 增加一个去除双引号的操作,确保两种数据库内容格式都能正常处理
dbIPList = Replace(dbIPList, """, """)

ipList = Split(dbIPList, ",")

found = False

For Each ip In ipList
    ip = Trim(ip)
    If InStr(aa, ip) > 0 Then
        found = True
        Exit For
    End If
Next

If found Then
    Response.Write "ok"
    Response.End
End If
登录后复制

以上就是如何改进IP地址匹配代码以支持多种数据库格式?的详细内容,更多请关注慧达安全导航其它相关文章!

点赞(0)

评论列表 共有 0 条评论

暂无评论
立即
投稿
发表
评论
返回
顶部