php web请求安全处理

来源:互联网 发布:惠阳政府网络问政 编辑:程序博客网 时间:2024/06/06 03:29
1、urlencode和rawurlencode的区别
<?php test('https://tieba.baidu.com/f?kw=2&fr=wwwt');test(':/?= &#');test('测试');function test($s){    echo "<b>urlencode('$s')</b> = [<b>";    var_dump(urlencode($s));    echo "</b>]<br/>";    echo "<b>rawurlencode('$s')</b> = [<b>";    var_dump(rawurlencode($s));    echo "</b>]<br/>";}//运行结果urlencode('https://tieba.baidu.com/f?kw=2&fr=wwwt') = [D:\software\wamp\www\linux\webApi\test.php:9:string 'https%3A%2F%2Ftieba.baidu.com%2Ff%3Fkw%3D2%26fr%3Dwwwt' (length=54)]rawurlencode('https://tieba.baidu.com/f?kw=2&fr=wwwt') = [D:\software\wamp\www\linux\webApi\test.php:12:string 'https%3A%2F%2Ftieba.baidu.com%2Ff%3Fkw%3D2%26fr%3Dwwwt' (length=54)]urlencode(':/?= &#') = [D:\software\wamp\www\linux\webApi\test.php:9:string '%3A%2F%3F%3D+%26%23' (length=19)]rawurlencode(':/?= &#') = [D:\software\wamp\www\linux\webApi\test.php:12:string '%3A%2F%3F%3D%20%26%23' (length=21)]urlencode('测试') = [D:\software\wamp\www\linux\webApi\test.php:9:string '%E6%B5%8B%E8%AF%95' (length=18)]rawurlencode('测试') = [D:\software\wamp\www\linux\webApi\test.php:12:string '%E6%B5%8B%E8%AF%95' (length=18)]


从上面的执行结果可以看出,urlencode和rawurlencode两个方法在处理字母数字,特殊符号,中文的时候结果都是一样的,唯一的不同是对空格的处理,urlencode处理成“+”,rawurlencode处理成“%20”


2、函数strip_tags:去掉 HTML 及 PHP 的标记

注意:本函数可去掉字串中包含的任何 HTML 及 PHP 的标记字串。若是字串的 HTML 及 PHP 标签原来就有错,例如少了大于的符号,则也会传回错误。而本函数和 fgetss() 有着相同的功能。fgetss是从文件中读取文件,并去掉html和php标记。

<?phpecho strip_tags("Hello <b>world!</b>");
运行结果

Hello world!


3、函数htmlspecialchars, 将特殊字元转成 HTML 格式

htmlspecialchars() 函数把预定义的字符转换为 HTML 实体。
预定义的字符是:
& (和号)成为 &
" (双引号)成为 "
' (单引号)成为 '
< (小于)成为 <
> (大于)成为 >

<?phpecho htmlspecialchars("This is some <b>bold</b> text.&");
运行结果

This is some <b>bold</b> text.&

4、函数htmlentities,将所有的字元都转成 HTML 字串

或许你还在遗憾htmlspecialchars只能处理4个html标记,那么现在你不要遗憾了,htmlentities是转化全部字符。

<?phpecho htmlentities("<? W3S?h????>");

运行结果

<? W3S?h????>


5、addslashes,函数返回在预定义字符之前添加反斜杠的字符串。

预定义字符是:
单引号(')
双引号(")
反斜杠(\)
NULL

<?phpecho addslashes('Shanghai is the "biggest" city in China.');
运行结果

Shanghai is the \"biggest\" city in China.


6、stripslashes是还原addslashes引用的字符串。

<?phpecho stripslashes("Who\'s Bill Gates?");

运行结果

Who's Bill Gates?


原创粉丝点击