登录
首页 >  文章 >  php教程

如何改进IP地址匹配代码以支持多种数据库格式?

时间:2024-12-31 10:07:04 304浏览 收藏

一分耕耘,一分收获!既然都打开这篇《如何改进IP地址匹配代码以支持多种数据库格式?》,就坚持看下去,学下去吧!本文主要会给大家讲到等等知识点,如果大家对本文有好的建议或者看到有不足之处,非常欢迎大家积极提出!在后续文章我会继续更新文章相关的内容,希望对大家都有所帮助!

如何改进IP地址匹配代码以支持多种数据库格式?

教你从左到右匹配一部分

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

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

今天带大家了解了的相关知识,希望对你有所帮助;关于文章的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>