php如何防止注入攻击,如何防止PHP中的代码注入攻击?

mysql_real_escape_string used when insert into database

htmlentities() used when outputting data into webpage

htmlspecialchars() used when??

strip_tags() used when ??

addslashes() used when ??

htmlspecialchars()使用时??

htmlspecialchars与htmlentities大致相同。区别:字符编码。

两者都编码控制字符,如,&等等用于打开标签等。htmlentities也编码来自其他语言的字符,例如变音符号,欧元符号等。如果你的网站是utf,使用htmlspecialchars(),否则使用htmlentities()。

strip_tags()使用时??

htmlspecialchars /实体编码特殊字符,因此它们被显示但不被解释。 strip_tags删除它们。

在实践中,这取决于你需要做什么。

一个例子…你编码了一个论坛,并给用户一个文本字段,使他们可以发布的东西。恶意的只是试试

pictures of kittens here

如果你不做任何事情,链接将显示,点击链接的受害者会得到很多弹出窗口。

如果你htmlentitiy / htmlspecialchar你的输出,文本将按原样。如果你strip_tag它,它只是删除标签并显示它:

pictures of kittens here

有时你可能想要一个混合,在那里留下一些标签,如< b> (strip_tags可以留下某些标签)。这也是不安全的,所以更好地使用一些完全爆炸的库对xss

addslashes

引用php手册:

Returns a string with backslashes before characters that need to be quoted in database queries etc. These characters are single quote (‘), double quote (“), backslash () and NUL (the NULL byte).

An example use of addslashes() is when you’re entering data into a database. For example, to insert the name O’reilly into a database, you will need to escape it. It’s highly recommeneded to use DBMS specific escape function (e.g. mysqli_real_escape_string() for MySQL or pg_escape_string() for PostgreSQL), but if the DBMS you’re using does’t have an escape function and the DBMS uses \ to escape special chars, you can use this function.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值