写了一个Ajax的验证用户注册的函数,
写了一个Ajax的验证用户注册的函数,不知道是不是正确的,请大家看一下.
服务器端代码
1function checkUser(){
2 var username;
3 var password;
4 username = from1.UserName.value;
5 password = from1.PassWord.value;
6
7 var xmlhttp;
8 xmlhttp = CreateXMLRequester();
9 xmlhttp.onreadystatechange=function(){
10 if(4==xmlhttp.readyState){
11 if(200==xmlhttp.status){
12 var state = xmlhttp.responseText;
13 switch(state){
14 case 0:
15 loginmsg.innerHTML = "您的用户名或密码不正确认,请重新输入!";
16 break;
17 case 1:
18 loginmsg.innerHTML = "登录信息正确!";//进行下一步的操作
19 break;
20 case 2:
21 loginmsg.innerHTML = "您的用户被管理员禁用,请与管理员联系!";//进行下一步的操作
22 break;
23 case 3:
24 loginmsg.innerHTML = "验证失败!";//进行下一步的操作
25 break;
26 }
27 }
28 else if(202==xmlhtttp.status){
29 loginmsg.innerHTML = "正在进行数据验证!";
30 }
31 else{
32 loginmsg.innerHTML = "验证失败!";
33 }
34 }
35 }
36 xmlhttp.open("post","bin/ajaxlogin.asp",true);
37 //当方法为post时需要如下设置http头
38 xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
39 //发送数据
40 xmlhttp.send("username="+escape(UserName)+"&password="+escape(PassWord));
41}
2 var username;
3 var password;
4 username = from1.UserName.value;
5 password = from1.PassWord.value;
6
7 var xmlhttp;
8 xmlhttp = CreateXMLRequester();
9 xmlhttp.onreadystatechange=function(){
10 if(4==xmlhttp.readyState){
11 if(200==xmlhttp.status){
12 var state = xmlhttp.responseText;
13 switch(state){
14 case 0:
15 loginmsg.innerHTML = "您的用户名或密码不正确认,请重新输入!";
16 break;
17 case 1:
18 loginmsg.innerHTML = "登录信息正确!";//进行下一步的操作
19 break;
20 case 2:
21 loginmsg.innerHTML = "您的用户被管理员禁用,请与管理员联系!";//进行下一步的操作
22 break;
23 case 3:
24 loginmsg.innerHTML = "验证失败!";//进行下一步的操作
25 break;
26 }
27 }
28 else if(202==xmlhtttp.status){
29 loginmsg.innerHTML = "正在进行数据验证!";
30 }
31 else{
32 loginmsg.innerHTML = "验证失败!";
33 }
34 }
35 }
36 xmlhttp.open("post","bin/ajaxlogin.asp",true);
37 //当方法为post时需要如下设置http头
38 xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
39 //发送数据
40 xmlhttp.send("username="+escape(UserName)+"&password="+escape(PassWord));
41}
服务器端代码
1<!--#include file=../public/conn.asp --><!--#include file=../public/md5.asp --><!--#include file=../public/CheckStr.asp -->
2<% dim sUserName,sPwd,sql,Comm,rs
3 sUserName=request.Form("username")
4 sPwd=md5(trim(request.Form("Password")))
5 '调用注册验证存储过程
6 Set Comm = Server.CreateObject("ADODB.Command")
7 with Comm
8 .ActiveConnection = Conn 'MyConStr是数据库连接字串
9 .CommandText = "PCheckLogin" '指定存储过程名
10 .CommandType = 4 '表明这是一个存储过程
11 .Prepared = true '要求将SQL命令先行编译
12 '参数说明:参数名、参数数据类型、参数类型、数据长度、参数值
13 .Parameters.Append .CreateParameter("RETURN",2,4)
14 .Parameters.Append .CreateParameter("@UserName",200,1,30,sUserName)
15 .Parameters.append .CreateParameter("@Password",200,1,30,sPwd)
16 Set rs = .Execute
17 end with
18 rs.close '想使用comm(0)的值必须先关闭记录集,用后打开,否则为0
19 if comm(0) = 0 then
20 response.write "0" '验证失败
21 else
22 rs.open
23 iUserId=rs("UserId")
24 IF rs("Checked")=2 THEN
25 response.write "2" '用户被禁止
26 END IF
27
28 '将最后登录时间修改为当前时间
29 conn.execute("update useradmin set LastLogin ='"&Date()&"' where userid="&iUserId)
30 response.Cookies("UserId")=iUserId
31 response.Cookies("UserLevel")=rs("UserLevelId")
32 response.Cookies("UserName")=rs("UserName")
33 response.Cookies("NickName")=rs("NickName")
34 response.Cookies("FullName")=rs("FullName")
35 response.Cookies("UnitId")=rs("UnitId")
36 response.Cookies("DepId")=rs("DepId")
37 response.Cookies("LastLogin")=rs("LastLogin")
38 response.Cookies("email")=rs("Email")
39 '存储过程:EXEC PManagePoint 登录次数,Userid
40 sql="EXEC PManagePoint 登录次数,'"&iUserId&"'"
41 conn.execute(sql)
42 response.write "1" '通过验证
43 end if
44 rs.close
45 set Comm = nothing
46 set rs=nothing
47
48 if err then
49 response.write "3" '验证过程发生错误
50 end if
51%>
2<% dim sUserName,sPwd,sql,Comm,rs
3 sUserName=request.Form("username")
4 sPwd=md5(trim(request.Form("Password")))
5 '调用注册验证存储过程
6 Set Comm = Server.CreateObject("ADODB.Command")
7 with Comm
8 .ActiveConnection = Conn 'MyConStr是数据库连接字串
9 .CommandText = "PCheckLogin" '指定存储过程名
10 .CommandType = 4 '表明这是一个存储过程
11 .Prepared = true '要求将SQL命令先行编译
12 '参数说明:参数名、参数数据类型、参数类型、数据长度、参数值
13 .Parameters.Append .CreateParameter("RETURN",2,4)
14 .Parameters.Append .CreateParameter("@UserName",200,1,30,sUserName)
15 .Parameters.append .CreateParameter("@Password",200,1,30,sPwd)
16 Set rs = .Execute
17 end with
18 rs.close '想使用comm(0)的值必须先关闭记录集,用后打开,否则为0
19 if comm(0) = 0 then
20 response.write "0" '验证失败
21 else
22 rs.open
23 iUserId=rs("UserId")
24 IF rs("Checked")=2 THEN
25 response.write "2" '用户被禁止
26 END IF
27
28 '将最后登录时间修改为当前时间
29 conn.execute("update useradmin set LastLogin ='"&Date()&"' where userid="&iUserId)
30 response.Cookies("UserId")=iUserId
31 response.Cookies("UserLevel")=rs("UserLevelId")
32 response.Cookies("UserName")=rs("UserName")
33 response.Cookies("NickName")=rs("NickName")
34 response.Cookies("FullName")=rs("FullName")
35 response.Cookies("UnitId")=rs("UnitId")
36 response.Cookies("DepId")=rs("DepId")
37 response.Cookies("LastLogin")=rs("LastLogin")
38 response.Cookies("email")=rs("Email")
39 '存储过程:EXEC PManagePoint 登录次数,Userid
40 sql="EXEC PManagePoint 登录次数,'"&iUserId&"'"
41 conn.execute(sql)
42 response.write "1" '通过验证
43 end if
44 rs.close
45 set Comm = nothing
46 set rs=nothing
47
48 if err then
49 response.write "3" '验证过程发生错误
50 end if
51%>