PHP 怎么PHP 反馈收集

wen PHP项目 3

本文目录导读:

PHP 怎么PHP 反馈收集

  1. 基础反馈表单
  2. 处理反馈提交
  3. 数据库表结构
  4. 管理后台查看反馈
  5. 防刷和验证码
  6. 安全建议

我来帮你详细介绍PHP反馈收集的实现方法。

基础反馈表单

HTML表单页面 (feedback.html)

<!DOCTYPE html>
<html>
<head>用户反馈</title>
</head>
<body>
    <form action="submit_feedback.php" method="POST">
        <div>
            <label>姓名:</label>
            <input type="text" name="name" required>
        </div>
        <div>
            <label>邮箱:</label>
            <input type="email" name="email" required>
        </div>
        <div>
            <label>反馈类型:</label>
            <select name="type" required>
                <option value="bug">Bug报告</option>
                <option value="feature">功能建议</option>
                <option value="complaint">投诉</option>
                <option value="other">其他</option>
            </select>
        </div>
        <div>
            <label>反馈内容:</label>
            <textarea name="content" rows="5" required></textarea>
        </div>
        <button type="submit">提交反馈</button>
    </form>
</body>
</html>

处理反馈提交

submit_feedback.php

<?php
// 开启session
session_start();
// 数据库配置
$host = 'localhost';
$dbname = 'feedback_system';
$username = 'root';
$password = '';
try {
    // 连接数据库
    $pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // 验证表单数据
    if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
        throw new Exception('非法请求');
    }
    // 获取并清理数据
    $name = htmlspecialchars(trim($_POST['name']));
    $email = filter_var(trim($_POST['email']), FILTER_SANITIZE_EMAIL);
    $type = htmlspecialchars(trim($_POST['type']));
    $content = htmlspecialchars(trim($_POST['content']));
    // 验证邮箱格式
    if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        throw new Exception('邮箱格式不正确');
    }
    // 验证内容长度
    if (strlen($content) < 10) {
        throw new Exception('反馈内容至少10个字符');
    }
    // 获取IP和User Agent
    $ip_address = $_SERVER['REMOTE_ADDR'];
    $user_agent = $_SERVER['HTTP_USER_AGENT'];
    // 准备SQL语句
    $sql = "INSERT INTO feedback (name, email, type, content, ip_address, user_agent, created_at) 
            VALUES (:name, :email, :type, :content, :ip_address, :user_agent, NOW())";
    $stmt = $pdo->prepare($sql);
    // 绑定参数
    $stmt->bindParam(':name', $name);
    $stmt->bindParam(':email', $email);
    $stmt->bindParam(':type', $type);
    $stmt->bindParam(':content', $content);
    $stmt->bindParam(':ip_address', $ip_address);
    $stmt->bindParam(':user_agent', $user_agent);
    // 执行插入
    $stmt->execute();
    // 发送通知邮件(可选)
    sendNotificationEmail($name, $email, $type, $content);
    // 记录日志
    error_log("Feedback submitted by: $name ($email)");
    // 返回成功信息
    $_SESSION['feedback_success'] = '感谢您的反馈!我们会尽快处理。';
    header('Location: feedback_success.php');
    exit;
} catch (Exception $e) {
    // 错误处理
    $_SESSION['feedback_error'] = $e->getMessage();
    header('Location: feedback_form.php');
    exit;
}
// 发送通知邮件的函数
function sendNotificationEmail($name, $email, $type, $content) {
    $to = 'admin@example.com';
    $subject = "新的反馈 - $type";
    $message = "
        <html>
        <body>
            <h2>新的反馈信息</h2>
            <p><strong>姓名:</strong>$name</p>
            <p><strong>邮箱:</strong>$email</p>
            <p><strong>类型:</strong>$type</p>
            <p><strong>内容:</strong>$content</p>
        </body>
        </html>
    ";
    $headers = "MIME-Version: 1.0" . "\r\n";
    $headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
    $headers .= "From: feedback@example.com" . "\r\n";
    mail($to, $subject, $message, $headers);
}
?>

数据库表结构

