PHP前端开发

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

百变鹏仔 1个月前 (12-15) #PHP
文章标签 多种

教你从左到右匹配一部分

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

dim aa, iplist, ip, foundaa = "11.9.67.180"iplist = array("99.88", "110.52", "43.80.235", "11.9.")found = falsefor each ip in iplist    if instr(aa, ip) > 0 then        found = true        exit for    end ifnextif found then    response.write "ok"    response.endend if

改动后只能完全匹配:

dim aa, iplist, ip, found, dbiplistaa = request.servervariables("remote_addr")dbiplist = rs("ip")iplist = split(dbiplist, ",")found = falsefor each ip in iplist    ip = trim(ip)    if instr(aa, ip) > 0 then        found = true        exit for    end ifnextif found then    response.write "ok"    response.endend if

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

Dim aa, ipList, ip, found, dbIPListaa = Request.ServerVariables("REMOTE_ADDR")dbIPList = Rs("ip")' 增加一个去除双引号的操作,确保两种数据库内容格式都能正常处理dbIPList = Replace(dbIPList, """, """)ipList = Split(dbIPList, ",")found = FalseFor Each ip In ipList    ip = Trim(ip)    If InStr(aa, ip) > 0 Then        found = True        Exit For    End IfNextIf found Then    Response.Write "ok"    Response.EndEnd If