Heim >Backend-Entwicklung >PHP-Tutorial >PHP安全过滤函数有哪些?

PHP安全过滤函数有哪些?

PHPz
PHPzOriginal
2016-06-13 11:25:491284Durchsuche

PHP安全过滤函数有:1、stripslashes函数;2、htmlspecialchars函数;3、htmlentities函数;4、strip_tags函数;5、intval函数等等。

PHP安全过滤函数有哪些?

现代互联网中,我们经常要 从世界各地的用户中获得输入数据。但是,我们都知道“永远不能相信那些用户输入的数据”。之前做PHP开发的时候,小网站、低需求,也产生了几个不安全的开发作品。PHP的安全过滤函数的使用可以有效的防止像sql攻击、xss攻击的问题。

PHP安全过滤函数

1、stripslashes() 函数

stripslashes()主要功能是删除反斜杠

<?php
echo stripslashes("Who\&#39;s Bill Gates?");
?>

输出结果:

Who&#39;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 参数改变。

注释:该函数是二进制安全的。

<?php
echo strip_tags("Hello <b>world!</b>");
?>
Hello world!

另外还有像intval,md5等函数,在程序中合理使用都可以起到很好的效果。

更多相关知识,请访问 PHP中文网!!

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn