首页 > Linux操作系统 > Linux操作系统 > 用密码保护页面 (转)
首先,创建一个数据库的表,命名为tblLogin;而后创建两个域(字段名称),一个叫"UserName",另一个叫"Password"。分别对两个字段设定初始值,使你可以用这个初始值进入被保护的页面。
接下来,你需要加Session("allow") = False在global.asa文件的
Session_OnStart中。当你输入了正确的用户名及密码后,这个值将变成True,使你得以进入被保护页面。这个会话在每次访问被保护页面时均会调用。
下面,创建一个表单:
<%@ Language=VBScript %>
将含上述代码的文件命名为main.asp或者其它的名字。
在下一章中,我们将创建查询语句用以检测输入的用户名和密码与数据库中的是否一致。
现在我们创建查询语句,可以验证在表单中输入的内容是否与数据库中的内容相一致。
<%@ Language=VBScript %>
<% Response.Buffer = True %>
<% Session("allow") = True %>
<%
UserName = Request.Form("username")
Password = Request.Form("password")
'grab the form contents
Set MyConn=Server.CreateObject("ADODB.Connection")
MyConn.Open "your connection string here"
SQL = "Select * From tblLogin"
Set RS = MyConn.Execute(SQL)
If UserName = RS("UserName") AND Password = RS("Password") Then
'if there is a match then show the page
%>
Put the contents of your page here.
<%
Else
Response.Redirect "http://www.yourdomain.com/login.asp"
RS.Close
MyConn.Close
Set RS = Nothing
Set MyConn = Nothing
End If
%>
'if there was no match then make the visitor try again to login.
把含上述代码的文件命名为login.asp
最后,把下面的三行代码加入需要保护的页面的最前面中。不要把它加到第二页的代码中。
<%@ Language=VBScript %>
<% Response.Buffer = True %>
<% If session("allow") = False Then Response.Redirect "main.asp" %>
如果你的页面名字不是main.asp,请将上面代码中相应的部分改成你创建的页面的名字。
如果会话(Session)变量的值为False(假),访问者在下一步看到的将会是登录页面(或其它页面)
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10294527/viewspace-124600/ ,如需转载,请注明出处,否则将追究法律责任。