Chống sql injection thật đơn giản
SQL Injection là lỗi khá phổ biến hiện nay. Thường khi bạn tự code mỗi chương trình thì lỗi này là rất khó tránh. Bạn rất khó phát hiện ra những lỗi như blind sql injection. Sau đây là cách fix triệt để lỗi trên
trường hơp xử lý id là integer
cấu trúc
PHP Code:
$id=intval($_GET[id]);
ex
http://binhphuoc.org/diendan/newthread.php?do=newthread&f=71
Trường hợp text id=abc...
PHP Code:
$id=getchar($_GET[id]);
PHP Code:
function getchar($text){
$text = strip_tags($text, '');
$text = preg_replace("#(\r\n|\r|\n)#s", "", $text);
$text = preg_replace("#(\'|;|\"|`|\(|\)|!|>|<)#s", "", $text);
$entities=true;
return mysql_real_escape_string(str_replace(
// replace special html characters
array('<', '>', '"'),
array('<', '>', '"'),
preg_replace(
// translates all non-unicode entities
'/&(?!' . ($entities ? '#[0-9]+|shy' : '(#[0-9]+|[a-z]+)') . ';)/si',
'&',
$text
)
));
}
Xem thêm:
Chống thực thi shell trong một thư mục (23/2/2010)
Chống sql injection thật đơn giản
Nguồn www.bovai.com