可能重複:
How to prevent SQL injection in PHP?抵禦Mysql的注射用PHP escapestring(任何這樣做的真正的好辦法
我想問問關於防範SQL注入的一些問題。從我一直在讀的內容中,我總會遇到以下三件事:
- stripslashes
- 其一起使用magic_quotes_gpc的
- mysql_real_escape_string(或mysqli的我在較新的PHP的假設?)
的問題是,我應該使用這兩種或將real_escape_string足夠?
例如,我有這行代碼有關的註冊頁面(我知道一個事實是脆弱的,因爲sqli助手讓我找到關於我的數據庫的一切:(因爲我還沒有實現任何上述) :
if(isset($_POST['submit'])){
//cleanup the variables
$username = ($_POST['username']);
$password = ($_POST['password']);
$email = ($_POST['email']);
$username = sanitise($username);
$password = sanitise($password);
$email = sanitise($email);
//quick/simple validation
if(empty($username)){ $action['result'] = 'error'; array_push($text,'Please type in a username'); }
if(empty($password)){ $action['result'] = 'error'; array_push($text,'Please type in a password'); }
if($action['result'] != 'error'){
$password = md5($password);
//add to the database
$add = mysql_query("INSERT INTO Users VALUES(NULL,'$username','$password','$email',0, 'First', 'Last', 'Phone Number Here', '...', 'Something about me...')");
if($add){
//get the new user id
$userid = mysql_insert_id();
//create a random key
$key = $username . $email . date('mY');
$key = md5($key);
//add confirm row
$confirm = mysql_query("INSERT INTO Confirm VALUES(NULL,'$userid','$key','$email')");
if($confirm){
//include the swift class
include_once 'swift/swift_required.php';
//put info into an array to send to the function
$info = array(
'username' => $username,
'email' => $email,
'key' => $key);
//send the email
if(send_email($info)){
//email sent
$action['result'] = 'success';
array_push($text,'Thanks for signing up. Please check your e-mail for confirmation.');
}else{
$action['result'] = 'error';
array_push($text,'Could not send confirmation e-mail');
}
}else{
$action['result'] = 'error';
array_push($text,'Confirm row was not added to the database. Reason: ' . mysql_error());
}
}else{
$action['result'] = 'error';
array_push($text,'User could not be added to the database. Reason: ' . mysql_error());
}
}
$action['text'] = $text;
}
?>
我想我的禁制功能將有助於事情 - 得到了它的在線,但它似乎是有點用處或許這不僅有助於防止跨站腳本這是:
function cleanInput($input) {
$search = array(
'@<script[^>]*?>
.*?</script>@si', // Strip out javascript
'@<[\/\!]*?[^<>]*?>@si', // Strip out HTML tags
'@<style[^>]*?>.*?
</style>
@siU', // Strip style tags properly
'@<![\s\S]*?--[ \t\n\r]*>@' // Strip multi-line comments
);
$output = preg_replace($search, '', $input);
return $output;
}
function sanitise($input) {
if (is_array($input)) {
foreach($input as $var=>$val) {
$output[$var] = sanitise($val);
}
}
else {
if (get_magic_quotes_gpc()) {
$input = stripslashes($input);
}
$input = cleanInput($input);
$output = $input;
}
return $output;
}
。
你會建議功能是沒用?
如果是的話,我將如何去保護的代碼原有的位?即:
$username = ($_POST['username']);
$password = ($_POST['password']);
$email = ($_POST['email']);
關於這個問題已經有一個非常大的話題,並且一個簡單的搜索會顯示它。 http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php –
確實如此,但我也在問關於粘貼的功能 - 我做了搜索。另外我對如何使用上面的post方法實現它有點困惑。因此請求新的線程。還請注意有關魔術引號和反斜槓的查詢,以及它們是否必要 –
使用PDO或MySQLi切換到準備好的聲明。 – MrCode