从留言板开始做网站(七)——服务端的数据验证和安全输入
2016-10-07 18:52
666 查看
留言板的基本功能已经实现,但是还是存在非常多的问题的。例如有用户忘记输入用户名或者留言内容或者直接在地址栏输入数据处理页localhost/message/send.php,所以我们需要一个验证的手段和安全性的检验,验证的手段非常多,有客户端的有服务端的。
首先检测表单是否提交,在send.php中添加以下代码:
这可以防止直接在地址栏输入处理页send.php。
对用户输入的内容进行过滤,我们新建一个自定义函数testInput(),这样可以提高重复性
本人知道的所有安全性就只有这些啦,完整的代码如下:
首先检测表单是否提交,在send.php中添加以下代码:
if ($_SERVER["REQUEST_METHOD"] == "POST"){ ....正常的数据处理代码 }else { header("refresh:3;url=index.php"); print('对不起,操作有误,页面正在跳转,请重新留言!'); }
这可以防止直接在地址栏输入处理页send.php。
对用户输入的内容进行过滤,我们新建一个自定义函数testInput(),这样可以提高重复性
//check the user's data function checkInput($data) { $data = trim($data); //移除字符串两侧的空白字符或者预定义字符 $data = stripslashes($data); //删除反斜杠 $data = htmlspecialchars($data); //把预定义字符转换为HTML实体 return $data; }完整的代码:
<?php下面进行用户输入空内容的处理:
header("Content-Type:text/html;charset=utf-8");
date_default_timezone_set('PRC');
if ($_SERVER["REQUEST_METHOD"] == "POST"){
// import the connection file
require "conn.php";
// insert the data to the table
$user_name = checkInput($_POST["user_name"]);
$user_mes = checkInput($_POST["user_mes"]);
$send_time = date("Y-m-d H:i:s");
// set the data encoding is utf8
$conn->query("set names 'utf8'");
$sql = "INSERT INTO message (user_name, user_mes, send_time)
VALUES ('$user_name', '$user_mes', '$send_time')";
// check it
if($conn->query($sql) === true) {
header("Location:index.php");
}else {
echo "数据插入失败" . $conn->error;
}
// close the datebase connection
$conn->close();
}else {
header("refresh:3;url=index.php");
print('对不起,操作有误,页面正在跳转,请重新留言!');
}
//check the user's data function checkInput($data) { $data = trim($data); //移除字符串两侧的空白字符或者预定义字符 $data = stripslashes($data); //删除反斜杠 $data = htmlspecialchars($data); //把预定义字符转换为HTML实体 return $data; }
?>
if (empty($user_name) or empty($user_mes)) { header("refresh:3;url=index.php"); print('对不起,用户昵称或留言内容不得为空,页面正在跳转,请重新留言!'); }else { $sql = "INSERT INTO message (user_name, user_mes, send_time) VALUES ('$user_name', '$user_mes', '$send_time')"; // check it if($conn->query($sql) === true) { header("Location:index.php"); }else { echo "数据插入失败" . $conn->error; } }一般的用户名称,为中文、英文以及数字的集合,当然也有其他符号类型,但是我们在这里限制用户在用户名中输入任何字符,只能输入中文、英文和数字。这个就需要正则表达式了。
if (preg_match("/^[\x{4e00}-\x{9fa5}a-zA-Z0-9]{3,60}$/u", $user_name))preg_match 是匹配正则表达式的函数,刚开始我的正则表达式是这样写的:/^[\u4e00-\u9fa5a-zA-Z0-9]{3,60}$/,结果编译出错,显示不支持这种格式,我百度了下,发现了这位前辈写的博文和我的情况几乎一样:PHP正则匹配中文 然后按照他的方法写,结果能正常匹配了。
if (preg_match("/^[\x{4e00}-\x{9fa5}a-zA-Z0-9]{3,60}$/u", $user_name)) { $sql = "INSERT INTO message (user_name, user_mes, send_time) VALUES ('$user_name', '$user_mes', '$send_time')"; // check it if($conn->query($sql) === true) { header("Location:index.php"); }else { echo "数据插入失败" . $conn->error; } }else { // header("refresh:3;url=index.php"); // print('对不起,用户名仅限中英文和数字,页面正在跳转,请重新填写!'); echo "<script>alert('对不起,用户名仅限中英文和数字!'); location='index.php';</script>"; }在匹配不成功的时候,小伙伴们应该发现了,我把原来的跳转方法给注释掉了,因为他们不跳转,我就转用了JS方法。header()不跳转硬挨是因为前面的print('对不起,用户昵称或留言内容不得为空,页面正在跳转,请重新留言!');这条输出语句的关系,我不知道我说的是否正确,还请看到的大神指点下。
本人知道的所有安全性就只有这些啦,完整的代码如下:
<?php以上都是在服务端的验证,但是这样其实并不友好,最佳的应该是在客户端进行一些基本的验证,而服务端的作为后备手段,在浏览器被禁止使用脚本的时候的,发挥作用。这个我们到下篇讲吧!
header("Content-Type:text/html;charset=utf-8");
date_default_timezone_set('PRC');
if ($_SERVER["REQUEST_METHOD"] == "POST"){
// import the connection file
require "conn.php";
// insert the data to the table
$user_name = checkInput($_POST["user_name"]);
$user_mes = checkInput($_POST["user_mes"]);
$send_time = date("Y-m-d H:i:s");
// set the data encoding is utf8
$conn->query("set names 'utf8'");
if (empty($user_name) or empty($user_mes)) {
header("refresh:3;url=index.php");
print('对不起,用户昵称或留言内容不得为空,页面正在跳转,请重新留言!');
}else {
if (preg_match("/^[\x{4e00}-\x{9fa5}a-zA-Z0-9]{3,60}$/u", $user_name)) { $sql = "INSERT INTO message (user_name, user_mes, send_time) VALUES ('$user_name', '$user_mes', '$send_time')"; // check it if($conn->query($sql) === true) { header("Location:index.php"); }else { echo "数据插入失败" . $conn->error; } }else { // header("refresh:3;url=index.php"); // print('对不起,用户名仅限中英文和数字,页面正在跳转,请重新填写!'); echo "<script>alert('对不起,用户名仅限中英文和数字!'); location='index.php';</script>"; }
}
// close the datebase connection
$conn->close();
}else {
header("refresh:3;url=index.php");
print('对不起,操作有误,页面正在跳转,请重新留言!');
}
//check the user's data
function checkInput($data) {
$data = trim($data); //移除字符串两侧的空白字符或者预定义字符
$data = stripslashes($data); //删除反斜杠
$data = htmlspecialchars($data); //把预定义字符转换为HTML实体
return $data;
}
?>
相关文章推荐
- 从留言板开始做网站(八)——客户端的数据验证(JS验证方法)
- JData 使用教程 对输入数据进行验证(服务端验证)
- 从留言板开始做网站(十)——后台留言处理(数据删除)
- jsData 使用教程(五) 对输入数据进行验证(服务端验证)
- 对用户的输入的数据进行安全验证
- 对用户输入的数据进行安全验证(Safe-Handling User Input)
- 从留言板开始做网站(六)——留言内容显示(数据读取)
- 安全编程: 验证输入--接收用户数据的最佳实践
- 从留言板开始做网站(五)——表单提交(数据插入)
- 数据安全验证之正则表达式
- asp.net应用安全事项输入验证
- 安全编程: 验证输入
- [导入]Ajax即时实现服务端数据验证(Asp.net 2.0)(示例代码下载)
- AjaxPro.NET框架实现服务端即时数据验证(Asp.net 2.0)(示例代码下载)
- AjaxPro.NET框架完成服务端即时数据验证(Asp.net 2.0)(示例代码下载)
- [导入]AjaxPro.NET框架实现服务端即时数据验证(Asp.net 2.0)(示例代码下载)
- 回发或回调参数无效。在配置中使用 或在页面中使用 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEventValidation 方法来注册回发或回调数据以进行验证。
- 回发或回调参数无效。在配置中使用 或在页面中使用 启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据有效并且是预期的,则使用 ClientScriptManager.RegisterForEvent
- Ajax即时实现服务端数据验证(Asp.net 2.0)(示例代码下载)
- AjaxPro.NET框架实现服务端即时数据验证(Asp.net 2.0)(示例代码下载)