求 ASP动态网页设计数据库登录验证模块

作者&投稿:翁程 (若有异议请与网页底部的电邮联系)
怎么在动态网站上进行账号密码通过数据库验证登录~

asp+access用户登录代码新建access数据库123.mdb在数据库中建表:user表字段为:id,username,password其中username为用户名字段,password为密码字段.id是主键建两个页面:index.htm登陆页面和login.asp处理登陆页面index.html代码如下:<head><metahttp-equiv="Content-Type"content="text/html;charset=gb2312"><title>登录</title></head><body><p></p><formname="form1"method="post"action="login.asp"><tablewidth="200"border="1"align="center"cellpadding="0"cellspacing="0"><tr><tdcolspan="2"><divalign="center">登录</div></td></tr><tr><tdwidth="57"><divalign="center">用户名</div></td><tdwidth="137"valign="top"><inputname="username"type="text"id="username"size="25"></td></tr><tr><td><divalign="center">密码</div></td><tdvalign="top"><inputname="password"type="password"id="password"size="25"></td></tr><tr><tdcolspan="2"><divalign="center"><inputtype="submit"name="Submit"value="提交"></div></td></tr></table></form></body></html>login.asp网页代码如下<%dimusername,passwordusername=request.form("username")password=request.form("password")setrs=server.createobject("adodb.recordset")conn="DBQ="+server.mappath("123.mdb")+";DefaultDir=;DRIVER={MicrosoftAccessDriver(*.mdb)};"sql="select*fromuserwhereusername='"&username&"'"rs.opensql,conn,1,1ifrs.eofthenresponse.write"<script>alert('登录失败');history.back();</script>"response.endelseifrs("password")<>passwordthenresponse.write"<script>alert('登录失败');history.back();</script>"response.endendifsession("admin")=usernameresponse.write"<script>alert('登录成功');location.href='index.htm'</script>"endif%>

去素材网上找一下

这个文件保存为验证登陆文件 checklogin.asp
<%
If Request.QueryString("action")="exit" Then
Session("Mname")=""
Session("Mgrade")=""
End IF
If Session("Mname")="" or Session("Mgrade")="" Then
Response.Write("<script language='javascript'>location.href='login.asp';</script>")
End If
%>

