PHP留言板功能实现:安全存储用户留言
PHP留言板功能实现:安全存储用户留言
本教程将引导你使用PHP和MySQL数据库创建一个简单的留言板功能,并将重点放在安全存储用户提交的数据上。
1. 创建HTML表单 (liuyan.html)
首先,我们需要一个HTML表单来收集用户的留言信息:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
<title>在线留言</title>
<link href='css/css.css' rel='stylesheet' type='text/css' />
</head>
<body>
<table width='1000' height='1801' border='0' align='center' class='web'>
<tr>
<td height='124' class='logo'><img src='img/logo.png' width='435' height='78' align='left' /></td>
</tr>
<tr>
<td height='55'>
<table width='1000' border='0'>
<tr class='daohang'>
<td height='40' bgcolor='#990033'><a href='index.html'>首页</a></td>
<td bgcolor='#990033'><a href='xuexiao.html'>我的学校</a></td>
<td bgcolor='#990033'><a href='zhiyuanzhe.html'>个人简介</a></td>
<td bgcolor='#990033'><a href='zuzhi.html'>我的经历</a></td>
<td bgcolor='#990033'><a href='xiangmu.html'>我的爱好</a></td>
<td bgcolor='#990033'><a href='shequ.html'>我的偶像</a></td>
<td bgcolor='#990033'><a href='liuyan.html'>在线留言</a></td>
</tr>
</table>
</td>
</tr>
<tr>
<td><img src='img/banner1.jpg' width='1010' height='263' /></td>
</tr>
<tr>
<td>
<form action='liuyan.php' method='POST'>
<table width='480' border='0' align='center'>
<tbody>
<tr>
<td height='50'><input name='username' type='text' placeholder='用户名' style=' width:300px; height:30px;'></td>
</tr>
<tr>
<td height='50'><input name='email' type='text' placeholder='邮箱' style=' width:300px; height:30px;'></td>
</tr>
<tr>
<td height='220'><textarea name='message' cols='' rows='' placeholder='留言内容' style=' width:400px; height:200px;'></textarea></td>
</tr>
</tbody>
</table>
<table width='480' border='0' height='100px' align='center'>
<tbody>
<tr>
<td width='224'><button type='submit'>提交</button></td>
<td width='566'><button type='reset'>重置</button></td>
</tr>
</tbody>
</table>
</form>
</td>
</tr>
<tr>
<td height='200' align='center' bgcolor='#000000'>
<p class='foot'>版权所有© 我的个人网站</p>
</td>
</tr>
</table>
</body>
</html>
2. 创建PHP处理脚本 (liuyan.php)
接下来,我们将创建一个名为 liuyan.php 的 PHP 脚本来处理表单提交的数据:
<?php
// 数据库连接信息
$servername = 'localhost';
$dbname = 'liuyan';
$db_username = 'root'; // 替换为你的数据库用户名
$db_password = '123'; // 替换为你的数据库密码
// 创建数据库连接
$conn = new mysqli($servername, $db_username, $db_password, $dbname);
// 检查连接是否成功
if ($conn->connect_error) {
die('连接失败: ' . $conn->connect_error);
}
// 获取表单数据
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$username = $_POST['username'];
$email = $_POST['email'];
$message = $_POST['message'];
// 数据清洗和验证(这里可以添加更多验证规则)
$username = htmlspecialchars(trim($username));
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
$message = htmlspecialchars(trim($message));
// 使用预处理语句防止SQL注入
$stmt = $conn->prepare('INSERT INTO messages (username, email, message) VALUES (?, ?, ?)');
$stmt->bind_param('sss', $username, $email, $message);
if ($stmt->execute()) {
echo '数据已成功保存到数据库中';
header('Location: liuyan.html'); // 跳转回留言板页面
exit;
} else {
echo '保存数据时出错: ' . $stmt->error;
}
$stmt->close();
}
// 关闭数据库连接
$conn->close();
?>
3. 代码说明
- 数据库连接信息: 在
liuyan.php中,你需要根据你的实际情况修改数据库连接信息,包括数据库服务器地址、数据库名称、用户名和密码。 - 数据清洗和验证: 在将数据插入数据库之前,我们对用户输入的数据进行了简单的清洗和验证,以防止跨站脚本攻击 (XSS) 和其他安全问题。
- 预处理语句: 为了防止 SQL 注入攻击,我们使用了预处理语句来执行数据库操作。预处理语句将 SQL 语句和数据分开处理,从而有效地防止攻击者通过恶意输入操控 SQL 查询逻辑。
4. 运行留言板
完成以上步骤后,将 liuyan.html 和 liuyan.php 文件上传到你的 Web 服务器,并在浏览器中访问 liuyan.html 文件即可使用留言板功能。
5. 总结
通过本教程,你学习了如何使用 PHP 和 MySQL 创建一个简单的留言板功能,并了解了如何通过数据清洗、验证和预处理语句来保护你的应用程序免受安全攻击。在实际开发中,你还可以根据需求添加更多功能,例如用户登录、留言审核、分页显示等。
原文地址: https://www.cveoy.top/t/topic/f3BT 著作权归作者所有。请勿转载和采集!