CREATE TABLE IF NOT EXISTS feedback (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(255) NOT NULL,
    type ENUM('bug', 'feature', 'complaint', 'other') NOT NULL,
    content TEXT NOT NULL,
    status ENUM('pending', 'processing', 'resolved', 'closed') DEFAULT 'pending',
    ip_address VARCHAR(45),
    user_agent TEXT,
    created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
    updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_status (status),
    INDEX idx_created_at (created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

管理后台查看反馈

admin_feedback.php

<?php
session_start();
require_once 'auth_check.php'; // 权限验证
// 分页设置
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$perPage = 10;
$offset = ($page - 1) * $perPage;
// 筛选条件
$typeFilter = isset($_GET['type']) ? $_GET['type'] : '';
$statusFilter = isset($_GET['status']) ? $_GET['status'] : '';
try {
    $pdo = new PDO("mysql:host=localhost;dbname=feedback_system;charset=utf8", "root", "");
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    // 构建查询
    $where = [];
    $params = [];
    if ($typeFilter) {
        $where[] = "type = :type";
        $params[':type'] = $typeFilter;
    }
    if ($statusFilter) {
        $where[] = "status = :status";
        $params[':status'] = $statusFilter;
    }
    $whereClause = $where ? "WHERE " . implode(" AND ", $where) : "";
    // 获取总数
    $countSql = "SELECT COUNT(*) FROM feedback $whereClause";
    $countStmt = $pdo->prepare($countSql);
    $countStmt->execute($params);
    $totalItems = $countStmt->fetchColumn();
    $totalPages = ceil($totalItems / $perPage);
    // 获取数据
    $sql = "SELECT * FROM feedback $whereClause ORDER BY created_at DESC LIMIT :limit OFFSET :offset";
    $stmt = $pdo->prepare($sql);
    $stmt->bindValue(':limit', $perPage, PDO::PARAM_INT);
    $stmt->bindValue(':offset', $offset, PDO::PARAM_INT);
    foreach ($params as $key => $value) {
        $stmt->bindValue($key, $value);
    }
    $stmt->execute();
    $feedbacks = $stmt->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $e) {
    die("数据库错误: " . $e->getMessage());
}
?>
<!DOCTYPE html>
<html>
<head>反馈管理</title>
    <style>
        table { width: 100%; border-collapse: collapse; }
        th, td { padding: 10px; text-align: left; border-bottom: 1px solid #ddd; }
        th { background-color: #f5f5f5; }
        .pagination { margin-top: 20px; }
        .pagination a { padding: 5px 10px; margin: 0 2px; border: 1px solid #ddd; text-decoration: none; }
        .pagination .active { background-color: #007bff; color: white; }
    </style>
</head>
<body>
    <h1>用户反馈管理</h1>
    <!-- 筛选表单 -->
    <form method="GET" style="margin-bottom: 20px;">
        <select name="type">
            <option value="">所有类型</option>
            <option value="bug" <?= $typeFilter == 'bug' ? 'selected' : '' ?>>Bug报告</option>
            <option value="feature" <?= $typeFilter == 'feature' ? 'selected' : '' ?>>功能建议</option>
            <option value="complaint" <?= $typeFilter == 'complaint' ? 'selected' : '' ?>>投诉</option>
        </select>
        <select name="status">
            <option value="">所有状态</option>
            <option value="pending" <?= $statusFilter == 'pending' ? 'selected' : '' ?>>待处理</option>
            <option value="processing" <?= $statusFilter == 'processing' ? 'selected' : '' ?>>处理中</option>
            <option value="resolved" <?= $statusFilter == 'resolved' ? 'selected' : '' ?>>已解决</option>
        </select>
        <button type="submit">筛选</button>
    </form>
    <!-- 反馈列表 -->
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>姓名</th>
                <th>邮箱</th>
                <th>类型</th>
                <th>内容</th>
                <th>状态</th>
                <th>时间</th>
                <th>操作</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($feedbacks as $fb): ?>
            <tr>
                <td><?= $fb['id'] ?></td>
                <td><?= htmlspecialchars($fb['name']) ?></td>
                <td><?= htmlspecialchars($fb['email']) ?></td>
                <td><?= $fb['type'] ?></td>
                <td><?= mb_substr(htmlspecialchars($fb['content']), 0, 50) ?>...</td>
                <td><?= $fb['status'] ?></td>
                <td><?= $fb['created_at'] ?></td>
                <td>
                    <a href="view_feedback.php?id=<?= $fb['id'] ?>">查看</a>
                    <a href="update_status.php?id=<?= $fb['id'] ?>&status=processing">处理中</a>
                    <a href="update_status.php?id=<?= $fb['id'] ?>&status=resolved">已解决</a>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <!-- 分页 -->
    <div class="pagination">
        <?php for ($i = 1; $i <= $totalPages; $i++): ?>
            <a href="?page=<?= $i ?>&type=<?= $typeFilter ?>&status=<?= $statusFilter ?>" 
               class="<?= $i == $page ? 'active' : '' ?>"><?= $i ?></a>
        <?php endfor; ?>
    </div>
</body>
</html>

防刷和验证码

添加验证码

// 在 feedback.html 表单中添加
<div>
    <label>验证码:</label>
    <input type="text" name="captcha" required>
    <img src="captcha.php" onclick="this.src='captcha.php?'+Math.random()">
</div>
// captcha.php
<?php
session_start();
$code = substr(md5(mt_rand()), 0, 5);
$_SESSION['captcha'] = $code;
$image = imagecreate(100, 30);
$bg = imagecolorallocate($image, 255, 255, 255);
$textcolor = imagecolorallocate($image, 0, 0, 0);
imagestring($image, 5, 20, 5, $code, $textcolor);
header('Content-Type: image/png');
imagepng($image);
imagedestroy($image);
// 在 submit_feedback.php 中验证
if (strtolower($_POST['captcha']) !== strtolower($_SESSION['captcha'])) {
    throw new Exception('验证码错误');
}

安全建议

  1. 使用CSRF令牌保护
  2. 限制提交频率(IP限制)
  3. 数据加密存储(敏感信息)
  4. 定期备份反馈数据
  5. 使用HTTPS传输
  6. 对文件上传做好安全检查

这样你就有了一个完整的PHP反馈收集系统,包括前端表单、后端处理、数据库存储和管理后台,根据你的具体需求,可以选择性地实现这些功能。

抱歉,评论功能暂时关闭!