dav*_*all 11 php arrays validation integer sanitization
我有以下数组,并想知道验证和santizing这个数组的最佳方法是什么,以确保只允许整数?
if(is_array($_POST['taxonomy'])) {
$term_ids = array_map('esc_attr', $_POST['taxonomy']);
}
Run Code Online (Sandbox Code Playgroud)
打印时看起来像这样:
Array
(
[0] => 13
[1] => 12
)
Run Code Online (Sandbox Code Playgroud)
我知道esc_attr不是很安全所以想要更加强化一些东西.
任何帮助都会很棒.
干杯,
戴夫
dec*_*eze 14
由于它是$_POST
数据,您需要检查ctype_digit
(即只包含数字的字符串):
$sanitizedValues = array_filter($_POST['taxonomy'], 'ctype_digit');
Run Code Online (Sandbox Code Playgroud)
请注意,这只是丢弃非数字值.
一种替代方法是使用phps 过滤器功能:
$array = array(
13, 12, '1', 'a'
);
$result = filter_var($array, FILTER_VALIDATE_INT, array(
'flags' => FILTER_REQUIRE_ARRAY,
'options' => array('min_range' => 1)
));
var_dump($result);
/*
array(4) {
[0]=>
int(13)
[1]=>
int(12)
[2]=>
int(1)
[3]=>
bool(false)
}
*/
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3535 次 |
最近记录: |