当前位置:首页 > 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实现简单留言板

<?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实现简单留言板

<?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>

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

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

相关文章

简单实现vue

简单实现vue

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

vue 实现简单登录

vue 实现简单登录

实现 Vue 简单登录功能 创建登录组件 在 Vue 项目中创建一个登录组件 Login.vue,包含用户名和密码输入框以及登录按钮。 <template> <div clas…

vue 实现简单表格

vue 实现简单表格

以下是在 Vue 中实现简单表格的几种方法: 使用原生表格标签 通过 Vue 的数据绑定功能渲染表格数据: <template> <table> <the…

vue实现简单弹幕效果

vue实现简单弹幕效果

实现基础弹幕效果 在Vue中实现弹幕效果需要结合CSS动画和动态数据渲染。以下是一个基础实现方案: 模板部分 <template> <div class="danmu-con…

vue实现简单图片切换

vue实现简单图片切换

以下是在Vue中实现简单图片切换的几种方法: 使用v-bind动态绑定图片路径 通过data属性存储图片数组和当前索引,利用v-bind动态切换src属性: data() { return {…

vue实现简单登录验证

vue实现简单登录验证

实现登录表单基础结构 使用Vue的模板语法构建登录表单,包含用户名和密码输入框及提交按钮。表单需绑定v-model实现双向数据绑定,并监听submit事件。 <template> &…