基于B/S的双学位招生管理系统的登录功能
时间: 2023-11-19 13:05:25 浏览: 30
实现一个基于B/S的双学位招生管理系统的登录功能,需要涉及以下几个方面的内容:
1. 前端页面设计:设计登录页面的UI界面,包括输入框、按钮、验证码等元素。
2. 前端交互实现:将用户输入的账号和密码通过AJAX请求发送到后端进行验证,并根据后端返回的结果进行相应的提示。
3. 后端验证逻辑:接收前端发送的账号和密码,从数据库中查询是否存在该用户,并验证密码是否正确。
4. 登录状态管理:将用户登录状态保存到Session中,以便在用户浏览其他页面时进行判断,同时也需要提供注销登录功能。
下面是一个简单的示例代码,仅供参考:
前端页面设计:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>双学位招生管理系统 - 登录</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<form>
<div>
<label for="username">用户名:</label>
<input type="text" id="username" name="username">
</div>
<div>
<label for="password">密码:</label>
<input type="password" id="password" name="password">
</div>
<div>
<label for="captcha">验证码:</label>
<input type="text" id="captcha" name="captcha">
<img src="captcha.php" id="captchaImg">
<a href="javascript:void(0);" onclick="document.getElementById('captchaImg').src='captcha.php?r='+Math.random();">看不清,换一张</a>
</div>
<div>
<input type="button" value="登录" onclick="login();">
</div>
</form>
</body>
<script>
// 登录函数
function login() {
var username = $("#username").val();
var password = $("#password").val();
var captcha = $("#captcha").val();
$.ajax({
url: "login.php",
type: "POST",
data: {
"username": username,
"password": password,
"captcha": captcha
},
success: function(data) {
if (data == "success") {
window.location.href = "index.php";
} else {
alert(data);
// 刷新验证码
document.getElementById("captchaImg").src = "captcha.php?r=" + Math.random();
}
}
});
}
</script>
</html>
```
前端交互实现:
```javascript
// 登录函数
function login() {
var username = $("#username").val();
var password = $("#password").val();
var captcha = $("#captcha").val();
$.ajax({
url: "login.php",
type: "POST",
data: {
"username": username,
"password": password,
"captcha": captcha
},
success: function(data) {
if (data == "success") {
window.location.href = "index.php";
} else {
alert(data);
// 刷新验证码
document.getElementById("captchaImg").src = "captcha.php?r=" + Math.random();
}
}
});
}
```
后端验证逻辑:
```php
<?php
// 连接数据库
$host = "localhost";
$dbname = "db_name";
$username = "username";
$password = "password";
$dsn = "mysql:host=$host;dbname=$dbname";
$db = new PDO($dsn, $username, $password);
// 获取用户输入的账号、密码和验证码
$username = $_POST["username"];
$password = $_POST["password"];
$captcha = $_POST["captcha"];
// 验证验证码
session_start();
if (strtolower($captcha) != strtolower($_SESSION["captcha"])) {
echo "验证码错误";
exit;
}
// 查询用户信息
$stmt = $db->prepare("SELECT * FROM user WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
// 验证密码
if ($user && password_verify($password, $user["password"])) {
// 登录成功,保存用户信息到Session
session_regenerate_id();
$_SESSION["user_id"] = $user["id"];
$_SESSION["username"] = $user["username"];
echo "success";
} else {
// 登录失败
echo "用户名或密码错误";
}
```
登录状态管理:
```php
<?php
// 验证用户是否已登录
session_start();
if (!isset($_SESSION["user_id"])) {
header("Location: login.php");
exit;
}
// 注销登录
if (isset($_GET["action"]) && $_GET["action"] == "logout") {
session_destroy();
header("Location: login.php");
exit;
}
```
以上代码仅供参考,在实际项目中还需要根据具体需求进行调整。同时,为了保证安全性,建议对密码进行加密存储,并且在登录时使用HTTPS协议进行传输。