Files
luowei/login.php
2025-01-06 19:52:30 +08:00

53 lines
1.5 KiB
PHP

<?php
session_start(); // 启用 Session
require_once "db_config.php"; // 引入数据库配置
$conn = $link;
// 检查是否提交表单
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$name = isset($_POST['name']) ? trim($_POST['name']) : '';
$password = isset($_POST['password']) ? trim($_POST['password']) : '';
if (empty($name) || empty($password)) {
echo "姓名和密码不能为空!";
} else {
// 将密码转换为 MD5 格式
$hashedPassword = md5($password);
// 查询用户信息
$sql = "SELECT * FROM user_70 WHERE name = ? AND password = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss", $name, $hashedPassword);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
// 登录成功,设置 Session
$_SESSION['s_name'] = $name;
header("Location: user_info.php"); // 跳转到 user_info.php
exit();
} else {
// 登录失败
echo "用户名或密码错误!";
}
}
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title>用户登录</title>
</head>
<body>
<form method="POST" action="login.php">
<label for="name">姓名:</label>
<input type="text" id="name" name="name"><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password"><br>
<button type="submit" id="ok" name="ok">提交</button>
</form>
</body>
</html>