0

I'm working on a couple of pages to manage data coming from a textarea which the user can use to write some content through a basic editor WYSIWYG. In this textarea I abilitate few tags. I'm wondering if the following process is going to be enough to protect myself from spam and other hidden dangers related to sql query injection and so on.

My steps

function string_db ($value) 
{ 
   $value = (get_magic_quotes_gpc()) ? stripslashes($value) : $value; 
   return mysql_real_escape_string($value); 
} 

$content = string_db(trim($_POST['conten']));
$content = strip_tags($content, '<p><a><b><u><i>'); // The 5 tags allowed
$content = str_replace("&lt;", "", $content);
$content = str_replace("&gt;", "", $content); //In case someone try to type html entities instead of html code

//INSERT DATA IN DB

On the page where I display the data previously saved in the db I use:

echo html_entity_decode($contentFromDb);

Is this enough? IS there a list of tests to do in order to prove the effectiveness?

Thanks a lot

4

1 に答える 1

2

代わりに、PHPのPDO機能を使用する必要があります。これを使用して、SQLインジェクションの脆弱性を排除するのに役立つプリペアドステートメントを作成します。

$DBH = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); 
$STH = $DBH->("INSERT INTO table (col1, col2, col3) values (?, ?, ?);");

# assign variables to each place holder, indexed 1-3  
$STH->bindParam(1, $col1var);  
$STH->bindParam(2, $col2var);  
$STH->bindParam(3, $col3var);  

# insert one row  
$col1var = "My first value"  
$col2var = "Value 2";  
$col3var = "Someone's 3rd value";  
$STH->execute();  

# insert another row with different values  
$col1var = "My first value; Query 2"  
$col2var = "Value 2 -- of the second query";  
$col3var = "Someone's 3rd value;#This one has weird characters";  
$STH->execute();  

上記の例では、データベースに接続し、データベースハンドルを。として設定します$DBH。次に、クエリを準備し、名前のないプレースホルダー(?s)を使用します。次に、PHP変数を名前のないプレースホルダーにバインドします。最後に、変数を設定してクエリを実行します。これは、PHP変数のデータを変更するだけで、新しい値で繰り返すことができます。

于 2012-05-27T03:06:46.087 に答える