本文目录导读:

我来详细说明 PHP 项目中实现评论审核功能的几种方案:
基础数据库设计
数据表结构
CREATE TABLE comments (
id INT PRIMARY KEY AUTO_INCREMENT,
content TEXT NOT NULL,
user_id INT NOT NULL,
article_id INT NOT NULL,
status ENUM('pending', 'approved', 'rejected') DEFAULT 'pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
reviewed_at TIMESTAMP NULL,
reviewed_by INT NULL,
reject_reason VARCHAR(255) NULL,
INDEX idx_status (status),
INDEX idx_article (article_id)
);
评论提交与审核流程
评论提交接口
// submit_comment.php
<?php
class CommentController {
public function submit() {
// 1. 验证用户输入
$content = filter_input(INPUT_POST, 'content', FILTER_SANITIZE_STRING);
$articleId = filter_input(INPUT_POST, 'article_id', FILTER_VALIDATE_INT);
// 2. 保存评论(默认状态为待审核)
$comment = new Comment();
$comment->content = $content;
$comment->article_id = $articleId;
$comment->user_id = $_SESSION['user_id'];
$comment->status = 'pending'; // 待审核
$comment->save();
// 3. 通知管理员(可选)
$this->notifyAdmin($comment);
return ['success' => true, 'message' => '评论已提交,等待审核'];
}
private function notifyAdmin($comment) {
// 发送邮件/站内信通知管理员
}
}
审核后台
// admin/review_comments.php
<?php
class ReviewController {
// 获取待审核评论列表
public function getPendingComments($page = 1, $perPage = 20) {
$offset = ($page - 1) * $perPage;
return Comment::where('status', 'pending')
->orderBy('created_at', 'desc')
->limit($perPage)
->offset($offset)
->get();
}
// 审核通过
public function approve($commentId) {
$comment = Comment::find($commentId);
if (!$comment) {
return ['success' => false, 'message' => '评论不存在'];
}
$comment->status = 'approved';
$comment->reviewed_at = now();
$comment->reviewed_by = auth()->id();
$comment->save();
// 可选:通知用户评论已通过
$this->notifyUser($comment, 'approved');
return ['success' => true];
}
// 审核拒绝
public function reject($commentId, $reason = '') {
$comment = Comment::find($commentId);
if (!$comment) {
return ['success' => false, 'message' => '评论不存在'];
}
$comment->status = 'rejected';
$comment->reviewed_at = now();
$comment->reviewed_by = auth()->id();
$comment->reject_reason = $reason;
$comment->save();
// 可选:通知用户评论被拒绝
$this->notifyUser($comment, 'rejected');
return ['success' => true];
}
}
前端展示控制
文章页评论展示
// article.php
<?php
class ArticleController {
public function showComments($articleId) {
// 只显示已审核通过的评论
$approvedComments = Comment::where('article_id', $articleId)
->where('status', 'approved')
->orderBy('created_at', 'desc')
->get();
// 显示用户自己的评论(不管审核状态)
$userComments = [];
if (auth()->check()) {
$userComments = Comment::where('user_id', auth()->id())
->where('article_id', $articleId)
->get();
}
return view('article.comments', [
'approvedComments' => $approvedComments,
'userComments' => $userComments
]);
}
}
自动审核(可选扩展)
敏感词过滤
<?php
class AutoReview {
private $blacklist = ['敏感词1', '敏感词2', '广告链接'];
private $whitelist = ['白名单用户1', '白名单用户2'];
public function autoReview($comment) {
// 白名单用户自动通过
if (in_array($comment->user->username, $this->whitelist)) {
return 'approved';
}
// 检查敏感词
foreach ($this->blacklist as $word) {
if (strpos($comment->content, $word) !== false) {
return 'rejected';
}
}
// 检查垃圾内容
if ($this->isSpam($comment)) {
return 'rejected';
}
// 默认返回待审核
return 'pending';
}
private function isSpam($comment) {
// 检查是否包含过多链接
$linkCount = preg_match_all('/https?:\/\/[^\s]+/', $comment->content);
if ($linkCount > 3) {
return true;
}
// 检查重复内容
$similarComments = Comment::where('content', 'LIKE', '%' . $comment->content . '%')
->count();
if ($similarComments > 5) {
return true;
}
return false;
}
}
批量审核功能
<?php
class BatchReviewController {
public function batchApprove() {
$commentIds = request()->input('comment_ids', []);
Comment::whereIn('id', $commentIds)
->where('status', 'pending')
->update([
'status' => 'approved',
'reviewed_at' => now(),
'reviewed_by' => auth()->id()
]);
return ['success' => true, 'message' => '批量审核通过'];
}
public function batchReject() {
$commentIds = request()->input('comment_ids', []);
$reason = request()->input('reason', '');
Comment::whereIn('id', $commentIds)
->where('status', 'pending')
->update([
'status' => 'rejected',
'reviewed_at' => now(),
'reviewed_by' => auth()->id(),
'reject_reason' => $reason
]);
return ['success' => true, 'message' => '批量拒绝成功'];
}
}
通知系统
<?php
class NotificationService {
public function notifyUser($comment, $action) {
if ($action === 'approved') {
// 用户通知:评论已通过
$message = "您的评论「{$comment->content}」已通过审核";
} else {
// 用户通知:评论被拒绝
$reason = $comment->reject_reason ?: "未提供原因";
$message = "您的评论「{$comment->content}」未通过审核,原因:{$reason}";
}
// 发送邮件或站内信
Notification::send($comment->user, [
'title' => '评论审核结果',
'content' => $message,
'type' => 'comment_review'
]);
}
}
管理界面模板示例
<!-- admin/comments.html -->
<div class="comment-list">
<h2>评论审核管理</h2>
<div class="filter-bar">
<select id="status-filter">
<option value="all">全部</option>
<option value="pending">待审核</option>
<option value="approved">已通过</option>
<option value="rejected">已拒绝</option>
</select>
<button onclick="batchApprove()">批量通过</button>
<button onclick="batchReject()">批量拒绝</button>
</div>
<table>
<thead>
<tr>
<th><input type="checkbox" id="select-all"></th>
<th>ID</th>
<th>用户</th>
<th>内容</th>
<th>状态</th>
<th>提交时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach ($comments as $comment): ?>
<tr>
<td><input type="checkbox" class="comment-checkbox" value="<?= $comment->id ?>"></td>
<td><?= $comment->id ?></td>
<td><?= htmlspecialchars($comment->user->username) ?></td>
<td><?= htmlspecialchars($comment->content) ?></td>
<td>
<span class="status-badge status-<?= $comment->status ?>">
<?= $comment->status_label ?>
</span>
</td>
<td><?= $comment->created_at ?></td>
<td>
<button onclick="approve(<?= $comment->id ?>)">通过</button>
<button onclick="reject(<?= $comment->id ?>)">拒绝</button>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
建议的审核流程
- 提交阶段:用户提交评论后默认为待审核
- 自动审核:敏感词过滤、垃圾内容检测
- 人工审核:管理员在后台审核评论
- 通知用户:审核结果通知
- 展示控制:前端只显示已审核通过的评论
这种方案适合中大型项目,可以根据实际需求选择实现完整功能或简化部分流程。