```html
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="Connections/conn2.asp" -->
<%
' *** Validate request to log in to this site.
MM_LoginAction = Request.ServerVariables("URL")
If Request.QueryString <> "" Then MM_LoginAction = MM_LoginAction + "?" + Server.HTMLEncode(Request.QueryString)
MM_valUsername = CStr(Request.Form("name"))
If MM_valUsername <> "" Then
Dim MM_loginSQL, MM_fldUserAuthorization
MM_loginSQL = "SELECT username, psw FROM login_user WHERE username = ? AND psw = ?"
MM_fldUserAuthorization = "" ' 如果需要获取用户权限字段,请设置为对应的字段名
Dim conn, MM_rsUser_cmd, MM_rsUser ',MM_conn2_STRING
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open MM_conn2_STRING ' 打开数据库连接字符串
Set MM_rsUser_cmd = Server.CreateObject("ADODB.Command")
MM_rsUser_cmd.ActiveConnection = conn
With MM_rsUser_cmd
.CommandType = 1 ' adCmdText,命令类型为文本
.CommandText = MM_loginSQL ' 设置SQL语句
.Prepared = true ' 预编译SQL语句
.Parameters.Append .CreateParameter("username", 200, 1, 50, MM_valUsername) ' 添加参数
.Parameters.Append .CreateParameter("password", 200, 1, 50, Request.Form("psw")) ' 添加参数
End With
Set MM_rsUser = MM_rsUser_cmd.Execute
If Not MM_rsUser.EOF Or Not MM_rsUser.BOF Then
' 进行验证码验证
Dim input_code
input_code = Request.Form("input_code")
If LCase(input_code) <> LCase(Session("CheckCode")) Then
Response.Write("<script>alert('验证码输入错误,请重新输入。')</script>")
Else
Session("MM_Username") = MM_valUsername
If (MM_fldUserAuthorization <> "") Then
Session("MM_UserAuthorization") = CStr(MM_rsUser.Fields.Item(MM_fldUserAuthorization).Value)
Else
Session("MM_UserAuthorization") = ""
End If
if CStr(Request.QueryString("accessdenied")) <> "" And True Then
MM_redirectLoginSuccess = Request.QueryString("accessdenied")
End If
MM_rsUser.Close
Response.Redirect(MM_redirectLoginSuccess)
End If
' 否则跳转到登录失败页面
Else
MM_rsUser.Close
Response.Redirect(MM_redirectLoginFailed)
end if
end if
%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<style type="text/css">
body {
background-image: url(%E5%9B%BE%E7%89%87/amelia-barklid-CUmCByAhXyQ-unsplash.jpg);
background-size: cover;
background-position: center;
}
.indexfont {
color: #FFF;
font-size: 24px;
text-align: center;
font-weight: bold;
}
.indextablefont {
color: #000;
font-weight: bold;
}
</style>
<script type="text/javascript">
function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}
window.onload = function() {
draw();
}
// 绘制验证码
function draw() {
var canvas = document.getElementById("canvas");
var context = canvas.getContext("2d");
context.fillStyle = "#fff";
context.fillRect(0, 0, 120, 40);
context.font = "bold 30px Arial";
var aCode = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"];
var len = aCode.length - 1;
var code = "";
for (var i = 0; i < 4; i++) {
var index = Math.round(Math.random() * len);
var c = aCode[index];
code += c;
context.fillStyle = "#" + (Math.random() * 0xffffff << 0).toString(16);
context.fillText(c, i * 25 + 20, 30);
}
//将验证码保存到隐藏的input标签和Session中
document.getElementById("code").value = code;
<% Session("CheckCode") = "'" & code & "'" %>
}
// 提交表单并验证验证码
function submitForm() {
var form = document.getElementById("user_login");
var code = document.getElementById("code").value;
var input_code= document.getElementById("input_code").value;
if (input_code.toLowerCase() === code.toLowerCase()) { // 判断用户输入的验证码是否正确
form.submit(); // 如果验证码正确,则提交表单
} else {
alert('验证码输入错误,请重新输入');
draw(); // 重新绘制验证码
document.getElementById("input_code").value = ""; // 清空用户输入的验证码
}
}
</script>
</head>
<body>
<table width="60%" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#0099FF" class="indexfont">::理工教学管理系统学生端登录::</td>
</tr>
<tr>
<td height="160" align="center">
<form ACTION="<%=MM_LoginAction%>" METHOD="POST" id="user_login" name="user_login">
<table width="60%" border="0" align="center">
<tr>
<td width="36%" class="indextablefont">Username:</td>
<td width="64%"><input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td class="indextablefont">Password:</td>
<td><input type="password" name="psw" id="psw" /></td>
</tr>
<tr>
<td class="indextablefont">验证码:</td>
<td>
<canvas id="canvas" width="120" height="40"></canvas>
<input type="hidden" name="code" id="code">
<input type="text" name="input_code" id="input_code">
<input type="button" value="看不清,换一张" onclick="draw()">
</td>
</tr>
<tr>
<tr>
<td> </td>
<td><input type="button" value="登录" onclick="submitForm()" />
<input name="button2" type="button" id="button2" onClick="MM_openBrWindow('index_student_zhuce.asp','','')" value="注册" /></td>
</tr>
</table>
<input type="checkbox" name="checkbox" id="checkbox" />
已阅读接受
</form>
</td>
</tr>
<tr>
<td bgcolor="#00CCFF" class="indexfont">::48号版权所有::</td>
</tr>
<tr>
<td bgcolor="#0099FF" class="indexfont"><%= Request.QueryString("err") %></td>
</tr>
</table>
</body>
</html>
验证码输正确也会弹窗说验证码错误,代码有什么错误bug?如何修改
既然弹窗提示验证码错误,就证明执行了“alert('验证码输入错误,请重新输入');”这句代码,
你可以在if (input_code.toLowerCase() === code.toLowerCase()) 判断之前,将input_code和code打印在控制台,
并且input_code.toLowerCase() 和code.toLowerCase()也打印出来,看看input_code.toLowerCase() === code.toLowerCase() 到底是否为true,
希望对你有帮助
发现跨数据库,连表查询,提示编码问题,两个库的数据表编码不一致,一个为utf8_general_ci,另一个是utf8mb4_unicode_ci,修改后重新同步解决
修改代码如下:
ALTER TABLE XX CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
针对这个问题,需要对验证码相关的代码进行深入分析,找到出错的原因,并进行修复。具体步骤如下:
需要注意的是,在修复bug时应先进行备份,并且要注意不要引入新的问题。同时,在修改完代码后需要进行全面的测试,以确保修复后的程序可以正常运行。