php表单的验证详解
在PHP网站开发扶植中,用户注册、登录,留言等等是必不成少的一些实用成果,用户提交的信息数据都是通过form表单提交,然而提交的数据不免会有这样或者那样的错误,不管是有心还是无意,为了保证数据的完整性、安适性,PHP form表单验证是过滤数据的必不成少的环节。大理石平台怎么样
首先我们对用户所有提交的数据都通过 PHP 的 htmlspecialchars() 函数措置惩罚惩罚。把特殊字符转换为 HTML 实体。这意味着 < 和 > 之类的 HTML 字符会被替换为 < 和 > 。这样可防备打击者通过在表单中注入 HTML 或 JavaScript 代码(跨站点脚本打击)对代码进行操作。
当用户提交表单时,,我们将做以下两件工作,:
使用 PHP trim() 函数去除用户输入数据中不须要的字符 (如:空格,tab,换行)。
使用PHP stripslashes()函数去除用户输入数据中的反斜杠 (\)
必需字段
字段 验证法则Name 必须。必需包罗字母和空格。
E-mail 必须。必需包罗有效的电子邮件地点(包罗 @ 和 .)。
Website 可选。如果选填,则必需包罗有效的 URL。
Comment 可选。多行输入字段(文本框)。
上述字段是必需的,不能为空。
格局匹配
1、匹配姓名
“/^[a-zA-Z ]*$/”
只允许空格和字母,”^”暗示开头,”$”暗示结尾,[a-zA-Z ]暗示a-z或者A-Z或者空格中的一个字符。
其实例代码如下:
1
2
3
4
5
6
<?php
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "只允许字母和空格!";
}
?>
2、匹配E-mail
“/([\w-]+\@[\w-]+.[\w-]+)/”
“\w”匹配包孕下划线的任何单词字符。等价于’[A-Za-z0-9_]’;
+匹配前面的子表达式一次或多次;
“-“匹配”-“。
3、匹配URL
“/\b(?:(?:https?|ftp):\/\/|)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i”
保存表单中的值
在用户点击提交按钮后,为确保字段值是否输入正确,我们在HTML的input元素中插添加PHP脚本, 各字段名为: name, email, 和 website。 在评论的 textarea 字段中,我们将脚本放于 <textarea> 和 </textarea> 标签之间。 PHP脚本输出值为: $name, $email, $website, 和 $comment 变量。
例如如下的代码:
1
2
3
4
Name: <input type="text" name="name" value="<?php echo $name;?>">
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
Website: <input type="text" name="website" value="<?php echo $website;?>">
Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
来一个PHP表单验证完整实例,其代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>form</title>
<style>
.error {color: #FF0000;}
</style>
</head>
<body>
<?php
// 界说变量并默认设置为空值
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
if (empty($_POST["name"]))
{
$nameErr = "名字是必须的";
}
else
{
$name = test_input($_POST["name"]);
// 检测名字是否只包罗字母跟空格
if (!preg_match("/^[a-zA-Z ]*$/",$name))
{
$nameErr = "只允许字母和空格";
}
}
if (empty($_POST["email"]))
{
$emailErr = "邮箱是必须的";
}
else
{
$email = test_input($_POST["email"]);
// 检测邮箱是否合法
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email))
{
$emailErr = "犯警邮箱格局";
}
}
if (empty($_POST["website"]))
{
$website = "";
}
else
{
$website = test_input($_POST["website"]);
// 检测 URL 地点是否合法
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website))
{
$websiteErr = "犯警的 URL 的地点";
}
}
if (empty($_POST["comment"]))
{
$comment = "";
}
else
{
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"]))
{
$genderErr = "性别是必须的";
}
else
{
$gender = test_input($_POST["gender"]);
}
}
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<h2>PHP 表单验证实例</h2>
<p><span class="error">* 必须字段。</span></p>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
名字: <input type="text" name="name" value="<?php echo $name;?>">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email" value="<?php echo $email;?>">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
网址: <input type="text" name="website" value="<?php echo $website;?>">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
备注: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
<br><br>
性别:
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">女
<input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">男
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php
echo "<h2>您输入的内容是:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $website;
echo "<br>";
echo $comment;
echo "<br>";
echo $gender;
?>
</body>
</html>
以上这些将展示如何安适地措置惩罚惩罚 PHP 表单。对 HTML 表单数据进行适当的验证对付防止黑客和垃圾邮件很重要!
php表单的验证详解
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/32429.html