您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 宣城分类信息网,免费分类信息发布

php如何实现表单数据验证

2024/4/20 14:04:28发布8次查看
php如何实现表单数据验证
首先通过“trim()”函数去除用户输入数据中不必要的字符 (如:空格,tab,换行);
示例:
$text = "\t\tthese are a few words :) ... ";$binary = "\x09example string\x0a";$hello = "hello world";var_dump($text, $binary, $hello);print "\n";$trimmed = trim($text);var_dump($trimmed);$trimmed = trim($text, " \t.");var_dump($trimmed);$trimmed = trim($hello, "hdle");var_dump($trimmed);// 清除 $binary 首位的 ascii 控制字符// (包括 0-31)$clean = trim($binary, "\x00..\x1f");var_dump($clean);
输出结果:
string(32) " these are a few words :) ... "string(16) " example string"string(11) "hello world"string(28) "these are a few words :) ..."string(24) "these are a few words :)"string(5) "o wor"string(14) "example string"
然后使用“stripslashes()”函数去除用户输入数据中的反斜杠;
示例:
$str = "is your name o\'reilly?";// 输出: is your name o'reilly?echo stripslashes($str);
最后在调用“htmlspecialchars()”函数将html代码进行转义。
我们对用户所有提交的数据都通过 php 的 htmlspecialchars() 函数处理。
当我们使用 htmlspecialchars() 函数时,在用户尝试提交以下文本域:
<script>location.href('http://www.runoob.com')</script>
该代码将不会被执行,因为它会被保存为html转义代码,如下所示:
&lt;script&gt;location.href('http://www.runoob.com')&lt;/script&gt;
以上代码是安全的,可以正常在页面显示。
以上就是php如何实现表单数据验证的详细内容。
宣城分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录