当magic_quotes_gpc开启时,是否有必要使用mysql_real_escape_string()? [英] Is it necessary to use mysql_real_escape_string(), when magic_quotes_gpc is on?

查看:110
本文介绍了当magic_quotes_gpc开启时,是否有必要使用mysql_real_escape_string()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要防止SQL注入,在magic_quotes_gpc启用时是否必须使用mysql_real_escape_string()?

To prevent SQL injection, is it necessary to use mysql_real_escape_string(), when magic_quotes_gpc is on?

推荐答案

对于一些罕见的编码, 当然,mysql_real_escape_string()仅在带引号的字符串中有效.所以,如果你这样做

For some rare encodings, such as GBk - yes.
But you should revert it not for this reason. Magic quotes should be turned off anyway (and will be in the next PHP version). So, mysql_real_escape_string() is the only escape function is left. Note that it is not sql injection prevention function. Many many people don't understand this point: it's just a part of syntax. It must be used not to "protect" anything, but to assemble syntactically correct SQL query. And must be used every time you build your query, no matter where data come from. Sure it will protect you from SQL injections too, as a side effect.
Of course, mysql_real_escape_string() works only within quoted strings. So, if you do

$num=mysql_real_escape_string($num);
$sql="SELECT INTO table SET data=$num"; /BAD!!!

它不会保护任何东西. 如果要使用不带引号的数字,则必须将其强制转换为正确的类型,例如:

It will protect nothing. If you going to use numbers unquoted, it must be cast to the proper type obligatory, like this:

$num=intval($num);
$sql="SELECT INTO table SET data=$num"; /GOOD

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