【毕业设计】基于 PHP 开发的社区交流系统

基于 PHP 开发的社区交流系统可以是一个论坛、博客平台或是问答网站等形式的在线平台,用于用户之间的互动交流。以下是一个简单的 PHP 社区交流系统的示例,包括用户注册、登录、发布帖子、回复帖子等功能。

技术栈

  • 前端:HTML, CSS, JavaScript
  • 后端:PHP
  • 数据库:MySQL

环境准备

  1. 安装 PHP 和 MySQL 服务。
  2. 安装 Web 服务器(如 Apache 或 Nginx)。
  3. 创建 MySQL 数据库。

数据库设计

创建一个名为 community_db 的数据库,并创建如下表结构:

  1. users 表:存储用户信息。
  2. posts 表:存储帖子信息。
  3. comments 表:存储评论信息。
CREATE DATABASE community_db;

USE community_db;

CREATE TABLE users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    username VARCHAR(255) NOT NULL UNIQUE,
    password VARCHAR(255) NOT NULL,
    email VARCHAR(255) NOT NULL UNIQUE
);

CREATE TABLE posts (
    id INT AUTO_INCREMENT PRIMARY KEY,
    user_id INT NOT NULL,
    title VARCHAR(255) NOT NULL,
    content TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (user_id) REFERENCES users(id)
);

CREATE TABLE comments (
    id INT AUTO_INCREMENT PRIMARY KEY,
    post_id INT NOT NULL,
    user_id INT NOT NULL,
    content TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (post_id) REFERENCES posts(id),
    FOREIGN KEY (user_id) REFERENCES users(id)
);

PHP 脚本

1. 用户注册 (register.php)
<?php
session_start();
require_once 'db.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $_POST['username'];
    $email = $_POST['email'];
    $password = password_hash($_POST['password'], PASSWORD_DEFAULT);

    $stmt = $db->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)");
    $stmt->bind_param("sss", $username, $email, $password);
    
    if ($stmt->execute()) {
        header("Location: login.php");
    } else {
        echo "Error: " . $stmt->error;
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Register</title>
</head>
<body>
    <h1>Register</h1>
    <form action="register.php" method="post">
        Username: <input type="text" name="username" required><br>
        Email: <input type="email" name="email" required><br>
        Password: <input type="password" name="password" required><br>
        <input type="submit" value="Register">
    </form>
</body>
</html>
2. 用户登录 (login.php)
<?php
session_start();
require_once 'db.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $username = $_POST['username'];
    $password = $_POST['password'];

    $stmt = $db->prepare("SELECT * FROM users WHERE username = ?");
    $stmt->bind_param("s", $username);
    $stmt->execute();
    $result = $stmt->get_result();
    if ($row = $result->fetch_assoc()) {
        if (password_verify($password, $row['password'])) {
            $_SESSION['user_id'] = $row['id'];
            header("Location: index.php");
        } else {
            echo "Invalid username or password.";
        }
    } else {
        echo "User not found.";
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login</title>
</head>
<body>
    <h1>Login</h1>
    <form action="login.php" method="post">
        Username: <input type="text" name="username" required><br>
        Password: <input type="password" name="password" required><br>
        <input type="submit" value="Login">
    </form>
</body>
</html>
3. 主页 (index.php)
<?php
session_start();
require_once 'db.php';

if (!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}

$user_id = $_SESSION['user_id'];

$stmt = $db->query("SELECT * FROM posts ORDER BY created_at DESC");
$posts = $stmt->fetch_all(MYSQLI_ASSOC);
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Community</title>
</head>
<body>
    <h1>Welcome to the Community!</h1>
    <a href="logout.php">Logout</a>
    <h2>Create Post</h2>
    <form action="create_post.php" method="post">
        Title: <input type="text" name="title" required><br>
        Content: <textarea name="content" required></textarea><br>
        <input type="submit" value="Create Post">
    </form>
    <hr>
    <?php foreach ($posts as $post): ?>
        <h2><?php echo htmlspecialchars($post['title']); ?></h2>
        <p><?php echo htmlspecialchars($post['content']); ?></p>
        <a href="comment.php?id=<?php echo $post['id']; ?>">Comment</a>
        <hr>
    <?php endforeach; ?>
</body>
</html>
4. 创建帖子 (create_post.php)
<?php
session_start();
require_once 'db.php';

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $title = $_POST['title'];
    $content = $_POST['content'];
    $user_id = $_SESSION['user_id'];

    $stmt = $db->prepare("INSERT INTO posts (user_id, title, content) VALUES (?, ?, ?)");
    $stmt->bind_param("iss", $user_id, $title, $content);

    if ($stmt->execute()) {
        header("Location: index.php");
    } else {
        echo "Error: " . $stmt->error;
    }
}
5. 发表评论 (comment.php)
<?php
session_start();
require_once 'db.php';

$post_id = intval($_GET['id']);

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $content = $_POST['content'];
    $user_id = $_SESSION['user_id'];

    $stmt = $db->prepare("INSERT INTO comments (post_id, user_id, content) VALUES (?, ?, ?)");
    $stmt->bind_param("iii", $post_id, $user_id, $content);

    if ($stmt->execute()) {
        header("Location: comment.php?id=$post_id");
    } else {
        echo "Error: " . $stmt->error;
    }
}

$stmt = $db->query("SELECT * FROM posts WHERE id = $post_id");
$post = $stmt->fetch_assoc();

$stmt = $db->query("SELECT * FROM comments WHERE post_id = $post_id");
$comments = $stmt->fetch_all(MYSQLI_ASSOC);

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Comment on Post</title>
</head>
<body>
    <h1><?php echo htmlspecialchars($post['title']); ?></h1>
    <p><?php echo htmlspecialchars($post['content']); ?></p>
    <h2>Add Comment</h2>
    <form action="comment.php?id=<?php echo $post_id; ?>" method="post">
        Content: <textarea name="content" required></textarea><br>
        <input type="submit" value="Add Comment">
    </form>
    <hr>
    <h2>Comments</h2>
    <?php foreach ($comments as $comment): ?>
        <p><?php echo htmlspecialchars($comment['content']); ?></p>
    <?php endforeach; ?>
</body>
</html>

其他脚本

6. 注销 (logout.php)
<?php
session_start();
session_destroy();
header("Location: login.php");
exit;

数据库连接 (db.php)

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "community_db";

$db = new mysqli($servername, $username, $password, $dbname);

if ($db->connect_error) {
    die("Connection failed: " . $db->connect_error);
}
?>

总结

以上代码展示了如何创建一个简单的基于 PHP 的社区交流系统,包括用户注册、登录、创建帖子、发表评论等功能。实际应用中还需要考虑安全性问题,如输入验证、SQL 注入防护等,并且可以增加更多功能,如搜索、分类、用户权限管理等。