当前位置:首页 > PHP

php实现简单留言板

2026-01-29 06:24:37PHP

创建数据库表结构

使用MySQL创建一个名为messages的表,包含idnameemailcontentcreated_at字段:

CREATE TABLE messages (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(100) NOT NULL,
    content TEXT NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

建立数据库连接

创建config.php文件存放数据库连接信息:

<?php
$host = 'localhost';
$dbname = 'your_database';
$username = 'your_username';
$password = 'your_password';

try {
    $pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    die("Connection failed: " . $e->getMessage());
}
?>

创建留言表单

构建HTML表单页面index.php

<?php require 'config.php'; ?>
<!DOCTYPE html>
<html>
<head>
    <title>简单留言板</title>
</head>
<body>
    <h1>留言板</h1>
    <form method="post" action="post_message.php">
        <label>姓名:</label>
        <input type="text" name="name" required><br>
        <label>邮箱:</label>
        <input type="email" name="email" required><br>
        <label>留言内容:</label>
        <textarea name="content" required></textarea><br>
        <button type="submit">提交</button>
    </form>

    <h2>留言列表</h2>
    <?php
    $stmt = $pdo->query("SELECT * FROM messages ORDER BY created_at DESC");
    while ($row = $stmt->fetch()) {
        echo "<div>";
        echo "<p><strong>{$row['name']}</strong> ({$row['email']})</p>";
        echo "<p>{$row['content']}</p>";
        echo "<small>" . date('Y-m-d H:i', strtotime($row['created_at'])) . "</small>";
        echo "</div><hr>";
    }
    ?>
</body>
</html>

处理表单提交

创建post_message.php处理留言提交:

<?php
require 'config.php';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = htmlspecialchars($_POST['name']);
    $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
    $content = htmlspecialchars($_POST['content']);

    try {
        $stmt = $pdo->prepare("INSERT INTO messages (name, email, content) VALUES (?, ?, ?)");
        $stmt->execute([$name, $email, $content]);
        header("Location: index.php");
        exit();
    } catch(PDOException $e) {
        die("Error: " . $e->getMessage());
    }
}
?>

安全增强措施

添加CSRF防护和输入验证:

// 在config.php中添加
session_start();
if (empty($_SESSION['token'])) {
    $_SESSION['token'] = bin2hex(random_bytes(32));
}

// 在表单中添加隐藏字段
<input type="hidden" name="token" value="<?php echo $_SESSION['token']; ?>">

// 在post_message.php中添加验证
if (!isset($_POST['token']) || $_POST['token'] !== $_SESSION['token']) {
    die("Invalid CSRF token");
}

样式美化

添加基本CSS样式到<head>部分:

<style>
    body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
    form { margin-bottom: 30px; }
    label { display: inline-block; width: 80px; }
    input, textarea { margin-bottom: 10px; width: 100%; }
    textarea { height: 100px; }
    button { padding: 8px 15px; background: #4CAF50; color: white; border: none; }
    div.message { margin-bottom: 15px; padding: 10px; background: #f5f5f5; }
    small { color: #666; }
</style>

这个实现包含了留言板的核心功能:数据库存储、表单提交、留言显示和基本安全措施。可以根据需要进一步扩展功能,如分页显示、管理员回复或留言审核等。

php实现简单留言板

标签: 留言板简单
分享给朋友:

相关文章

留言板怎么实现vue

留言板怎么实现vue

使用 Vue 实现留言板 创建 Vue 项目 通过 Vue CLI 或 Vite 创建一个新的 Vue 项目。确保已安装 Node.js 和 npm/yarn。 npm init vue@late…

简单实现vue

简单实现vue

实现一个简易 Vue 框架 以下是一个基于 JavaScript 的简易 Vue 实现,核心功能包括数据响应式、模板编译和依赖收集。 数据响应式 通过 Object.defineProperty…

实现简单vue

实现简单vue

实现一个简单的 Vue 框架 数据响应式系统 通过 Object.defineProperty 实现数据劫持,监听数据变化: function defineReactive(obj, key, v…

php实现留言板功能

php实现留言板功能

PHP 留言板功能实现 数据库设计 创建数据库表 messages,包含以下字段: id:自增主键 name:留言者姓名(VARCHAR) email:留言者邮箱(VARCHAR) content:…

vue实现简单树

vue实现简单树

使用 Vue 实现简单树形结构 递归组件实现 递归组件是 Vue 实现树形结构的常见方式。通过组件调用自身实现无限层级嵌套。 <template> <ul> &…

vue实现简单表格

vue实现简单表格

使用 Vue 实现简单表格 Vue 提供了多种方式实现表格功能,以下是基于 Vue 3 的两种常见方法: 基础表格实现 通过 v-for 指令循环渲染表格数据: <template&…