php安全过滤函数有:1、stripslashes函数;2、htmlspecialchars函数;3、htmlentities函数;4、strip_tags函数;5、intval函数等等。
现代互联网中,我们经常要 从世界各地的用户中获得输入数据。但是,我们都知道“永远不能相信那些用户输入的数据”。之前做php开发的时候,小网站、低需求,也产生了几个不安全的开发作品。php的安全过滤函数的使用可以有效的防止像sql攻击、xss攻击的问题。
php安全过滤函数
1、stripslashes() 函数
stripslashes()主要功能是删除反斜杠
<?phpecho stripslashes("who\'s bill gates?");?>
输出结果:
who's bill gates?
2、htmlspecialchars() 函数
使用这个函数会把字符串转换为html实体
众所周知像 双引号“,单引号‘,在操作对数据表进行操作的时候是很危险的。而使用htmlsprcialchars后,双引号就会变成",单引号就变成'
<?php$str = "this is some <b>bold</b> text.";echo htmlspecialchars($str);?>
以上代码的 html 输出如下(查看源代码):
<!doctype html><html><body>this is some <b>bold</b> text.</body></html>
以上代码的浏览器输出:
this is some <b>bold</b> text.
3、htmlentities() 函数
htmlentities() 把字符转换为 html 实体
htmlentities和htmlsprcialchars的使用和效果都比较类似,htmlsprcialchars只是转换以上几种特殊字符,而htmlentities转换全部的字符。
需要额外说明的一点在于使用htmlentities处理中文的时候第三个参数encoding,要使用正确的编码,否则会出现乱码。一般来说使用htmlsprcialchars转换基本字符就已经足够了。
<?php$str = "<? w3s?h>";echo htmlentities($str);?>
以上代码的 html 输出如下(查看源代码):
<!doctype html><html><body><© w3sçh°°¦§></body></html>
以上代码的浏览器输出:
<? w3s?h>
4、strip_tags()函数
剥去字符串中的 html 标签:
strip_tags() 函数剥去字符串中的 html、xml 以及 php 的标签。
注释:该函数始终会剥离 html 注释。这点无法通过 allow 参数改变。
注释:该函数是二进制安全的。
<?phpecho strip_tags("hello <b>world!</b>");?>
hello world!
另外还有像intval,md5等函数,在程序中合理使用都可以起到很好的效果。
更多相关知识,请访问 !!