【毕业设计】基于 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 表:存储评论信息。
sql 复制代码
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 复制代码
<?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 复制代码
<?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 复制代码
<?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 复制代码
<?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 复制代码
<?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 复制代码
<?php
session_start();
session_destroy();
header("Location: login.php");
exit;

数据库连接 (db.php)

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 注入防护等,并且可以增加更多功能,如搜索、分类、用户权限管理等。

相关推荐
龙哥·三年风水6 小时前
群控系统服务端开发模式-应用开发-个人资料
分布式·php·群控系统
Dingww101110 小时前
梧桐数据库中的网络地址类型使用介绍分享
数据库·oracle·php
Genius Kim14 小时前
SpringCloud Sentinel 服务治理详解
spring cloud·sentinel·php
原机小子18 小时前
城镇保障性住房管理:SpringBoot系统解决方案
数据库·spring boot·php
kali-Myon18 小时前
NewStarCTF2024-Week5-Web&Misc-WP
前端·python·学习·mysql·web安全·php·web
DK七七19 小时前
当今陪玩系统小程序趋势,陪玩系统源码搭建后的适用于哪些平台
小程序·php·uniapp
tekin1 天前
vscode php Launch built-in server and debug, PHP内置服务xdebug调试,自定义启动参数配置使用示例
ide·vscode·php·launch.json·runtimeargs·php内置服务自定义参数
The_Ticker1 天前
PHP查询实时股票行情
开发语言·php·学习方法
y0ungsheep1 天前
CTF中的phar反序列化 [SWPU 2018]SimplePHP
运维·web安全·网络安全·php·代码规范
前端白袍2 天前
计算机网络:简述LAN口模式下NAT和代理的区别
网络·计算机网络·php