这个是登陆的文件,login.asp 里面的具体字段 ,请根据你的需要进行修改.
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<%
Response.Buffer=true
Response.Expires=0
Response.ExpiresAbsolute=Now()-1
Response.CacheControl="no-cache"
%>
<!--#include file="include/conn.asp"-->
<!--#include file="include/function.asp"-->
<%
If Not Isempty(Request("login")) Then
txt_name=Str_filter(Request.Form("txt_name"))
txt_passwd=Str_filter(Request.Form("txt_passwd"))
If txt_name<>"" Then
Set rs=Server.CreateObject("ADODB.Recordset")
sqlstr="select Mname,Mpasswd,Mgrade from tab_manager where Mname='"&txt_name&"'"
rs.open sqlstr,conn,1,1
If rs.eof Then
Response.Write("<script lanuage='javascript'>alert('管理员名称不正确,请核实后重新输入!');location.href='login.asp';</script>")
Else
If rs("Mpasswd")<>txt_passwd Then
Response.Write("<script lanuage='javascript'>alert('密码不正确,请确认后重新输入!');location.href='login.asp';</script>")
Else
Session("Mname")=rs("Mname")
Session("Mgrade")=rs("Mgrade")
Response.Redirect("index.asp")
End If
End If
Else
errstr="请输入管理员名称!"
End If
End If
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>电子商务网站----管理员登录</title>
<link rel="stylesheet" href="css/css.css">
<script type="text/javascript">
<!--
function Mycheck(){
if(document.all.txt_name.value==""){
alert('请输入管理员名称!');return false;}
if(document.all.txt_passwd.value==""){
alert('请输入密码!');return false;}
if(document.all.verifycode.value==""){
alert('请输入验证码!');return false;}
}
-->
</script>
<style type="text/css">
<!--
.style1 {color: #CCCCCC}
-->
</style>
</head>
<body topmargin="120px">
<table width="544" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><img src="images/login_05.gif" width="546" height="98"></td>
</tr>
<tr>
<td height="150" background="images/login_07.gif"> <table width="70%" border="0" align="center" cellpadding="5" cellspacing="0">
<form name="form1" method="post" action="">
<tr>
<td width="36%" height="35" align="right">管理员:</td>
<td width="64%" height="35"><input name="txt_name" type="text" class="textbox" id="txt_name" size="18" maxlength="50"></td>
</tr>
<tr>
<td height="35" align="right">密
码:</td>
<td height="35"><input name="txt_passwd" type="password" class="textbox" id="txt_passwd" size="19" maxlength="50"></td>
</tr>
<tr>
<td height="35" colspan="2" align="center"><input name="login" type="submit" id="login" value="登 录" class="button">

<input type="reset" name="Submit2" value="重 置" class="button"></td>
</tr>
</form>
</table></td>
</tr>
<tr>
<td><img src="images/login_09.gif" width="546" height="21"></td>
</tr>
</table>
</body>
</html>

这个是需要调用到的文件 function.asp

<%
'---------- 定义转换字符函数 -----------
Function Str_filter(InString)
NewStr=Replace(InString,"'","''")
NewStr=Replace(NewStr,"<","<")
NewStr=Replace(NewStr,">",">")
NewStr=Replace(NewStr,"chr(60)","<")
NewStr=Replace(NewStr,"chr(37)",">")
NewStr=Replace(NewStr,"""",""")
NewStr=Replace(NewStr,";",";;")
NewStr=Replace(NewStr,"--","-")
NewStr=Replace(NewStr,"/*"," ")
NewStr=Replace(NewStr,"%"," ")
Str_filter=NewStr
End Function
'---------- 根据时间获取的字符串 -----------
Function GetOrderNo(dDate)
GetOrderNo = RIGHT("0000"+Trim(Year(dDate)),4)+RIGHT("00"+Trim(Month(dDate)),2)+RIGHT("00"+Trim(Day(dDate)),2)+RIGHT("00" + Trim(Hour(dDate)),2)+RIGHT("00"+Trim(Minute(dDate)),2)+RIGHT("00"+Trim(Second(dDate)),2)
End Function
'---------- 获取随机数函数 -----------
Function randStr(num)
strings="0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z"
str=split(strings,",")
for i=1 to num
Randomize
str1=str(int((ubound(str))*rnd))
returnstr=returnstr&str1
next
randStr=returnstr
End Function
%>
<script language="javascript">
//----------- 验证数字 ----------
function checkNum(Num){
var Expression=/^[1-9]+[0-9]*$/;
var re=new RegExp(Expression);
if(re.test(Num)==true){
return true;}
else{
return false;}
}
//----------- 验证E-mail地址 ----------
function checkEmail(email){
var Expression=/\w+([-+.']\w+)*\.\w+([-.]\w+)*/;
var re=new RegExp(Expression);
if(re.test(email)==true){
return true;}
else{
return false;}
}
//----------- 验证网址 ----------
function checkUrl(url){
var Expression=/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w-.\/?%&=]*)?/;
var re=new RegExp(Expression);
if(re.test(url)==true){
return true;}
else{
return false;}
}
//----------- 验证身份证号码 ----------
function checkCode(code) {
var Expression=/\d{17}[\d|X]|\d{15}/;
var re=new RegExp(Expression);
if(re.test(code)==true){
return true;}
else{
return false;}
}
//判断输入的日期是否正确
function CheckDate(INDate)
{ if (INDate=="")
{return true;}
subYY=INDate.substr(0,4)
if(isNaN(subYY) || subYY<=0){
return true;
}
//转换月份
if(INDate.indexOf('-',0)!=-1){ separate="-"}
else{
if(INDate.indexOf('/',0)!=-1){separate="/"}
else {return true;}
}
area=INDate.indexOf(separate,0)
subMM=INDate.substr(area+1,INDate.indexOf(separate,area+1)-(area+1))
if(isNaN(subMM) || subMM<=0){
return true;
}
if(subMM.length<2){subMM="0"+subMM}
//转换日
area=INDate.lastIndexOf(separate)
subDD=INDate.substr(area+1,INDate.length-area-1)
if(isNaN(subDD) || subDD<=0){
return true;
}
if(eval(subDD)<10){subDD="0"+eval(subDD)}
NewDate=subYY+"-"+subMM+"-"+subDD
if(NewDate.length!=10){return true;}
if(NewDate.substr(4,1)!="-"){return true;}
if(NewDate.substr(7,1)!="-"){return true;}
var MM=NewDate.substr(5,2);
var DD=NewDate.substr(8,2);
if((subYY%4==0 && subYY%100!=0)||subYY%400==0){ //判断是否为闰年
if(parseInt(MM)==2){
if(DD>29){return true;}
}
}else{
if(parseInt(MM)==2){
if(DD>28){return true;}
}
}
var mm=new Array(1,3,5,7,8,10,12); //判断每月中的最大天数
for(i=0;i< mm.length;i++){
if (parseInt(MM) == mm[i]){
if(parseInt(DD)>31){return true;}
}else{
if(parseInt(DD)>30){return true;}
}
}
if(parseInt(MM)>12){return true;}
return false;}
</script>

如果仍有疑问,请联系我留言.

这样,,我给你一条思路,希望你能想出来..
第一,先用SQL语句查出 admin 表里面的 管理员用户名与密码.
代码: select * from admin where username='"&request("username")&"' and password='"&request.("password")&"'

上面是查询语句,.你想想,如果管理员输入用户名与密码之后,能查询出符合条件的话,肯定会有内容输出,如果没有呢?那就代码用户名与密码都不存在啦.如果不存在的话,那就返回用户名与密码出错了..
明白了吗?
希望你能从上面得到启发,代码我就不详细写给你了.希望你能有进步...

以下是我常用的登陆验证,希望能够帮到你

<!--#include file="onn.asp"-->
<!--#include file="MD5.asp"-->
<% on error resume next
if request.Form("username")<>"" then
sql="select * from admin where username='"&request.Form("username")&"' and password='"&md5(request.Form("password"))&"'"
set rs=Conn.execute(sql)
if rs.eof and rs.bof then
error.clear
response.Write("<script>alert(""你输入的用户名或密码错误!"")</script>")
else

response.cookies("username")="yes"
response.Redirect("manage.asp")
end if
end if

%>