
最近看了几篇关于防止SQL Injection的文章,跟大家分享一下我学到的东西。
PHP防止SQL Injection主要有两种做法,分别应用两个函数:addslashes()和mysql_real_escape_string()。早期一般用addslashes(),现在主要用mysql_real_escape_string(),这两个有什么分别呢?Alan Storm 作了以下的解释:
PHP’s mysql_real_escape_string function will, more or less, ask mysql what character(s) needs to be escaped, where the addslashses function will just add a backslash in front of and any single quote (‘), double quote (“), backslash (\) or NUL (the NULL byte) character.
我翻译如下:
PHP的mysql_real_escape_string函数会分析哪些字元需要进行处理,而addslashes则单纯对所有的单引号(‘),双引号(“),反斜线(\)和NUL字元加入反斜线。
在同一篇文章中,Waage说(翻译):
mysql_real_escape_string会为以下字元加入反斜线:
\x00, \n, \r, \, ‘, ” 和 \x1a. characters.
而addslash只为以下字元加反斜线:
‘ \ and NUL
Continue reading »