php 安全

如何不使用prepared statment,而是用过滤的方法避免SQL注入呢?

一般来说mysql自带的过滤函数是比较可靠的。mysql_real_escape_string()

但是这个函数在某些字符集中有问题,比如GBK。如果你用UTF8那没关系。

在PHP里设置数据库的字符集不应该用:mysql_query("SET NAMES UTF8");

而应该用:mysql_set_charset()

这样mysql_real_escape_string()在处理GBK编码的时候就不会出问题了。


mysql_real_escape_string() not safe when SET NAMES is used

use mysql_set_charset() instead.

 

Note: This is the preferred way to change the charset. Using mysql_query() to execute SET NAMES .. is not recommended.

 

SET NAMES is usually used to switch the encoding from what is default to what the application needs. This is done in a way that mysql_real_escape_string doesn’t know about this. This means if you switch to some multi byte encoding that allows backslash as 2nd 3rd 4th… byte you run into trouble, because mysql_real_escape_string doesn’t escape correctly. UTF-8 is safe…

Safe way to change encoding is mysql_set_charset, but that is only available in new PHP versions

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值