2012-10-05 90 views
0

使用get_magic_quotes_gpc防止數據庫攻擊仍然相關嗎?如果啓用了魔術引號,我想剝去額外的斜線。防止數據庫攻擊

if(get_magic_quotes_gpc()){ 
    If magic quotes is enabled, strip the extra slashes 
    array_walk_recursive($_GET,create_function('&$v,$k','$v = stripslashes($v);')); 
    array_walk_recursive($_POST,create_function('&$v,$k','$v = stripslashes($v);')); 
} 

我看着php手冊,看到它已被棄用。我不確定我可以使用哪些替代方案,或者如果可能有一些調整,我不知道。因爲我對編程和學習不同的編碼技術仍然陌生。任何提示將不勝感激

+4

你真的應該使用參數化查詢到PDO:http://stackoverflow.com/questions/ 60174 /最好的方式,以防止SQL注入在PHP –

+0

SO應該只是自動化關於使用PDO的帖子,當涉及到PHP的mysql_問題。 – ficuscr

+0

謝謝你的鏈接@JordanKaye – Octavius

回答

1

使用此

function mysql_prep($value) 
{ 
    $magic_quotes_active = get_magic_quotes_gpc(); 
    $new_enough_php = function_exists("mysql_real_escape_string"); 
    if ($new_enough_php) { 
     // undo any magic quote effects so mysql_real_escape_string can do the work 
     if ($magic_quotes_active) { 
      $value = stripslashes($value); 
     } 
     $value = mysql_real_escape_string($value); 
    } else { 
     // if magic quotes aren't already on then add slashes manually 
     if (!$magic_quotes_active) { 
      $value = addslashes($value); 
     } 
     // if magic quotes are active, then the slashes already exist 
    } 
    return ($value); 
} 

我會建議你prepared statement

$q=$pdo->prepare("query where id=:id"); 
$q->execute(array(":id"=>1)) 
+0

不要侮辱jhonraymos。但在使用該代碼之前,我會升級PHP。我知道這並不總是一個完美的世界。 – ficuscr

+0

你會如何去升級它,爲什麼呢? @ficuscr – Octavius

+1

只是說。 PHP 4.3在2002年發佈。我通常不會對不支持並且超過10年的東西進行編碼。 – ficuscr