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

本文 属于转载,原文地址为:http://blog.csdn.net/yongh701/article/details/40976869

一、基本目标

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


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


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


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



二、基本思想

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

弹出部分使用了JavaScript的脚本语言

使用asp对用户信息表进行查询。

站点的基本结构如下:



三、制作过程

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

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

[html]  view plain  copy
 print ?
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  5. <title>login</title>  
  6. </head>  
  7.   
  8. <body>  
  9. <form method="post" action="login.asp">   
  10. username:<input type="text" name="username" />  
  11. password:<input type="password" name="password" />  
  12. <input type="submit" value="login" />  
  13. </form>  
  14. </body>  
  15. </html>  

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

[html]  view plain  copy
 print ?
  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  6. <title>login</title>  
  7. </head>  
  8.   
  9. <body>  
  10.   
  11. <%  
  12. '向把login.html传过来的两个信息用变量保存起来  
  13. username=Request.Form("username")  
  14. password=Request.Form("password")  
  15. '数据库是上一级目录的Database.mdb  
  16. %>  
  17. <%  
  18. db="../Database.mdb"  
  19. '连接数据库指定动作,这段必须独立地占用一个<%%>否则在某些情况下IE8会出错  
  20. Set conn = Server.CreateObject("ADODB.Connection")  
  21. conn.Open "driver={Microsoft Access Driver (*.mdb)};pwd=admin;dbq=" & Server.MapPath(db)   
  22. %>  
  23. <%  
  24. Set rs = Server.CreateObject( "ADODB.Recordset" )  
  25. '看表中是否有此username  
  26. sql = "select * from test where username='"+username+"';"  
  27. rs.open sql,conn,1,3  
  28. '如果什么都查不到,弹窗,弹回login.html  
  29. if (rs.bof and rs.eof) then  
  30. %>  
  31. <script>  
  32. alert("查无此人");  
  33. window.location.href = "login.html";  
  34. </script>  
  35. <%  
  36. '否则拿查出来的密码,与用户输入的密码作对比,看是否一致  
  37. '查出来的密码必须先用一个变量接住,在ASP中不能直接比较  
  38. else  
  39. dbpwd=rs("password")  
  40. '如果不一致,则弹窗,ASP没有!=,表示不等于请用<>  
  41. if password<>dbpwd then  
  42. %>  
  43. <script>  
  44. alert("密码错误");  
  45. window.location.href = "login.html";  
  46. </script>  
  47. <%  
  48. else  
  49. '如果用户名密码都输入正确,则有此用户,timeout是为了防止用户非正常退出的,如果5分钟没有任何操作则判定其已经退出,ok是正常登陆的标志  
  50. Session.Timeout=5  
  51. Session("username")=username  
  52. Session("login")="ok"  
  53. %>  
  54. <script>  
  55. alert("登陆成功");  
  56. window.location.href = "success.asp";  
  57. </script>  
  58. <%  
  59. end if  
  60. end if  
  61. '用完数据库记得关  
  62. rs.close  
  63. set rs=nothing  
  64. conn.close  
  65. set conn=nothing  
  66. %>  
  67. </body>  
  68. </html>  



3、success.asp

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

[html]  view plain  copy
 print ?
  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  6. <title>欢迎登陆</title>  
  7. </head>  
  8.   
  9. <body>  
  10. <%  
  11. if Session.Contents("login")<>"ok" then   
  12. %>  
  13. <script>  
  14. alert("请正常登陆!");  
  15. window.location.href = "login.html";  
  16. </script>  
  17. <%  
  18. else  
  19. Response.Write("欢迎登陆,"+Session.Contents("username"))  
  20. end if  
  21. %>  
  22. <a href="exit.asp">正常退出</a>  
  23. </body>  
  24. </html>  


4、exit.asp退出处理页面

[html]  view plain  copy
 print ?
  1. <%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
  6. <title>正在退出...</title>  
  7. </head>  
  8.   
  9. <body>  
  10. <%  
  11. '所有session立即超时,并且移除所有session  
  12. Session.Abandon  
  13. Session.Contents.RemoveAll()  
  14. %>  
  15. <script>  
  16. window.location.href = "login.html";  
  17. </script>  
  18. </body>  
  19. </html>

猜你喜欢

转载自blog.csdn.net/BoRenLiang/article/details/74917691
今日推荐