用mysql.connector和html写一个登录页面,并且可以验证账号密码正误,错误时提示密码错误,正确则进入新的页面

时间: 2024-02-05 11:11:37 浏览: 21
以下是一个简单的实现: 1. 创建一个 MySQL 数据库,包含一个名为 `users` 的表,表中有两个字段:`username` 和 `password`。在表中添加一些用户账号和密码。 2. 创建一个 HTML 登录页面,包含两个输入框和一个登录按钮。 3. 使用 MySQL Connector Python 模块连接到数据库,并且定义一个函数 `login(username, password)`,用于验证用户输入的账号密码是否正确。 4. 在登录按钮点击事件中调用 `login()` 函数,如果验证成功则跳转到另一个 HTML 页面,否则在登录页面显示错误提示信息。 下面是代码示例: HTML 文件 `login.html`: ``` <!DOCTYPE html> <html> <head> <title>Login</title> </head> <body> <h1>Login</h1> <form> <label>Username:</label> <input type="text" id="username"><br> <label>Password:</label> <input type="password" id="password"><br> <button type="button" onclick="login()">Login</button> </form> <div id="message"></div> <script> function login() { var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState === 4) { if (xhr.status === 200) { if (xhr.responseText === 'success') { window.location.href = 'welcome.html'; } else { document.getElementById('message').innerHTML = 'Invalid username or password.'; } } else { document.getElementById('message').innerHTML = 'Error: ' + xhr.statusText; } } }; var username = document.getElementById('username').value; var password = document.getElementById('password').value; xhr.open('POST', 'login.py'); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.send('username=' + encodeURIComponent(username) + '&password=' + encodeURIComponent(password)); } </script> </body> </html> ``` Python 文件 `login.py`: ```python import mysql.connector import cgi # 连接到 MySQL 数据库 cnx = mysql.connector.connect(user='username', password='password', database='mydatabase') def login(username, password): cursor = cnx.cursor() query = ("SELECT COUNT(*) FROM users WHERE username=%s AND password=%s") cursor.execute(query, (username, password)) result = cursor.fetchone()[0] cursor.close() return result == 1 # 处理 POST 请求 form = cgi.FieldStorage() username = form.getvalue('username') password = form.getvalue('password') if login(username, password): print('Content-Type: text/plain') print('Status: 200 OK') print() print('success') else: print('Content-Type: text/plain') print('Status: 401 Unauthorized') print() print('invalid username or password') ``` HTML 文件 `welcome.html`: ``` <!DOCTYPE html> <html> <head> <title>Welcome</title> </head> <body> <h1>Welcome</h1> <p>You have successfully logged in.</p> </body> </html> ``` 注意事项: - 代码中的数据库连接信息需要根据实际情况进行修改。 - 为了简单起见,代码中没有对输入的用户名和密码进行安全性检查,实际开发中需要注意防止 SQL 注入等安全问题。

相关推荐

最新推荐

recommend-type

MySQL密码正确却无法本地登录-1045

主要介绍了MySQL密码正确却无法本地登录-1045 Access denied for user 'root'@'localhost' (using password:YES,需要的朋友可以参考下
recommend-type

详解MySQL安装及MySQL8.0新密码认证方式

主要介绍了详解MySQL安装及MySQL8.0新密码认证方式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
recommend-type

mysql8.0.19忘记密码的完美解决方法

主要介绍了mysql8.0.19忘记密码的完美解决方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
recommend-type

mysql第一次安装成功后初始化密码操作步骤

在本篇文章里小编给大家整理了关于mysql第一次安装成功后初始化密码操作步骤以及相关知识点,有兴趣的朋友们可以学习下。
recommend-type

MySQL 如何修改root用户的密码

主要介绍了MySQL 如何修改root用户的密码,帮助大家更好的使用MySQL,不用担心忘记密码,感兴趣的朋友可以了解下
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用 Python 画一个可以动的爱心

好的,我可以帮助您使用Python来绘制一个动态的爱心。您可以使用turtle库来实现。以下是实现代码: ```python import turtle import math # 设置画布和画笔 canvas = turtle.Screen() canvas.bgcolor("black") pencil = turtle.Turtle() pencil.speed(0) pencil.color("red", "pink") pencil.pensize(3) # 定义爱心函数 def draw_love(heart_size, x_offset=0, y_offset=0):
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。