PHP安全过滤函数有哪些?

PHPz
Release: 2020-09-04 14:44:38
Original
1208 people have browsed it

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?");
?>
Copy after login

输出结果:

Who&#39;s Bill Gates?
Copy after login

2、htmlspecialchars() 函数

使用这个函数会把字符串转换为HTML实体

众所周知像 双引号“,单引号‘,在操作对数据表进行操作的时候是很危险的。而使用htmlsprcialchars后,双引号就会变成",单引号就变成'

<?php
$str = "This is some <b>bold</b> text.";
echo htmlspecialchars($str);
?>
Copy after login

以上代码的 HTML 输出如下(查看源代码):

<!DOCTYPE html>
<html>
<body>
This is some <b>bold</b> text.
</body>
</html>
Copy after login

以上代码的浏览器输出:

This is some <b>bold</b> text.
Copy after login

3、htmlentities() 函数

htmlentities() 把字符转换为 HTML 实体

htmlentities和htmlsprcialchars的使用和效果都比较类似,htmlsprcialchars只是转换以上几种特殊字符,而htmlentities转换全部的字符。

需要额外说明的一点在于使用htmlentities处理中文的时候第三个参数encoding,要使用正确的编码,否则会出现乱码。一般来说使用htmlsprcialchars转换基本字符就已经足够了。

<?php
$str = "<? W3S?h????>";
echo htmlentities($str);
?>
Copy after login

以上代码的 HTML 输出如下(查看源代码):

<!DOCTYPE html>
<html>
<body>
<© W3Sçh°°¦§>
</body>
</html>
Copy after login

以上代码的浏览器输出:

<? W3S?h????>
Copy after login

4、strip_tags()函数

剥去字符串中的 HTML 标签:

strip_tags() 函数剥去字符串中的 HTML、XML 以及 PHP 的标签。

注释:该函数始终会剥离 HTML 注释。这点无法通过 allow 参数改变。

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

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

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

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

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!