私は挿入されたデータをmysqlクエリに入力する前にそれをフィルタリングするための2つのシンプルな関数を作成しました。フォームフィールドのためにこれはデータをフィルタリングしてSQLインジェクションやその他の攻撃を防ぐ安全な方法ですか?
(私も個別に各フィールドをチェックするために正規表現を使用しています
// Form filter
function filter($var)
{
// HTML is not allowed
$var = strip_tags(trim($var));
// Check magic quotes and stripslashes
if(get_magic_quotes_gpc())
{
$var = stripslashes($var);
}
// Not using it right now, is it recommended?
// $var = htmlentities($var, ENT_QUOTES);
// Escape
$var = mysql_real_escape_string($var);
// Return
return $var;
}
次にURLに送信されたIDの()のために、私はこのフィルタを使用しています:。
// ID filter
function idfilter($idfilter)
{
// Delete everything except numbers
$idfilter = ereg_replace("[^0-9]", "", $idfilter);
// Round numbers
$idfilter = round($idfilter);
// Test if the input is indeed a number
if(!is_numeric($idfilter) || $idfilter % 1 != 0)
{
$idfilter = 0;
}
// Filter using the formfilter (above)
return filter($idfilter);
}
があります
ありがとう、私はそれを見ていきます! – mat
+1ですが、[htmlspecialchars](http://www.php.net/manual/en/function.htmlspecialchars.php)を使用すると、HTMLコードのように表示され、解釈されないようにしたい場合や、もちろん剥奪すべきではありません。 –
前述したように、正規表現の作業のために 'preg_ *'のために 'ereg_ *'をダンプします。 – Dan