【ASP】連線Access資料庫的登陸系統
阿新 • • 發佈:2019-01-01
本文 屬於轉載,原文地址為: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]
- <!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登陸驗證頁面是本系統最核心的頁面
[html] view plain copy print ?
- <%@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" )
- '看錶中是否有此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 = "login.html";
- </script>
- <%
- '否則拿查出來的密碼,與使用者輸入的密碼作對比,看是否一致
- '查出來的密碼必須先用一個變數接住,在ASP中不能直接比較
- else
- dbpwd=rs("password")
- '如果不一致,則彈窗,ASP沒有!=,表示不等於請用<>
- if password<>dbpwd then
- %>
- <script>
- alert("密碼錯誤");
- window.location.href = "login.html";
- </script>
- <%
- else
- '如果使用者名稱密碼都輸入正確,則有此使用者,timeout是為了防止使用者非正常退出的,如果5分鐘沒有任何操作則判定其已經退出,ok是正常登陸的標誌
- Session.Timeout=5
- Session("username")=username
- Session("login")="ok"
- %>
- <script>
- alert("登陸成功");
- window.location.href = "success.asp";
- </script>
- <%
- end if
- end if
- '用完資料庫記得關
- rs.close
- set rs=nothing
- conn.close
- set conn=nothing
- %>
- </body>
- </html>
3、success.asp
沒什麼好說的,關鍵是看他是否有正常登陸標誌,login的內容是否為ok,沒有則將其彈出登陸頁面
[html] view plain copy print ?
- <%@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>
- <%
- else
- Response.Write("歡迎登陸,"+Session.Contents("username"))
- end if
- %>
- <a href="exit.asp">正常退出</a>
- </body>
- </html>
4、exit.asp退出處理頁面
[html] view plain copy print ?
- <%@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>