Content

ASP 连接Access数据库的登陆系统(.net连接access数据库)不要告诉别人

文章摘要

这篇文章主要介绍了如何通过VBSCRIPT编写一个用户登录功能。代码通过获取用户的用户名和密码,连接到指定的数据库(上一级目录的Database.mdb),并在其中查询用户是否存在。如果用户名或密码不正确,会弹窗提示信息并重定向到登录页面;如果用户信息正确,则提示登录成功并重定向到成功页面。代码中还包含了对数据库连接的正确关闭,确保资源的高效利用。

 <%@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=”https://www.jb51.net/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” ) ‘看表中是否有此username sql=”select * from test where username='”+username+”‘;” rs.open sql,conn,1,3 ‘如果什么都查不到,弹窗,弹回login.html if (rs.bof and rs.eof) then %> <script> alert(“查无此人”); window.location.href=”https://www.jb51.net/article/login.html”; </script> <% ‘否则拿查出来的密码,与用户输入的密码作对比,看是否一致 ‘查出来的密码必须先用一个变量接住,在ASP中不能直接比较 else dbpwd=rs(“password”) ‘如果不一致,则弹窗,ASP没有!=,表示不等于请用<> if password<>dbpwd then %> <script> alert(“密码错误”); window.location.href=”https://www.jb51.net/article/login.html”; </script> <% else ‘如果用户名密码都输入正确,则有此用户,timeout是为了防止用户非正常退出的,如果5分钟没有任何操作则判定其已经退出,ok是正常登陆的标志 Session.Timeout=5 Session(“username”)=username Session(“login”)=”ok” %> <script> alert(“登陆成功”); window.location.href=”https://www.jb51.net/article/success.asp”; </script> <% end if end if ‘用完数据库记得关 rs.close set rs=nothing conn.close set conn=nothing %> </body> </html>