2007年5月7日 星期一

用PHP函数解决SQL injection

php.ini :
magic_quotes_gpc = off
可以使用 addslashes()對敏感內容做處理,會在敏感字元前加'\'.

但是
magic_quotes_gpc = on
且又使用 addslashes()處理敏感內容,會重複在敏感字元前加'\'.
可能會產生


Tom\'s book

的結果.

solution:

function quotes($content)
{
//如果magic_quotes_gpc=Off,那麽就開始處理
if (!get_magic_quotes_gpc()) {
//判斷$content是否爲陣列
if (is_array($content)) {
//如果$content是陣列,那麽就處理它的每一個單無
foreach ($content as $key=>$value) {
$content[$key] = addslashes($value);
}
}else{
//如果$content不是陣列,那麽就僅處理一次
addslashes($content);
}
} else {
//如果magic_quotes_gpc=On,那麽就不處理
}
return $content;
}

沒有留言:

網誌存檔

關於我自己

Aspire freedom , Hope to do Soming make self complete ~