php - 在 PHP 中正确使用 isset 和 filter_input

标签 php isset superglobals

我正在为一些简单的功能开发一个基本的 API。我正在捕获如下输入:

if ($action == 'delete' && isset($_POST['targetId']) && isset($_POST['userId'])) {
//The isset causes "Do not access Superglobal _POST array directly" warning in Netbeans
        $userId = filter_input(INPUT_POST, 'userId');
        $beamId = filter_input(INPUT_POST, 'targetId');
}

我是否应该使用filter_input来检查值是否已设置?喜欢:

if ($action == 'delete' && filter_input(INPUT_POST, 'targetId') && filter_input(INPUT_POST, 'userId')) {

}

我不是在这里考虑选项,而是我会对安全且防黑客的最正确的解决方案感到满意。

编辑:是的,以上信息将用作 SQL 的输入

最佳答案

另一个解决方案可以使用filter_has_var()。

if (filter_has_var(INPUT_POST, "userId")) {
    //occurs when $_POST['userId'] is set, even when empty
}

更多信息:Official documentation

关于php - 在 PHP 中正确使用 isset 和 filter_input,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23731976/

相关文章:

PHP isset($this) 并在静态和对象上下文中使用相同的对象方法

php - 访问类内函数中声明的变量 (PHP)

php - $GLOBALS 超全局在传递给函数时被修改

php - 我需要 memcached 和 Opcache 吗?

php - MySQL WHERE 查询不起作用

php - PHP —未定义索引:

codeigniter - 在 codeigniter 对象上的 isset()

php - 了解 PHP $GLOBALS 变量

php - 在 Symfony2 中从实体和存储库登录的正确方法

php - 如何让这个很长的 url 看起来很短?