Heim >Backend-Entwicklung >PHP-Tutorial >PHP表单验证 -w3school PHP学习笔记 w3school菜鸟 w3school官网 w3school教程下载
注意:在PHP处理表单时请注意数据的安全性
在处理html表单之前对提交数据进行验证是非常有必要的,能够防止黑客的入侵和垃圾邮件的骚扰。
上面的html表单假设应用如下的规则:
字段–验证规则
姓名(name)–必填,必须包含字母和空格
邮件(email)–必需,必须包含有效的电子邮件地址,包括@和.
网址(website)–可选填,如果填则必须是有效的url
评论(comment)–可选填,多行输入字段
性别(gender)–必需,选择一项
html代码如下:
<code><span><span>head</span>></span><span>body</span>><span>form</span><span>action</span>=<span>'test.php'</span><span>method</span>=<span>'post'</span>> 姓名:<span>input</span><span>type</span>=<span>'text'</span><span>name</span>=<span>'name'</span> /><span>br</span>/> 电邮:<span>input</span><span>type</span>=<span>'text'</span><span>name</span>=<span>'email'</span> /><span>br</span>/> 网址:<span>input</span><span>type</span>=<span>'text'</span><span>name</span>=<span>'website'</span> /><span>br</span>/> 评论:<span>textarea</span><span>name</span>=<span>'comment'</span><span>rows</span>=<span>3</span><span>cols</span>=<span>21</span>><span><span>textarea</span>></span><span>br</span>/> 性别:<span>input</span><span>type</span>=<span>"radio"</span><span>name</span>=<span>'gender'</span><span>value</span>=<span>0</span><span>checked</span>=<span>'checked'</span>/>女性 <span>input</span><span>type</span>=<span>"radio"</span><span>name</span>=<span>'gender'</span><span>value</span>=<span>1</span> />男性<span>br</span>/><span>input</span><span>type</span>=<span>'submit'</span><span>value</span>=<span>'提交'</span> /><span><span>form</span>></span><span><span>body</span>></span><span><span>html</span>></span></code>
此表单通过post的方法提交数据
通过PHP验证表单数据:
一般在过滤提交过来的数据时,会用到htmlspecialchars()函数处理数据,它会将html标签字符转换为html实体。例如会被替换为
在我们使用了htmlspecailchars()函数后,下面的代码
<code><span>script</span>><span>location.href(<span>'http://www.hacked.com'</span>)</span><span><span>script</span>></span></code>
将会被转义为:
<code><span>&</span><span>lt</span>;script<span>&</span><span>gt</span>;location<span>.</span>href(<span>'http://www.hacked.com'</span>)<span>&</span><span>lt</span>;/script<span>&</span><span>gt</span>;</code>
,现在这个提交的数据就是安全的,它不会对页面代码造成损坏。
另外,我们除了用htmlspecialchar()函数过滤数据外,还要做两件事:
<code><span><span><?php </span><span>$name</span>=<span>$email</span>=<span>$website</span>=<span>$comment</span>=<span>''</span>; <span>$gender</span>=<span>0</span>; <span>$name</span>=test_input(<span>$_POST</span>[<span>'name'</span>]); <span>$email</span>=test_input(<span>$_POST</span>[<span>'email'</span>]); <span>$website</span>=test_input(<span>$_POST</span>[<span>'website'</span>]); <span>$comment</span>=test_input(<span>$_POST</span>[<span>'comment'</span>]); <span><span>function</span><span>test_input</span><span>(<span>$data</span>)</span>{</span><span>$data</span>=trim(<span>$data</span>);<span>//去除$data数据两侧多余的空格</span><span>$data</span>=stripslashes(<span>$data</span>);<span>//去除$data中的反斜杠</span><span>$data</span>=htmlspecialchars(<span>$data</span>);<span>//将html标签转以为html实体</span><span>return</span><span>$data</span>; } <span>?></span></span></span></code>').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });
以上就介绍了PHP表单验证 -w3school PHP学习笔记,包括了w3school方面的内容,希望对PHP教程有兴趣的朋友有所帮助。