2012-04-10 134 views
0

提交的數據我做了如下措施來保護subbmited數據免受SQL攻擊SQL防止SQL注入

$myusername = stripslashes($myusername); 
$myusername = mysql_real_escape_string($myusername); 
$mypassword = stripslashes($mypassword); 
$mypassword = mysql_real_escape_string($mypassword); 
$confirm_password = stripslashes($confirm_password); 
$confirm_password = mysql_real_escape_string($confirm_password); 
$fullname = stripslashes($fullname); 
$fullname = mysql_real_escape_string($fullname); 

是否有這樣做的更簡單的方法?這是一份註冊表格,我有許多領域需要保護。

+1

查看準備好的語句(例如[PDO](http://php.net/manual/en/book.pdo.php)或['mysqli_prepare'](http://www.php.net/manual) /en/mysqli.prepare.php))。 – 2012-04-10 18:09:17

回答

1

有沒有更簡單的方法呢?

是的,首先,disable automatic slashes,所以你不需要去掉它們。這將已經減少代碼:

$myusername = mysql_real_escape_string($myusername); 
$mypassword = mysql_real_escape_string($mypassword); 
$confirm_password = mysql_real_escape_string($confirm_password); 
$fullname = mysql_real_escape_string($fullname); 

如果再使用所謂的參數化查詢,你不需要甚至稱mysql_real_escape_string不再很好,但你可以安全地使用變量。

請注意,您正在使用mysql_real_escape_string的不安全變體,因爲您沒有提供數據庫鏈接。

另請參閱:Best way to stop SQL Injection in PHP

0

是,使用PHP的PDO對象(PHP數據庫對象),並創建prepared statements.

$dbh = new PDO('mysql:dbname=YOURDB;host=localhost', 'user', 'pass'); 
$sql = 'SELECT name FROM user WHERE id = :id'; 
$sth = $dbh->prepare($sql); 
$sth->execute(array(':id' => 25); 
$result = $sth->fetchAll(); 

它也可以幫助你知道你可以找一個PDO包裝,讓您的生活變得更輕鬆。