php实现文章审核
PHP实现文章审核的方法
实现文章审核功能通常需要结合数据库操作、文本过滤和权限管理。以下是几种常见的实现方式:
数据库设计
在数据库中创建文章表时,添加审核状态字段:
CREATE TABLE articles (
id INT AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(255),
content TEXT,
status ENUM('pending', 'approved', 'rejected') DEFAULT 'pending',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
提交文章时设置状态
用户提交文章时,默认设置为待审核状态:
$stmt = $pdo->prepare("INSERT INTO articles (title, content, status) VALUES (?, ?, 'pending')");
$stmt->execute([$title, $content]);
审核后台实现
创建管理员审核界面,列出待审核文章:
$pendingArticles = $pdo->query("SELECT * FROM articles WHERE status = 'pending'")->fetchAll();
审核操作处理
处理通过或拒绝操作:
if (isset($_POST['approve'])) {
$stmt = $pdo->prepare("UPDATE articles SET status = 'approved' WHERE id = ?");
$stmt->execute([$_POST['article_id']]);
}
if (isset($_POST['reject'])) {
$stmt = $pdo->prepare("UPDATE articles SET status = 'rejected' WHERE id = ?");
$stmt->execute([$_POST['article_id']]);
}
前端展示控制
根据审核状态显示不同内容:
$article = $pdo->query("SELECT * FROM articles WHERE id = $id")->fetch();
if ($article['status'] != 'approved' && !$isAdmin) {
die('文章正在审核中');
}
敏感词过滤
在提交时进行敏感词检测:
$forbiddenWords = ['敏感词1', '敏感词2'];
foreach ($forbiddenWords as $word) {
if (strpos($content, $word) !== false) {
die('内容包含敏感词');
}
}
自动审核规则
可以设置一些自动通过规则:
if (strlen($content) > 100 && !containsLinks($content)) {
$status = 'approved';
} else {
$status = 'pending';
}
审核日志记录
记录审核操作:
$pdo->prepare("INSERT INTO audit_logs (user_id, article_id, action, created_at) VALUES (?, ?, ?, NOW())")
->execute([$_SESSION['user_id'], $articleId, $action]);
这些方法可以单独使用或组合使用,根据实际需求调整审核流程的严格程度。







