<?php
// 开启 session
session_start();
// 如果已经登录,重定向到主页
if (isset($_SESSION['admin']) && $_SESSION['admin'] === true) {
header("Location: index.php");
exit();
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
include("conn.php");
// 获取表单数据
$name = $_POST['username'];
$password = $_POST['password'];
$mdpassword = md5($password); // 将密码进行 md5 加密
// 查询数据库
$db = new Database();
$result = $db->findone("SELECT * FROM admin WHERE name='$name'");
// 验证用户名和密码
if ($result) {
if ($result["password"] === $mdpassword) {
echo "登入成功";
$_SESSION['admin'] = true;
$_SESSION['username'] = $name;
// 根据 session 中的 repage 值决定重定向地址
if (isset($_SESSION['repage'])) {
header("Location: " . $_SESSION['repage']);
} else {
header("Location: index.php");
}
exit();
} else {
exit("密码错误");
}
} else {
exit("用户名错误");
}
}
?>
<html>
<head>
<title>欢迎登入</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h1>欢迎登入</h1>
<form action="login.php" method="post">
<div class="form-group">
<label for="username">用户名:</label>
<input type="text" class="form-control" id="username" name="username" placeholder="请输入账号" required>
</div>
<div class="form-group">
<label for="password">密码:</label>
<input type="password" class="form-control" id="password" name="password" placeholder="请输入密码" required>
</div>
<button type="submit" class="btn btn-default">登入</button>
<button type="button" class="btn btn-default" onclick="window.location.href='register.php'">注册</button>
</form>
</div>
</body>
</html>