用户注册、登录程序

下载:用户注册、登录文件(自解压文件)

1 说 明

  本文介绍,一个用ASP编写的网站的注册、登录系统。
  用户注册时,须输入用户名、密码等个人信息;下一次访问时,用该用户名和密码登录,登录后成功后,方可察看有关网页内容。如果没有登录直接访问其他页面,则重定向回注册页面。
  该系统中,包含了检验和加密等功能,具有较好的实用性。

2 网页功能与代码

2.1 登录控制网页-default.asp

  功能:检查是否通过用户名、密码验证。如果,已经通过,显示“登录成功”,如果,没有通过,则重定向至登录网页-logon.asp。
  代码:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file=security.asp-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>欢迎光临</title>
</head>
<%
if session("pass")<>"action1" then '检验在session中是否有登录成功的信息
response.Redirect("logon.asp")
Response.End
end if
%>
<body>
<h1 align="center">&nbsp;</h1>
<h1 align="center">登录成功! </h1>
</body>
</html>
  提示:在实际使用时,将上述代码插入需要控制打开的网页中即可!

2.2 登录网页-logon.asp

  功能:用户界面,以及将用户填写信息发送到检验网页-validate.asp
  代码:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>注册</title>
</head>
<body>
<p> </p><p> </p>
<table width="600" border="0" align="center">
<tr>
<td><h2 align="center">用户登录</h2>
<form name="form1" method="post" action="validate.asp">
<table width="67%" border="1" align="center" cellspacing="0" bordercolor="#0000FF">
<tr>
<th width="25%">用户名</th>
<td width="75%"><input name="user" type="text" size="20">
</td>
</tr>
<tr>
<th>密码</th>
<td><input name="password" type="password" size="20" > </td>
</tr>
<tr>
<th> 附加码</th>
<td><input type="text" name="otherpwd" size="20" class="button1">
<%
dim temprnd '随机生成 附加码
Randomize
temprnd=cstr(Int((9999 - 1000 + 1) * Rnd() + 1000))
response.Write (temprnd)
%>
<input type="hidden" name="reotherpwd" value="<%=temprnd%>"> '直接将生成附加码发送至检验网页
</td>
</tr>
<tr>
<th>&nbsp;</th>
<td><input type="submit" name="Submit" value="提交">
   
<input type="submit" name="teset" value="重写"> </td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<h2 align="center">&nbsp;</h2>
</body>
</html>

2.3 检验网页-validate.asp

<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>登录网页</title>
</head>
<body>
<p> </p>
<p> </p>
<h2 align="center">
<%
Session.Timeout=10  '限制Session中保存登录成功信息的时间
dim db '以下连上数据库,建立一个Connection对象实例db
set db=Server.Createobject("ADODB.Connection")
db.Open "DBQ="&Server.Mappath("maindata.asa")&";DRIVER={Microsoft Access Driver (*.mdb)};"
dim strSql,rs,strSql1
strSql="Select * From user"
set rs=db.execute(strSql)
dim a1,a2,nam,psw,otherpwd,reotherpwd
a1=Request.Form("user")
a2=Request.Form("password")
otherpwd=Request.Form("otherpwd")
reotherpwd=Request.Form("reotherpwd")
do while not rs.Eof
nam=rs("user_name")
rem 利用decrypt函数还原用户密码
psw=decrypt(rs("password"))
if reotherpwd<>otherpwd then
Response.Write "附加码不对!<p>"
Response.Write "<a href='logon.asp' >重新登录</a>"
Response.End
end if
if a1=nam and a2=psw then
session("pass")="action1" '将登录成功的信息保存在session中,以便备查(如,default.asp中
response.Redirect("default.asp") '如果用户名、密码正确,登录成功,引导到另一张网页
Response.End
end if
rs.movenext
loop
Response.Write "用户信息不正确<p>"
Response.Write "<a href='logon.asp' >重新登录</a>"
%>
</h2>
</body>
</html>

2.4 注册网页-login.asp

  功能:用户注册界面,且将注册信息发送至,检验及添加至数据库的网页-insert.asp
  代码:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>注册</title>
</head>
<body>
<p> </p><p> </p>
<table width="600" border="0" align="center">
<tr>
<td><h2 align="center">用户注册</h2>
<form name="form1" method="post" action="insert.asp">
<table width="67%" border="1" align="center" cellspacing="0" bordercolor="#0000FF">
<tr>
<th width="25%">用户名</th>
<td width="75%"><input type="text" name="user">
** 必须填写</td>
</tr>
<tr>
<th>密码</th>
<td><input type="password" name="password" >
**</td>
</tr>
<tr>
<th width="25%">确认密码</th>
<td width="75%"><input type="password" name="password2">
**</td>
</tr>
<tr>
<th width="25%">性别</th>
<td width="75%"><input type="radio" name="sex" value="男">

