New FAMILUG

The PyMiers

Thursday 29 March 2012

Tiêm phòng chống SQL injection

Viết ngay kiểu quên, úp dết sau =))

1. Validate Input:
Dùng hàm để lọc input.
function sql_quote( $value ){
// kiem tra thiet lap trong file php.ini (bien ten la magic_quotes_gpc),
if( get_magic_quotes_gpc() ) //neu tra ve 1 -> tu la magic_quotes_gpc = ON
{
//để loại bỏ các dấu \
$value = stripslashes( $value );
}
//',",<,>,*,/,-,#,&
//check if this function exists : neu co ton tai thi PHP tu 5.0 tro len
if( function_exists( "mysql_real_escape_string" ) )
{
$value = mysql_real_escape_string( $value );
}
else
{ //for PHP version < 4.3.0 use addslashes
$value = addslashes( $value );
} //thoat ky tu : _ , % $value = str_replace("_","\_",$value);
$value = str_replace("%","\%",$value);
return $value;
}
?>


2. Dùng rewiteurl trong .htaccess để thay đổi url

3. Cắt ký tự nguy hiểm:(', \, "..)
Dùng trim();

4. Chặn thông báo lỗi:
- Thêm @ trước hàm dễ gây lỗi.
- error_reporting(0) ở đầu đoạn code php.

No comments:

Post a Comment