ASP限制IP访问

ASP限制IP访问,第1张

1.ASP限制IP访问代码

<%

''获取访问者的地址

ip=Request.ServerVariables("REMOTE_ADDR")

''允许的IP地址段为10.0.0.0~10.50.50.255

allowip1="10.0.0.0"

allowip2="10.50.10.70"

response.write checkip(ip,allowip1,allowip2)

function checkip(ip,allowip1,allowip2)

dim check(4)

checkip=false

ipstr=split(ip,".")

allow1=split(allowip1,".")

allow2=split(allowip2,".")

if cint(allow1(0))>cint(allow2(0)) then ''判断IP地址段是否合*

response.write "禁止访问"

exit function

end if

for i=0 to ubound(ipstr)

if cint(allow1(i))<cint(allow2(i)) then

if cint(allow1(i))=cint(ipstr(i)) then

check(i)=true

checkip=true

exit for

else

if cint(ipstr(i))<cint(allow2(i)) then

check(i)=true

checkip=true

exit for

else

if cint(ipstr(i))>cint(allow2(i)) then

check(i)=false

checkip=false

exit for

else

check(i)=true

checkip=true

end if

end if

end if

else

if cint(allow1(i))>cint(ipstr(i)) or cint(allow1(i))<cint(ipstr(i)) then

check(i)=false

checkip=false

if i<>ubound(ipstr) then

exit for

end if

else

check(i)=true

end if

end if

next

if (check(0)=true and check(1)=true and check(2)=true and check(3)=false) and (cint(allow2(2))>cint(ipstr(2))) then

checkip=true

end if

end function

%>

2.用户IP限制

<%'用户IP限制

function LockIP(sip)

dim str1,str2,str3,str4

dim num

LockIP=false

if isnumeric(left(sip,2)) then

str1=left(sip,instr(sip,".")-1)

sip=mid(sip,instr(sip,".")+1)

str2=left(sip,instr(sip,".")-1)

sip=mid(sip,instr(sip,".")+1)

str3=left(sip,instr(sip,".")-1)

str4=mid(sip,instr(sip,".")+1)

if isNumeric(str1)=0 or isNumeric(str2)=0 or isNumeric(str3)=0 or isNumeric(str4)=0 then

else

num=cint(str1)*256*256*256+cint(str2)*256*256+cint(str3)*256+cint(str4)-1

sql="select count(*) from LockIP where ip1 <="&num&" and ip2 >="&num&""

set rs_ip=conn.execute(sql)

if rs_ip(0)>0 then

LockIP=true

end if

set rs_ip=nothing

end if

end if

end function%>

<%if LockIP(Request.ServerVariables("REMOTE_ADDR")) then

response.write "<script>alert('您的IP:"&request.servervariables("remote_addr")&"已经被限制不能访问,请和管理员联系')location.href='about:blank'</script><script>window.close()</script>"

response.end

end if

%>

<%

noip=Request.Servervariables("HTTP_X_FORWARDED_FOR")

If noip="" Then noip=Request.Servervariables("REMOTE_ADDR")

set rst11=server.createobject("adodb.recordset")

sql="select * from fuck where userip='"&noip&"'"

rst11.open sql,conn,1,1

if not rst11.eof and not rst11.bof then

response.write "<script>alert('您的IP:"&request.servervariables("remote_addr")&"已经被限制不能访问,请和管理员联系')location.href='about:blank'</script><script>window.close()</script>"

response.end

end if

%>

<%

IP_Address = Request.ServerVariables("HTTP_X_FORWARDED_FOR")

If IP_Address = "" Then

IP_Address = Request.ServerVariables("REMOTE_ADDR")

end if

%>

这样可以取到访问者的ip地址,判断一下是不是那两个ip,如果是的话就不做操作,不让访问的话做个跳转

<%

response.Write"<script>alert('只对内部开放!')location.href('提示页面.asp')</script>"

%>

我想你肯定是获取的外网的IP,学校把一个公网IP分给了很多寝室,而寝室的内网IP则是不一样的。可以把获取IP那部分改下。

request.ServerVariables("REMOTE_ADDR")这是获取外网IP的

下面这段代码是获取内网IP的,我在网上找的,你也可以谷歌或百度一下。

function

get_ip()

{

global

$_SERVER

if

(

isset($_SERVER["HTTP_X_FORWARDED_FOR"])

)

{

$realIP

=

$_SERVER["HTTP_X_FORWARDED_FOR"]

}

elseif

(

isset($_SERVER["HTTP_CLIENT_IP"])

)

{

$realIP

=

$_SERVER["HTTP_CLIENT_IP"]

}

else

{

$realIP

=

$_SERVER["REMOTE_ADDR"]

}

return

$realIP

希望能帮到你。


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/360562.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-10
下一篇2023-05-10

发表评论

登录后才能评论

评论列表(0条)

    保存