<input type="radio" name="sex" value="女">
女</td>
</tr>
<tr>
<th width="25%">爱好</th>
<td width="75%"><input type="checkbox" name="love" value="音乐">
音乐
<input type="checkbox" name="love" value="计算机">
计算机</td>
</tr>
<tr>
<th width="25%">职业</th>
<td width="75%"><select name="career" size="1">
<option value="   " selected>请选择</option>
<option value="教育业">教育业</option>
<option value="金融业">金融业</option>
<option value="其他">其他</option>
</select></td>
</tr>
<tr>
<th width="25%">简介</th>
<td width="75%"><textarea name="introduction" cols="40" rows="2" wrasp="soft"></textarea></td>
</tr>
<tr>
<th>&nbsp;</th>
<td><input type="submit" name="Submit" value="提交">
<input type="submit" name="teset" value="重写"> </td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<h2 align="center">&nbsp;</h2>
</body>
</html>

2.5 检验注册信息,将合格信息添加数据库网页-insert.asp

  功能:检验注册信息是否合格。如,用户名是否已经注册过;密码、确认密码是否相同。
  代码:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file=security.asp-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>注册结果</title>
</head>
<body>
<p> </p>
<p> </p>
<h2 align="center">
<%
dim db '以下连上数据库,建立一个Connection对象实例db
set db=Server.Createobject("ADODB.Connection")
db.Open "DBQ="&Server.Mappath("maindata.asa")&";DRIVER={Microsoft Access Driver (*.mdb)};"
dim strSql,rs,strSql1
strSql="Select * From user"
set rs=db.execute(strSql)
dim a1,a2,b2,c2,a3,a4,a5,a6,i
a1=Request.Form("user")
a2=Request.Form("password")
b2=Request.Form("password2")
a3=Request.Form("sex")
a4=Request.Form("love")
a5=Request.Form("career")
a6=Request.Form("introduction")
'下面是检查用户名和密码是否填写完整
if trim(a1)="" or trim(a2)="" then
Response.Write "对不起,用户名、密码必须填写!<p>"
Response.Write "<a href='login.asp' >重填</a>"
Response.End
end if
'下面将注册用户名,逐条与数据库是记录的用户名对比。如果数据库中已经有了,提示用户不能再注册。
do while not rs.Eof
i=rs("user_name")
if trim(a1)=trim(i) then
Response.Write "用户名重名<p>"
Response.Write "<a href='login.asp' >重填</a>"
Response.End
end if
rs.movenext
loop
'下面是检查密码与确认密码是否相同
if a2 <> b2 then
Response.Write "密码确认不正确<p>"
Response.Write "<a href='login.asp' >重填</a>"
Response.End
end if
'下面是将注册信息添加到数据库中。利用encrypt()加密用户密码。
c2=encrypt(a2)
strsql="insert into user(user_name,password,sex,love,career,introduction,submit_date) Values('"& a1 & "', '"& a2 &"', '" & a3 &"','"& a4 & "','"& a5 & "','"& a6 & "', #" & now() & "#)"
db.Execute(strsql)
%>
</h2>
<h2 align="center" >
<%
Response.Write "注册成功!<p>请记牢用户名和密码!"
%>
</h2>
<p align="center" class="base-4" >&nbsp;</p>
<table width="121" height="35" border="0" align="center" cellpadding="0" cellspacing="0">
<tr align="middle">
<td><script language="JavaScript">
function shutwin(){
window.close();
return;}
</script> <a href="javascript:shutwin();">关闭本窗口</a></td>
</tr>
</table>
<p align="center" >&nbsp;</p>
<p>
</body>
</html>

2.6 加密、还原密码的Function过程网页-security.asp

<%
function decrypt(dcode)
dim texts
dim i
for i=1 to len(dcode)
texts=texts & chr(asc(mid(dcode,i,2))-i)
next
decrypt=texts
end function
function encrypt(ecode)
Dim texts
dim i
for i=1 to len(ecode)
texts=texts & chr(asc(mid(ecode,i,2))+i)
next
encrypt = texts
end function
%>

3 数据库文件

  Access数据库文件-maindata.asa
  提示:为了安全将扩展名mdb,改为asa。
  数据库中存放一张名为user的表。表的结构为包含8个字段:
  ID:自动编号,数值;
  user_name:文本(存放用户名);
  password:文本(存放密码);
  sex:文本(存放性别);
  love:文本(存放爱好);
  career:文本(存放职业);
  introduction:文本(存放兴趣);
  submit_date:日期/时间(存放注册时间,由程序自动获取服务器日期时间)。


主页                                  {其它

   

《花桥电脑书斋》-邱泳昱的个人主页
如需转载望能征求本人意见