is_int()不能检查PHP中的$ _GET吗? [英] is_int() cannot check $_GET in PHP?
本文介绍了is_int()不能检查PHP中的$ _GET吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码:
<?php
$ id = $ _GET [id] ;
if(is_int($ id)=== FALSE){
header('HTTP / 1.1 404 Not Found');
exit('404,page not found');
}
?>
它总是进入if。
code> is_int 检查数据类型是一个整数,但是
$ _GET
将是一个字符串。因此,它总是会返回 false
。 在一个捏,你可以投到一个整数,然后检查!= 0。
$ id = isset($ _ GET ['id'])? (int)$ _GET ['id']:null;
if(!$ id){// === 0 || === null
header('HTTP / 1.1 404 Not Found');
exit('404,page not found');
}
但是更强大的解决方案会涉及某种类型的输入字符串验证/像PHP的内置 filter_input_array()
。
(10月13日编辑过,因为它仍然收到upvotes它的措辞有点令人困惑。)
Here is my code:
<?php
$id = $_GET["id"];
if (is_int($id) === FALSE) {
header('HTTP/1.1 404 Not Found');
exit('404, page not found');
}
?>
It always enters inside the if.
解决方案
is_int
checks that the data type is an integer, but everything in $_GET
will be a string. Therefore, it will always return false
.
In a pinch, you could cast to an integer and then check for != 0.
$id = isset($_GET['id']) ? (int) $_GET['id'] : null;
if (!$id) { // === 0 || === null
header('HTTP/1.1 404 Not Found');
exit('404, page not found');
}
But a more robust solution would involve some type of input string validation / filtering, like PHP's built-in filter_input_array()
.
(Edited post on Oct/13 since it is still receiving upvotes and it was somewhat confusingly worded.)
这篇关于is_int()不能检查PHP中的$ _GET吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文