验证:似乎忽略了字母字符不是数字的事实
Validation: seems to disregard the fact that alpha characters are not numerical
根据标题,当用户在两个计算字段中的任何一个中输入恶意输入(不是 1 或 0)时,我的二进制计算器会触发错误。
输入应限制为 1 或 0。为什么当我输入“a
”时,计算仍然继续进行?
$submit = htmlspecialchars(strip_tags(stripslashes($_POST['submit'])));
$val1 = htmlspecialchars(strip_tags(stripslashes($_POST['val1'])));
$val2 = htmlspecialchars(strip_tags(stripslashes($_POST['val2'])));
$val1_length = strlen($val1);
$val2_length = strlen($val2);
$val1 = array_reverse(str_split($val1, 1));
$val2 = array_reverse(str_split($val2, 1));
// Val 1 - Checking
$count = 0; // count variable counts how many times the loop recurs and stops it appropriately
while ($count < $val1_length) {
if(($val1[$count] != 0) && ($val1[$count] != 1) && (is_numeric($binary_input))) { // checks if input is comprised of 0 or 1
showInputError();
exit(); // input does not contain 0 or 1, abort script and do not attempt further calculations
}
$count = $count + 1; // increment the count variable after one successful loop
} // Val1 was fine
它继续,因为 a
不是数字。
检查这一行:
if(($val1[$count] != 0) && ($val1[$count] != 1) && (is_numeric($binary_input))) {
转换为:如果不等于 0 且不等于 1 并且是数字。
a
:不等于 0 且不等于 1 并且 不是 数字 -> 继续。
改为:
if(!is_numeric($val1[$count]) || !in_array(intval($val1[$count]),array(0,1)))
我不知道$binary_input
是什么,请解释。
$binary_input
从何而来?我想你应该在那里检查 $val1
和 $val2
,而且如果输入的不是数字,你想显示错误,你应该检查 !(is_numeric($val1) && is_numeric($val2))
...
根据标题,当用户在两个计算字段中的任何一个中输入恶意输入(不是 1 或 0)时,我的二进制计算器会触发错误。
输入应限制为 1 或 0。为什么当我输入“a
”时,计算仍然继续进行?
$submit = htmlspecialchars(strip_tags(stripslashes($_POST['submit'])));
$val1 = htmlspecialchars(strip_tags(stripslashes($_POST['val1'])));
$val2 = htmlspecialchars(strip_tags(stripslashes($_POST['val2'])));
$val1_length = strlen($val1);
$val2_length = strlen($val2);
$val1 = array_reverse(str_split($val1, 1));
$val2 = array_reverse(str_split($val2, 1));
// Val 1 - Checking
$count = 0; // count variable counts how many times the loop recurs and stops it appropriately
while ($count < $val1_length) {
if(($val1[$count] != 0) && ($val1[$count] != 1) && (is_numeric($binary_input))) { // checks if input is comprised of 0 or 1
showInputError();
exit(); // input does not contain 0 or 1, abort script and do not attempt further calculations
}
$count = $count + 1; // increment the count variable after one successful loop
} // Val1 was fine
它继续,因为 a
不是数字。
检查这一行:
if(($val1[$count] != 0) && ($val1[$count] != 1) && (is_numeric($binary_input))) {
转换为:如果不等于 0 且不等于 1 并且是数字。
a
:不等于 0 且不等于 1 并且 不是 数字 -> 继续。
改为:
if(!is_numeric($val1[$count]) || !in_array(intval($val1[$count]),array(0,1)))
我不知道$binary_input
是什么,请解释。
$binary_input
从何而来?我想你应该在那里检查 $val1
和 $val2
,而且如果输入的不是数字,你想显示错误,你应该检查 !(is_numeric($val1) && is_numeric($val2))
...