php论坛代码

php
<?php session_start(); // 数据库连接 $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "forum"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 用户注册 if(isset($_POST['register'])) { $username = $_POST['username']; $password = $_POST['password']; $sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')"; $conn->query($sql); echo "注册成功!"; } // 用户登录 if(isset($_POST['login'])) { $username = $_POST['username']; $password = $_POST['password']; $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'"; $result = $conn->query($sql); if($result->num_rows > 0) { $_SESSION['username'] = $username; echo "登录成功!"; } else { echo "用户名或密码错误!"; } } // 发帖 if(isset($_POST['create_post'])) { $title = $_POST['title']; $content = $_POST['content']; $username = $_SESSION['username']; $sql = "INSERT INTO posts (title, content, author) VALUES ('$title', '$content', '$username')"; $conn->query($sql); echo "帖子发布成功!"; } // 回帖 if(isset($_POST['reply'])) { $post_id = $_POST['post_id']; $content = $_POST['content']; $username = $_SESSION['username']; $sql = "INSERT INTO replies (post_id, content, author) VALUES ('$post_id', '$content', '$username')"; $conn->query($sql); echo "回帖成功!"; } // 展示帖子及回帖 $sql = "SELECT * FROM posts"; $result = $conn->query($sql); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { echo "标题: " . $row["title"]. " - 作者: " . $row["author"]. " - 内容: " . $row["content"]. "<br>"; // 查询回帖 $post_id = $row['id']; $reply_sql = "SELECT * FROM replies WHERE post_id='$post_id'"; $reply_result = $conn->query($reply_sql); if ($reply_result->num_rows > 0) { while($reply_row = $reply_result->fetch_assoc()) { echo "&nbsp;&nbsp;&nbsp;回帖: " . $reply_row["content"]. " - 作者: " . $reply_row["author"]. "<br>"; } } } } else { echo "暂无帖子!"; } $conn->close(); ?>

此代码仅作为示例,没有包含任何安全性措施,在实际应用中,务必添加适当的安全性检查和防护措施。

php
<!DOCTYPE html> <html> <head> <title>简易论坛</title> </head> <body> <h2>用户注册</h2> <form method="post" action=""> 用户名: <input type="text" name="username"><br> 密码: <input type="password" name="password"><br> <input type="submit" name="register" value="注册"> </form> <h2>用户登录</h2> <form method="post" action=""> 用户名: <input type="text" name="username"><br> 密码: <input type="password" name="password"><br> <input type="submit" name="login" value="登录"> </form> <?php if(isset($_SESSION['username'])): ?> <h2>发帖</h2> <form method="post" action=""> 标题: <input type="text" name="title"><br> 内容: <textarea name="content"></textarea><br> <input type="submit" name="create_post" value="发布帖子"> </form> <h2>回帖</h2> <form method="post" action=""> 帖子ID: <input type="text" name="post_id"><br> 内容: <textarea name="content"></textarea><br> <input type="submit" name="reply" value="回帖"> </form> <?php endif; ?> </body> </html>

此 HTML 代码包含了注册、登录、发帖和回帖的表单。根据用户是否登录,展示相应的表单。用户成功登录后,会话变量 $_SESSION['username'] 会被设置为当前用户名,以便在后续的操作中识别用户身份。

此代码仍然非常简单,并且缺少许多功能和安全性检查。在实际使用中,需要进一步完善代码,包括但不限于数据验证、用户权限管理、数据过滤和适当的错误处理。