网络编程
位置:首页>> 网络编程>> Asp编程>> ASP+SQL Server构建网页防火墙(2)

ASP+SQL Server构建网页防火墙(2)

  发布时间:2009-01-21 19:56:00 

标签:防火墙,sql,server,asp

三、程序设计

下面要实现的就是一个简单的页面防火墙的功能。此页面只限制本单位内部网的用户进行访问(在此假设内部网的IP地址是从10.61.96.至10.65.97.之间),如果是单位外部用户进行访问则要求输入访问用户名及密码。在此要使用到request对象的ServerVariables属性,通过它来获得环境变量的值。

程序源码(firewall.asp)如下:



<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb_2312-80">
<meta name="GENERATOR" content="Microsoft FrontPage Express 2.0">
<title>firewall.asp</title>
</head>
<body background="#800080 ">
<%
'使用Request.ServerVariables("REMOTE_ADDR")得到IP地址并保存在变量remoteip中
remoteip=Request.ServerVariables("REMOTE_ADDR")
stip=cstr(remoteip)
取得IP地址第三个段的值并保存到stip中
for i=1 to 2
stip=right(stip,len(stip)-instr(1,stip,"."))
next
stip=left(stip,instr(1,stip,".")-1)
'IP地址有效性检验及密码验证,包括两方面的内容:如果IP地址符合则通过验证;如果IP地址不符合则检验输入的用户名、密码是否正确
if (left(remoteip,5) <> "10.61" or stip<"96" or stip>"97") then
username=request.form("t1")
password=request.form("t2")
Set fs = CreateObject("Scripting.FileSystemObject")
Set thisfile = fs.OpenTextFile("dsn.txt")
db_loc=thisfile.readline
thisfile.close
cnstr=db_loc&&"uid="&&username&&";"&&"pid="&&password
on error resume next
set cn=server.createobject("adodb.connection")
cn.open cnstr
if err=3709 then %>
<p><font color="#FF0000">对不起,用户:<%=username%>没有访问权限,或密码不正确! <BR></font></p>
<form method="POST">
<p align="center">用户名:<input type="text" name="T1" size="20">口令:<input type="password" name="T2" size="20"><input type="submit" value="提交" name="B1"><input type="reset" value="全部重写" name="B2">
</p>
</form>
<%end if
cn.close
set cn=nothing%>
<%else %>
恭喜您,您已经通过了验证,可以直接使用本站点的资源!
<%end if%>
</body>
</html> 


稍微修改一下上面如IP地址等信息,该程序就可以运行了。

当然,上面只是实现在一个页面中的防火墙功能。如果一个网站有多个页面的话,可以设置一个session变量来对用户进行标志,在下面的页面中都进行判断。

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com