1. 程式人生 > >【ASP】連線Access資料庫的登陸系統

【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>