ASP 連接Access數(shù)據(jù)庫(kù)的登陸系統(tǒng)
一、基本目標(biāo)
首先在Access數(shù)據(jù)庫(kù)Database.mdb中存在著用戶信息表test:
編寫一個(gè)登陸系統(tǒng),如果用戶輸入的用戶名在表中沒(méi)有,則提示“查無(wú)此人”,如果輸入密碼錯(cuò)誤,則提示“密碼錯(cuò)誤”
如果用戶輸入的用戶名與密碼都正確,則跳轉(zhuǎn)到登陸成功頁(yè)
登陸成功頁(yè)在普通情況下,不允許通過(guò)輸入網(wǎng)址就能訪問(wèn)
二、基本思想
使用asp的session對(duì)象確保了用戶名與密碼的傳遞。
彈出部分使用了javascript的腳本語(yǔ)言,使用asp對(duì)用戶信息表進(jìn)行查詢。
站點(diǎn)的基本結(jié)構(gòu)如下:
三、制作過(guò)程
整個(gè)站點(diǎn)使用utf-8碼保證不會(huì)亂碼,所以每一頁(yè)在頁(yè)頭必須有<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />,如果使用DW的高版本則自動(dòng)添加,低版本請(qǐng)把gb2312改成utf-8,記事本自便。
1、登陸頁(yè)面login.html僅僅是一個(gè)表單的靜態(tài)頁(yè)面。關(guān)鍵是用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登陸驗(yàn)證頁(yè)面是本系統(tǒng)最核心的頁(yè)面
<%@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傳過(guò)來(lái)的兩個(gè)信息用變量保存起來(lái)username=Request.Form("username")password=Request.Form("password")"數(shù)據(jù)庫(kù)是上一級(jí)目錄的Database.mdb%><%db="../Database.mdb""連接數(shù)據(jù)庫(kù)指定動(dòng)作,這段必須獨(dú)立地占用一個(gè)<%%>否則在某些情況下IE8會(huì)出錯(cuò)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("查無(wú)此人");window.location.href = "login.html";</script><%"否則拿查出來(lái)的密碼,與用戶輸入的密碼作對(duì)比,看是否一致"查出來(lái)的密碼必須先用一個(gè)變量接住,在ASP中不能直接比較elsedbpwd=rs("password")"如果不一致,則彈窗,ASP沒(méi)有!=,表示不等于請(qǐng)用<>if password<>dbpwd then%><script>alert("密碼錯(cuò)誤");window.location.href = "login.html";</script><%else"如果用戶名密碼都輸入正確,則有此用戶,timeout是為了防止用戶非正常退出的,如果5分鐘沒(méi)有任何操作則判定其已經(jīng)退出,ok是正常登陸的標(biāo)志Session.Timeout=5Session("username")=usernameSession("login")="ok"%><script>alert("登陸成功");window.location.href = "success.asp";</script><%end ifend if"用完數(shù)據(jù)庫(kù)記得關(guān)rs.closeset rs=nothingconn.closeset conn=nothing%></body></html>
3、success.asp
沒(méi)什么好說(shuō)的,關(guān)鍵是看他是否有正常登陸標(biāo)志,login的內(nèi)容是否為ok,沒(méi)有則將其彈出登陸頁(yè)面
<%@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("請(qǐng)正常登陸!");window.location.href = "login.html";</script><%elseResponse.Write("歡迎登陸,"+Session.Contents("username"))end if%><a href="exit.asp">正常退出</a></body></html>
4、exit.asp退出處理頁(yè)面
<%@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立即超時(shí),并且移除所有sessionSession.AbandonSession.Contents.RemoveAll()%><script>window.location.href = "login.html";</script></body></html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
相關(guān)文章:
1. Java8內(nèi)存模型PermGen Metaspace實(shí)例解析2. chat.asp聊天程序的編寫方法3. 手把手教你使用Java來(lái)編寫ASP組件(5)4. AspNetCore&MassTransit Courier實(shí)現(xiàn)分布式事務(wù)的詳細(xì)過(guò)程5. ASP 百度主動(dòng)推送代碼范例6. asp文件如何打開7. ASP實(shí)現(xiàn)類似hashMap功能的類8. 使用HttpClient消費(fèi)ASP.NET Web API服務(wù)案例9. ASP.NET MVC使用JSAjaxFileUploader插件實(shí)現(xiàn)單文件上傳10. asp中將字符串轉(zhuǎn)數(shù)字的函數(shù)小結(jié)
