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

server/2024/9/22 7:01:59/

基于 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 脚本

php_57">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>
php_98">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>
php_143">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>
php_187">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;}
}
php_210">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>

其他脚本

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

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


http://www.ppmy.cn/server/120164.html

相关文章

思通数科开源产品:免费的AI视频监控卫士安装指南

准备运行环境&#xff1a; 确保您的服务器或计算机安装了Ubuntu 18.04 LTS操作系统。 按照产品要求&#xff0c;安装以下软件&#xff1a; - Python 3.9 - Java JDK 1.8 - MySQL 5.5 - Redis 2.7 - Elasticsearch 8.14 - FFmpeg 4.1.1 - RabbitMQ 3.13.2 - Minio &#xff08;…

es的封装

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、类和接口介绍0.封装思想1.es的操作分类 二、创建索引1.成员变量2.构造函数2.添加字段3.发送请求4.创建索引总体代码 三.插入数据四.删除数据五.查询数据 前…

source ~/.bash_profile有什么用

source ~/.bash_profile 是在 Unix/Linux 系统上用来重新加载用户的 Bash 配置文件 ~/.bash_profile 的命令。这条命令的作用是使得当前的 Bash 环境重新读取并应用 ~/.bash_profile 中的设置和变量定义。 作用&#xff1a; 1. 更新环境变量&#xff1a; ~/.bash_profile 是用户…

C# 禁止程序重复启动

修改&#xff1a;Program.cs [STAThread] static void Main() {Mutex mutex new Mutex(true, "NewGuid123456", out bool isCreatedNew);if (!isCreatedNew){MessageBox.Show(Application.ProductName "is running...");return;}Application.EnableVisu…

Linux基础开发环境(git的使用)

1.账号注册 git 只是一个工具&#xff0c;要想实现便捷的代码管理&#xff0c;就需要借助第三方平台进行操作&#xff0c;当然第三平台也是基于git 开发的 github 与 gitee 代码托管平台有很多&#xff0c;这里我们首选 Github &#xff0c;理由很简单&#xff0c;全球开发者…

前端-HTML学习

HTML页面标签设计 <div style"width: 50%;height: 100%;border-radius:15px;border: black thin solid;padding-top: 5px;">盒子标签</div><!--div盒子标签属性width&#xff1a;宽height&#xff1a;高background-color&#xff1a;背景色border-ra…

工业一体机在汽车零部件工厂ESOP系统中的关键作用

在当今竞争激烈的汽车市场中&#xff0c;汽车零部件工厂的高效生产和严格质量控制至关重要。而工业一体机在汽车零部件工厂的 ESOP&#xff08;电子标准化作业程序&#xff09;系统中发挥着关键作用。 一、汽车零部件工厂面临的挑战 汽车零部件的生产过程复杂且要求严格&#…

springboot每次都需要重设密码?明明在springboot的配置中设置了密码

第一步&#xff1a;查看当前的密码是什么&#xff1f; 打开redis-cli.exe&#xff0c;输入config get requirepass&#xff0c;查看当前的密码是什么&#xff1f; 接着&#xff0c;修改redis的配置文件&#xff0c;找到redis的安装目录&#xff0c;找到相关的conf文件&#x…