美文网首页
做了CDN加速的ASP网站获取用户真实IP程序

做了CDN加速的ASP网站获取用户真实IP程序

作者: ONEDAYLOG | 来源:发表于2020-05-11 16:44 被阅读0次

    现在在移动、电信、联通三大运营商的竞争下,网站为了能在三大运营商下均能正常访问一般都会选择开启CDN加上。CDN加速开启后获取的IP就会成为CDN节点的IP这样非常不利于防止数据注入和唯一性的判断。

    写了下asp取真实IP的代码,搭环境测试无代理、一级或多级代理的情况,可以正常获取

    function checkip(checkstring)'用正则判断IP是否合法 
        dim re1 
        set re1=new RegExp 
        re1.pattern="^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$" 
        re1.global=false 
        re1.Ignorecase=false 
        checkip=re1.test(checkstring) 
        set re1=nothing 
    end function
    
    function get_cli_ip()
        '取真实IP函数,先 HTTP_CLIENT_IP 再 HTTP_X_FORWARDED_FOR 再 REMOTE_ADDR 
        dim client_ip 
        if checkip(Request.ServerVariables("HTTP_CLIENT_IP"))=true then 
            get_cli_ip = checkip(Request.ServerVariables("HTTP_CLIENT_IP")) 
        else 
            MyArray = split(Request.ServerVariables("HTTP_X_FORWARDED_FOR"),",") 
            if ubound(MyArray)>=0 then 
                client_ip = trim(MyArray(0)) 
                if checkip(client_ip)=true then 
                    get_cli_ip = client_ip 
                    exit function 
                end if 
            end if 
            get_cli_ip = Request.ServerVariables("REMOTE_ADDR") 
        end if 
    end function
    

    原来的站点停运,整合到简书
    2016年8月5日

    相关文章

      网友评论

          本文标题:做了CDN加速的ASP网站获取用户真实IP程序

          本文链接:https://www.haomeiwen.com/subject/nyaunhtx.html