嘿咻~要给自己起个萌萌哒新名字哦(〃'▽'〃
前端社区页面(index.php)
<?php // 假设已经连接数据库 // 数据库连接代码省略 ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>社区首页</title> </head> <body> <h1>社区首页</h1> <?php // 查询社区帖子并显示 $sql = "SELECT * FROM posts ORDER BY created_at DESC"; $result =$conn->query($sql); if ($result->num_rows > 0) { while($row =$result->fetch_assoc()) { echo "<div class='post'>"; echo "<h2>" . htmlspecialchars($row["title"]) . "</h2>"; echo "<p>" . nl2br(htmlspecialchars($row["content"])) . "</p>"; echo "<small>发表于: " . $row["created_at"] . "</small>"; echo "</div>"; } } else { echo "没有帖子"; } ?> </body> </html>
后台管理页面(admin.php)
<?php session_start(); // 检查用户是否已经登录 if (!isset($_SESSION['user_id'])) { header("Location: login.php"); // 如果未登录,重定向到登录页面 exit; } // 数据库连接代码省略 // 处理添加帖子的表单提交 if ($_SERVER["REQUEST_METHOD"] == "POST") { $title =$_POST['title']; $content =$_POST['content']; // 插入新帖子的SQL语句 $sql = "INSERT INTO posts (title, content, created_at) VALUES (?, ?, NOW())"; if ($stmt =$conn->prepare($sql)) { $stmt->bind_param("ss",$title, $content); $stmt->execute(); $stmt->close(); } } // 显示管理界面 ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>后台管理</title> </head> <body> <h1>后台管理</h1> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <label for="title">标题:</label> <input type="text" name="title" id="title" required> <label for="content">内容:</label> <textarea name="content" id="content" required></textarea> <input type="submit" value="发布帖子"> </form> </body> </html>
<?php session_start(); // 数据库连接代码省略 // 处理登录表单提交 if ($_SERVER["REQUEST_METHOD"] == "POST") { $username =$_POST['username']; $password =$_POST['password']; // 查询用户 $sql = "SELECT id FROM users WHERE username = ? AND password = ?"; if ($stmt =$conn->prepare($sql)) { $stmt->bind_param("ss",$username, $password); $stmt->execute(); $stmt->store_result(); if ($stmt->num_rows == 1) { $stmt->bind_result($user_id); $stmt->fetch(); $_SESSION['user_id'] =$user_id; header("Location: admin.php"); exit; } else { $error = "用户名或密码错误!"; } $stmt->close(); } } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <h1>登录</h1> <?php if (!empty($error)): ?> <p style="color: red;"><?php echo $error; ?></p> <?php endif; ?> <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <label for="username">用户名:</label> <input type="text" name="username" id="username" required> <label for="password">密码:</label> <input type="password" name="password" id="password" required> <input type="submit" value="登录"> </form> </body> </html>
暂无评论
请先登录后发表评论!
暂无评论