首页 > 开发 > Asp > 正文

ASP 连接Access数据库的登陆系统

2019-10-20 15:00:14
字体:
来源:转载
供稿:网友

一、基本目标

首先在Access数据库Database.mdb中存在着用户信息表test:

ASP,Access,数据库,登陆系统

编写一个登陆系统,如果用户输入的用户名在表中没有,则提示“查无此人”,如果输入密码错误,则提示“密码错误”

ASP,Access,数据库,登陆系统

如果用户输入的用户名与密码都正确,则跳转到登陆成功页

ASP,Access,数据库,登陆系统

登陆成功页在普通情况下,不允许通过输入网址就能访问

ASP,Access,数据库,登陆系统

二、基本思想

使用asp的session对象确保了用户名与密码的传递。

弹出部分使用了javascript的脚本语言,使用asp对用户信息表进行查询。

站点的基本结构如下:

ASP,Access,数据库,登陆系统

三、制作过程
整个站点使用utf-8码保证不会乱码,所以每一页在页头必须有<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />,如果使用DW的高版本则自动添加,低版本请把gb2312改成utf-8,记事本自便。 

1、登陆页面login.html仅仅是一个表单的静态页面。关键是用post方法传递信息,Action是到login.asp

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>login</title></head><body><form method="post" action="login.asp"> username:<input type="text" name="username" />password:<input type="password" name="password" /><input type="submit" value="login" /></form></body></html>

 2、login.asp登陆验证页面是本系统最核心的页面

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>login</title></head><body><%'向把login.html传过来的两个信息用变量保存起来username=Request.Form("username")password=Request.Form("password")'数据库是上一级目录的Database.mdb%><%db="../Database.mdb"'连接数据库指定动作,这段必须独立地占用一个<%%>否则在某些情况下IE8会出错Set conn = Server.CreateObject("ADODB.Connection")conn.Open "driver={Microsoft Access Driver (*.mdb)};pwd=admin;dbq=" & Server.MapPath(db) %><%Set rs = Server.CreateObject( "ADODB.Recordset" )'看表中是否有此usernamesql = "select * from test where username='"+username+"';"rs.open sql,conn,1,3'如果什么都查不到,弹窗,弹回login.htmlif (rs.bof and rs.eof) then%><script>alert("查无此人");window.location.href = "login.html";</script><%'否则拿查出来的密码,与用户输入的密码作对比,看是否一致'查出来的密码必须先用一个变量接住,在ASP中不能直接比较elsedbpwd=rs("password")'如果不一致,则弹窗,ASP没有!=,表示不等于请用<>if password<>dbpwd then%><script>alert("密码错误");window.location.href = "login.html";</script><%else'如果用户名密码都输入正确,则有此用户,timeout是为了防止用户非正常退出的,如果5分钟没有任何操作则判定其已经退出,ok是正常登陆的标志Session.Timeout=5Session("username")=usernameSession("login")="ok"%><script>alert("登陆成功");window.location.href = "success.asp";</script><%end ifend if'用完数据库记得关rs.closeset rs=nothingconn.closeset conn=nothing%></body></html>

 3、success.asp
 没什么好说的,关键是看他是否有正常登陆标志,login的内容是否为ok,没有则将其弹出登陆页面 

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>欢迎登陆</title></head><body><%if Session.Contents("login")<>"ok" then %><script>alert("请正常登陆!");window.location.href = "login.html";</script><%elseResponse.Write("欢迎登陆,"+Session.Contents("username"))end if%><a href="exit.asp">正常退出</a></body></html>

4、exit.asp退出处理页面 

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>正在退出...</title></head><body><%'所有session立即超时,并且移除所有sessionSession.AbandonSession.Contents.RemoveAll()%><script>window.location.href = "login.html";</script></body></html>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持CuoXin错新网。


注:相关教程知识阅读请移步到ASP教程频道。
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表