我有一些結構鏈接如下...使用is_numeric的表單驗證
http://domain.com?problem_id=23&course_id=4
從GET「域」的預期值(PROBLEM_ID和COURSE_ID)都爲整數。我可以通過簡單的說...
if (is_numeric($_GET['problem_id'])){
//It's safe, so do stuff.
} else {
echo 'It appears you submitted a problem incorrectly. Please contact us for assistance';
exit;
}
或者這是否仍然像SQL注入等nastiness開放?
提出的解決方案
$int_problem_id = (int) $_GET['problem_id'];
if (ctype_digit($int_problem_id)){
//It's safe, so do stuff.
} else {
echo 'It appears you submitted a problem incorrectly. Please contact us for assistance';
exit;
}
http://stackoverflow.com/questions/60174/how-to-prevent-sql-injection-in-php – Hast
請注意['is_numeric()'](http://php.net/manual/en /function.is-numeric.php)可能實際上並沒有做你認爲的事情。以下全部對於is_numeric返回true:'3.14','+ 0123.45e6','0xf4c3b00c','0b10100111001'。是['ctype_digit()'](http://www.php.net/manual/en/function.ctype-digit.php)你正在尋找什麼? – jcsanyi
@jcsanyi:使用ctype_digit是一個更好的方法。謝謝! – gtilflm