继萌ICP备案之后,一直想搞一个自己的备案系统。今天,终于完成了!
警告:以下代码经晚夜的个人博客和本人查证,存在一处严重漏洞,不建议继续使用。转到 https://blog.mcenahle.com/2024/07/19/85.html 以下载修复版。
搭建方法:
一、数据库和表
CREATE DATABASE record_system;
USE record_system;
CREATE TABLE records (
id INT AUTO_INCREMENT PRIMARY KEY,
site_name VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
url VARCHAR(255) NOT NULL,
site_description TEXT,
status VARCHAR(50) DEFAULT 'pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE admin_users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL
);
INSERT INTO admin_users (username, password) VALUES ('admin', 'admin123');
注意,上面的“admin”和“admin123”是后期登录管理系统的用户名和密码。
二、申请备案页面(record_form.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>申请备案</title>
</head>
<body>
<h1>申请备案</h1>
<form action="submit_record.php" method="post">
<label for="site_name">网站名:</label>
<input type="text" id="site_name" name="site_name" required><br><br>
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" required><br><br>
<label for="url">网址:</label>
<input type="url" id="url" name="url" required><br><br>
<label for="site_description">网站描述:</label>
<textarea id="site_description" name="site_description" required></textarea><br><br>
<input type="submit" value="提交">
</form>
</body>
</html>
三、处理表单提交(submit_record.php)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "record_system";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
$site_name = $_POST['site_name'];
$email = $_POST['email'];
$url = $_POST['url'];
$site_description = $_POST['site_description'];
$sql = "INSERT INTO records (site_name, email, url, site_description)
VALUES ('$site_name', '$email', '$url', '$site_description')";
if ($conn->query($sql) === TRUE) {
echo "备案申请提交成功!";
} else {
echo "提交失败: " . $conn->error;
}
$conn->close();
?>
四、管理员登录页面(admin_login.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>管理员登录</title>
</head>
<body>
<h1>管理员登录</h1>
<form action="admin_login.php" method="post">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required><br><br>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required><br><br>
<input type="submit" value="登录">
</form>
</body>
</html>
五、处理管理员登录请求(admin_login.php)
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "record_system";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM admin_users WHERE username='$username' AND password='$password'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$_SESSION['loggedin'] = true;
header("Location: admin_dashboard.php");
} else {
echo "用户名或密码错误";
}
$conn->close();
?>
六、管理后台页面(admin_dashboard.php)
<?php
session_start();
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
header("Location: admin_login.html");
exit;
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "record_system";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
$sql = "SELECT * FROM records";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>管理后台</title>
</head>
<body>
<h1>管理后台</h1>
<table border="1">
<tr>
<th>ID</th>
<th>网站名</th>
<th>邮箱</th>
<th>网址</th>
<th>网站描述</th>
<th>状态</th>
<th>创建时间</th>
<th>操作</th>
</tr>
<?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['site_name'] . "</td>";
echo "<td>" . $row['email'] . "</td>";
echo "<td>" . $row['url'] . "</td>";
echo "<td>" . $row['site_description'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['created_at'] . "</td>";
echo "<td><a href='update_status.php?id=" . $row['id'] . "'>更新状态</a></td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='8'>暂无数据</td></tr>";
}
?>
</table>
</body>
</html>
<?php
$conn->close();
?>
七、更新备案状态(update_status.php)
<?php
session_start();
if (!isset($_SESSION['loggedin']) || $_SESSION['loggedin'] !== true) {
header("Location: admin_login.html");
exit;
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "record_system";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
$id = $_GET['id'];
$sql = "UPDATE records SET status='approved' WHERE id='$id'";
if ($conn->query($sql) === TRUE) {
echo "备案状态更新成功!";
} else {
echo "更新失败: " . $conn->error;
}
$conn->close();
?>
八、查询备案页面(query_record.html)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>查询备案</title>
</head>
<body>
<h1>查询备案</h1>
<form action="query_record.php" method="get">
<label for="email">邮箱:</label>
<input type="email" id="email" name="email" required><br><br>
<input type="submit" value="查询">
</form>
</body>
</html>
九、处理查询请求(query_record.php)
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "record_system";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
$email = $_GET['email'];
$sql = "SELECT * FROM records WHERE email='$email'";
$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>查询结果</title>
</head>
<body>
<h1>查询结果</h1>
<table border="1">
<tr>
<th>ID</th>
<th>网站名</th>
<th>网址</th>
<th>网站描述</th>
<th>状态</th>
<th>创建时间</th>
</tr>
<?php
if ($result->num_rows > 0) {
while ($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['site_name'] . "</td>";
echo "<td>" . $row['url'] . "</td>";
echo "<td>" . $row['site_description'] . "</td>";
echo "<td>" . $row['status'] . "</td>";
echo "<td>" . $row['created_at'] . "</td>";
echo "</tr>";
}
} else {
echo "<tr><td colspan='6'>暂无数据</td></tr>";
}
?>
</table>
</body>
</html>
<?php
$conn->close();
?>
GoodBoyboy
给博主提个小建议,代码块的英文字体建议改一下,看代码真的有点难受(个人建议)
Mcenahle
@GoodBoyboy : 现在好多了,用的是 CascadiaMono 字体,这是编程最常见的字体了
GoodBoyboy
@Mcenahle : :[ablobcatheart]
Mcenahle
@GoodBoyboy : 行,我改一下
晚夜
嘿博主~这个创意好棒哇
我也想加入这个项目ヾ(≧∇≦*)ゝ
可以加个企鹅交谈一下嘛~
Mcenahle
@晚夜 : 2867155965